New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Context managers written as C types no longer work in Python 2.7 #61470
Comments
We have implemented the context manager API for connections and cursors in our mxODBC module and this works fine in Python 2.6. In Python 2.7 we get the following error: Traceback (most recent call last):
File "context-manager.py", line 6, in <module>
with db.cursor() as cursor:
AttributeError: __exit__ Here's the code snippet: import mx.ODBC.unixODBC as ODBC
connectionString = '...'
db = ODBC.DriverConnect(connectionString)
with db.cursor() as cursor:
print cursor The mxODBC cursor is not an instance, it's implemented as Python type in C. It implements the tp_getattr slot, but not the tp_desc_get slot. Looking at the apparently new API _PyObject_LookupSpecial(), this does not appear to support the tp_getattr slot and goes straight for the tp_desc_get slot. |
After some experiments, it turned out that by simply filling in the tp_methods slot, the problem went away. Still, the change to use _PyObject_LookupSpecial() appears to have missed the (older) use case where you don't define tp_members, but instead implement method lookup as part of the tp_getattr slot. Not sure whether this is worth fixing. I just filed this report so that others running into the same problem can find it. |
The change should also be documented here, http://docs.python.org/2.7/whatsnew/2.7.html#porting-to-python-2-7 |
On 21.02.2013 17:36, Marc-Andre Lemburg wrote:
Sorry: *tp_methods*, not tp_members. |
This is a property of all special methods not just __enter__ and __exit__. |
As 2.7 has reached EOL, I'm going to go ahead and close the issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: