3

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?

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.