Message1153
when assigning to the __dict__ attribute of an object
instance, the namespace dictionary (stringmap) is not
replaced, but a '__dict__' key is created, and the new
stringmap is assigned as a value to this key
_in the existing namespace_. This happens both when using
direct instance.__dict__=xyz assignment and when using
the __setattr__() method of the class, of the 'object'
class, or of the instance. The following code
demonstrates the problem:
############### snip ###########################
# let's define a simple class
class C(object):
pass
c=C()
c.attr1=42
print c.__dict__, type(c.__dict__)
# we know Jython uses stringmaps as namespace
# dicts, let's create a new stringmap object
# (perhaps there is a simpler way)
smap_type=type(c.__dict__)
sm2=smap_type.__new__(smap_type)
sm2['attr2']=0
# our first attempt to assign new stringmap to c.__dict__
c.__dict__=sm2
print '#1:', c.__dict__
#this obviously does not work, let's try a different method
c.__setattr__('__dict__', sm2)
print '#2:', c.__dict__
############### snip ###########################
CPython2.4 output:
{'attr1': 42} <type 'dict'>
#1: {'attr2': 0}
#2: {'attr2': 0}
Jython2.2a1 output:
{'attr1': 42} <type 'stringmap'>
#1: {'__dict__': {'attr2': 0}, 'attr1': 42}
#2: {'__dict__': {'attr2': 0}, 'attr1': 42}
This bug hampers many advanced uses of Python's
meta-object protocol, such as the _threading_local.py
module from CPython2.4 which allows to have "global"
(syntactically, they indeed are) variables which
in fact are stored on a per-thread basis. Which in
turn is very useful when you have thread-specific
"background state" information, such as the Request
object in web programming.
|
|
Date |
User |
Action |
Args |
2008-02-20 17:17:30 | admin | link | issue1506749 messages |
2008-02-20 17:17:30 | admin | create | |
|