Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top