[英]Autoproperty failing in IronPython works in Python?
I have this following python code, it works fine in python but fails with the following error in IronPython 2.6 any ideas as to why? 我有以下python代码,它在python中可以正常工作,但在IronPython 2.6中由于以下错误而失败,关于为什么的任何想法?
======================================================================
ERROR: testAutoProp (__main__.testProperty)
----------------------------------------------------------------------
Traceback (most recent call last):
File "oproperty.py", line 66, in testAutoProp
a.x = 200
File "oproperty.py", line 31, in __set__
getattr(obj, self.fset.__name__)(value)
AttributeError: 'A' object has no attribute '<lambda$48>'
----------------------------------------------------------------------
Ran 1 test in 0.234s
FAILED (errors=1)
Here is the code 这是代码
#provides an overridable version of the property keyword
import unittest
#provides an overridable version of the property keyword
class OProperty(object):
"""Based on the emulation of PyProperty_Type() in Objects/descrobject.c"""
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
self.fget = fget
self.fset = fset
self.fdel = fdel
self.__doc__ = doc
def __get__(self, obj, objtype=None):
if obj is None:
return self
if self.fget is None:
raise AttributeError, "unreadable attribute"
if self.fget.__name__ == '<lambda>' or not self.fget.__name__:
return self.fget(obj)
else:
return getattr(obj, self.fget.__name__)()
def __set__(self, obj, value):
if self.fset is None:
raise AttributeError, "can't set attribute"
if self.fset.__name__ == '<lambda>' or not self.fset.__name__:
self.fset(obj, value)
else:
getattr(obj, self.fset.__name__)(value)
def __delete__(self, obj):
if self.fdel is None:
raise AttributeError, "can't delete attribute"
if self.fdel.__name__ == '<lambda>' or not self.fdel.__name__:
self.fdel(obj)
else:
getattr(obj, self.fdel.__name__)()
def autoProperty( attrname, desc ):
"Try to auto gen getters and setting for any property type"
getFn = lambda self: getattr( self, attrname )
setFn = lambda self, v: setattr( self, attrname, v )
#set the corresponding function in the descriptor
desc.fget = getFn
desc.fset = setFn
#if there is a blank colname let X property sort it out
#if hasattr( desc, "colname") and desc.colname == "":
# i = 0
# while attrname[ i ] == "_":
# i = i + 1
# desc.colname = attrname[i:]
return desc
class testProperty(unittest.TestCase):
def testAutoProp(self):
class A(object):
def __init__(self):
self._x = 50
x = autoProperty( "_x", OProperty() )
a = A()
a.x = 200
if __name__ == "__main__":
unittest.main()
Just looking at your code and traceback, it looks to me as though lambdas on IronPython have a name such as <lambda$48>
instead of just <lambda>
. 仅查看您的代码和回溯,在我看来,IronPython上的lambda就有一个名称,例如<lambda$48>
而不是<lambda>
。 That means your test if self.fset.__name__ == '<lambda>' or not self.fset.__name__:
will take the wrong branch. 这意味着您要测试if self.fset.__name__ == '<lambda>' or not self.fset.__name__:
进行错误的分支。
Try: 尝试:
if self.fset.__name__.startswith('<lambda') or not self.fset.__name__:
and so on. 等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.