This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: with statement looks up __exit__ incorrectly
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: benjamin.peterson Nosy List: benjamin.peterson, benrg, eric.araujo, georg.brandl
Priority: normal Keywords:

Created on 2011-03-07 07:19 by benrg, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg130231 - (view) Author: (benrg) Date: 2011-03-07 07:19
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.
msg130235 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2011-03-07 07:35
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.
msg130241 - (view) Author: (benrg) Date: 2011-03-07 08:35
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.
msg130257 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2011-03-07 14:13
Well, the pep is wrong, too.
msg130615 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-03-11 21:27
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.
History
Date User Action Args
2022-04-11 14:57:14adminsetgithub: 55637
2011-03-11 21:27:25eric.araujosetnosy: + eric.araujo
messages: + msg130615
2011-03-07 14:13:53benjamin.petersonsetnosy: georg.brandl, benjamin.peterson, benrg
messages: + msg130257
2011-03-07 08:43:29georg.brandlsetassignee: benjamin.peterson

nosy: + benjamin.peterson
2011-03-07 08:35:45benrgsetmessages: + msg130241
2011-03-07 07:35:02georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg130235

resolution: not a bug
2011-03-07 07:19:16benrgcreate