【问题标题】:Monkey patching C extension in Python猴子修补 Python 中的 C 扩展
【发布时间】:2012-03-27 05:40:24
【问题描述】:

使用question 972 中讨论的方法,我无法在 psycopg 中修改 cursor() 方法:

尝试使用 types 修补 psycopg2 中的方法,但没有成功:

>>> import psycopg2, types
import psycopg2, types
>>> db = psycopg2.connect('dbname=foo')
db = psycopg2.connect('dbname=foo')
>>> def mycursor(self):
def mycursor(self):
...     db.rollback()
    db.rollback()
...     return self.cursor()
    return self.cursor()
... 

>>> db.mycursor = types.MethodType(mycursor, db)
db.mycursor = types.MethodType(mycursor, db)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'psycopg2._psycopg.connection' object has no attribute 'mycursor'
>>> 

是因为它是 C 扩展吗?

【问题讨论】:

    标签: python psycopg2 monkeypatching python-c-extension python-extensions


    【解决方案1】:

    正确。 C 中定义的类型不能添加任意属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 2011-03-20
      • 2017-02-12
      • 1970-01-01
      • 2012-03-13
      • 2013-12-26
      • 2012-06-14
      相关资源
      最近更新 更多