使用问题 972中讨论的方法,我无法在 psycopg 中修改 cursor() 方法:
尝试使用类型修补 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扩展吗?