簡體   English   中英

IronPython中的自動屬性失敗可在Python中運行嗎?

[英]Autoproperty failing in IronPython works in Python?

我有以下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)

這是代碼

#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()

僅查看您的代碼和回溯,在我看來,IronPython上的lambda就有一個名稱,例如<lambda$48>而不是<lambda> 這意味着您要測試if self.fset.__name__ == '<lambda>' or not self.fset.__name__:進行錯誤的分支。

嘗試:

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

等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM