I know that Java weak reference are implemented differently, e.g. all
objects are inherently weak-referencable and __weakref__ slot is
meaningless. Still, Jython could add special handling for the slot to
simplify things for code that blindly (not checking exact
implementation) uses it.
E.g.:
class X (object):
__slots__ = ('__weakref__')
x = X ()
x.__weakref__ = 1
This code fails in CPython but works fine in Jython. However, it wastes
one slot, making each object unnecessarily larger (I admit I don't
really know how Jython works, but that's what I guess it results in).
Also, it seems Jython is capable of not creating duplicate weakref object:
>>> a = object ()
>>> b = weakref.ref (a)
>>> c = weakref.ref (a)
>>> b, c, b is c
(<weakref at 1; to 'object' at 2>, <weakref at 1; to 'object' at 2>, True)
so x.__weakref__ (descriptor getter) could be implemented as in CPython.
I'm not proposing to artificially limit set of weak-referencable types
--- I'm just asking to make __weakref__ slot "just work as intended"
without caring whether it is CPython or Jython.
|