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

with statement looks up __exit__ incorrectly #55637

Closed
benrg mannequin opened this issue Mar 7, 2011 · 5 comments
Closed

with statement looks up __exit__ incorrectly #55637

benrg mannequin opened this issue Mar 7, 2011 · 5 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@benrg
Copy link
Mannequin

benrg mannequin commented Mar 7, 2011

BPO 11428
Nosy @birkenfeld, @benjaminp, @merwok

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 = 'https://github.com/benjaminp'
closed_at = <Date 2011-03-07.07:35:02.078>
created_at = <Date 2011-03-07.07:19:16.528>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'with statement looks up __exit__ incorrectly'
updated_at = <Date 2011-03-11.21:27:25.767>
user = 'https://bugs.python.org/benrg'

bugs.python.org fields:

activity = <Date 2011-03-11.21:27:25.767>
actor = 'eric.araujo'
assignee = 'benjamin.peterson'
closed = True
closed_date = <Date 2011-03-07.07:35:02.078>
closer = 'georg.brandl'
components = ['Interpreter Core']
creation = <Date 2011-03-07.07:19:16.528>
creator = 'benrg'
dependencies = []
files = []
hgrepos = []
issue_num = 11428
keywords = []
message_count = 5.0
messages = ['130231', '130235', '130241', '130257', '130615']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'benjamin.peterson', 'eric.araujo', 'benrg']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue11428'
versions = ['Python 3.2']

@benrg
Copy link
Mannequin Author

benrg mannequin commented Mar 7, 2011

class MakeContextHandler:
  def __init__(self, enter, exit):
    self.__enter__ = enter
    self.__exit__ = exit

with MakeContextHandler(lambda: None, lambda *e: None): pass

In 3.1.3 this worked; in 3.2 it raises AttributeError('__exit__'), which appears to be a bug.

@benrg benrg mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Mar 7, 2011
@birkenfeld
Copy link
Member

The bug is actually in 3.1 and fixed in 3.2: special methods (those with __underscore__ names) are supposed to be looked up on the class, not the instance.

@benrg
Copy link
Mannequin Author

benrg mannequin commented Mar 7, 2011

But when I translate my example according to PEP-343, it works (i.e., doesn't raise an exception) in 3.2, and PEP-343 says "[t]he details of the above translation are intended to prescribe the exact semantics." So I think that at least one of PEP-343, the evaluation of mgr.__exit__, or the evaluation of with mgr: pass must be broken, though I'm no longer sure which.

@benjaminp
Copy link
Contributor

Well, the pep is wrong, too.

@merwok
Copy link
Member

merwok commented Mar 11, 2011

After a quick perusal of the PEP, there are no wrong definitions of context managers (IOW they define methods in a class, not attribute on an object), but the pseudo-code explaining how the statement works use things like “mgr.__enter__” instead of “type(mgr).__enter__(mgr)”, which might be misleading.

@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
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants