문제

Using the method discussed in question 972, I was unable to monkey patch the cursor() method in psycopg:

Tried to patch a methond in psycopg2 with types but it did not work:

>>> 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'
>>> 

Is it because it's a C extension?

도움이 되었습니까?

해결책

Correct. Types defined in C cannot have arbitrary attributes added.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top