views:

38

answers:

1

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?

    ======================================================================
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()
+3  A: 

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>. That means your test if self.fset.__name__ == '<lambda>' or not self.fset.__name__: will take the wrong branch.

Try:

if self.fset.__name__.startswith('<lambda') or not self.fset.__name__:

and so on.

Duncan
Thanks Duncan that makes complete sense, just modified the code and the tests work now. Cheers. Jon.
jonmiddleton