Skip to content
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

Closed
malemburg opened this issue Feb 21, 2013 · 6 comments
Closed

Context managers written as C types no longer work in Python 2.7 #61470

malemburg opened this issue Feb 21, 2013 · 6 comments
Labels
docs Documentation in the Doc dir

Comments

@malemburg
Copy link
Member

BPO 17268
Nosy @malemburg, @tiran, @benjaminp, @zware

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:

assignee = None
closed_at = <Date 2020-04-26.16:57:43.739>
created_at = <Date 2013-02-21.16:31:51.710>
labels = ['docs']
title = 'Context managers written as C types no longer work in Python 2.7'
updated_at = <Date 2020-04-26.16:57:43.737>
user = 'https://github.com/malemburg'

bugs.python.org fields:

activity = <Date 2020-04-26.16:57:43.737>
actor = 'zach.ware'
assignee = 'docs@python'
closed = True
closed_date = <Date 2020-04-26.16:57:43.739>
closer = 'zach.ware'
components = ['Documentation']
creation = <Date 2013-02-21.16:31:51.710>
creator = 'lemburg'
dependencies = []
files = []
hgrepos = []
issue_num = 17268
keywords = []
message_count = 6.0
messages = ['182598', '182599', '182600', '182602', '182608', '367316']
nosy_count = 5.0
nosy_names = ['lemburg', 'christian.heimes', 'benjamin.peterson', 'docs@python', 'zach.ware']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue17268'
versions = ['Python 2.7']

@malemburg
Copy link
Member Author

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.

@malemburg
Copy link
Member Author

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.

@tiran
Copy link
Member

tiran commented Feb 21, 2013

The change should also be documented here, http://docs.python.org/2.7/whatsnew/2.7.html#porting-to-python-2-7

@tiran tiran added the docs Documentation in the Doc dir label Feb 21, 2013
@malemburg
Copy link
Member Author

On 21.02.2013 17:36, Marc-Andre Lemburg wrote:

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.

Sorry: *tp_methods*, not tp_members.

@benjaminp
Copy link
Contributor

This is a property of all special methods not just __enter__ and __exit__.

@zware
Copy link
Member

zware commented Apr 26, 2020

As 2.7 has reached EOL, I'm going to go ahead and close the issue.

@zware zware closed this as completed Apr 26, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

4 participants