【发布时间】: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