[New-bugs-announce] [issue39037] Wrong trial order of __exit__ and __enter__ in the with statement

Géry report at bugs.python.org
Fri Dec 13 10:19:37 EST 2019


New submission from Géry <gery.ogam at gmail.com>:

>>> class A: pass
    ... 
    >>> with A(): pass
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: __enter__

I expected `AttributeError: __exit__`, since PEP 343 states (https://www.python.org/dev/peps/pep-0343/#specification-the-with-statement):

> The details of the above translation are intended to prescribe the exact semantics. If either of the relevant methods are not found as expected, the interpreter will raise AttributeError, in the order that they are tried (__exit__, __enter__).

and the language documentation states (https://docs.python.org/3/reference/compound_stmts.html#the-with-statement):

> The execution of the with statement with one “item” proceeds as follows:
> 1. The context expression (the expression given in the with_item) is evaluated to obtain a context manager.
> 2. The context manager’s __exit__() is loaded for later use.
> 3. The context manager’s __enter__() method is invoked.

----------
components: Interpreter Core
messages: 358333
nosy: gvanrossum, maggyero, ncoghlan
priority: normal
severity: normal
status: open
title: Wrong trial order of __exit__ and __enter__ in the with statement
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39037>
_______________________________________


More information about the New-bugs-announce mailing list