sgzwiz / brython

Automatically exported from code.google.com/p/brython
BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

Setting an attribute with setattr on an object() passed as an argument fails #22

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In console:

def setx(o, v):
   print( "class after", o.__class__)
   setattr(o, "x", v)

o = object()
setattr(o, "x", 1)
print("direct setattr", o.x)

o = object()
print("class before", o.__class__)
setx(o, 1)
print("indirect setattr", o.x)

Reports:

direct setattr 1
class before <undefined>
class after $DomWrapper
AttributeError: object has no attribute x
Line 12
print("indirect setattr", o.x)
<done in 9 ms>

Temporary workaround:
When o.__class__ is "$DowWrapper"
    replace setattr(o, key, value)
with
    o.__setattr__(key, value)

Original issue reported on code.google.com by pedro.ro...@gmail.com on 30 Dec 2012 at 7:14

GoogleCodeExporter commented 9 years ago
Fixed by revision 273
When arguments are passed to a function they are transformed using $JS2Py in 
py_utils.js (this is necessary to handle callbacks from DOM events). If attemps 
to detect the arguments class fails, they are wrapped into a $DomWrapper. 
Setting a value to the __class__ attribute of $ObjectClass fixes the bug

Original comment by pierre.q...@gmail.com on 30 Dec 2012 at 9:26