New submission from Géry <gery.o...@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 <rep...@bugs.python.org>
<https://bugs.python.org/issue39037>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to