Skip to content

Commit cbecfbe

Browse files
[3.13] gh-144386: Update equivalent code for "with", "async with" and "async for" (GH-144472) (GH-144946)
They use special method lookup for special methods. (cherry picked from commit 9e8fa2d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent ea07223 commit cbecfbe

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

Doc/reference/compound_stmts.rst

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -537,21 +537,24 @@ The following code::
537537
is semantically equivalent to::
538538

539539
manager = (EXPRESSION)
540-
enter = type(manager).__enter__
541-
exit = type(manager).__exit__
542-
value = enter(manager)
540+
enter = manager.__enter__
541+
exit = manager.__exit__
542+
value = enter()
543543
hit_except = False
544544

545545
try:
546546
TARGET = value
547547
SUITE
548548
except:
549549
hit_except = True
550-
if not exit(manager, *sys.exc_info()):
550+
if not exit(*sys.exc_info()):
551551
raise
552552
finally:
553553
if not hit_except:
554-
exit(manager, None, None, None)
554+
exit(None, None, None)
555+
556+
except that implicit :ref:`special method lookup <special-lookup>` is used
557+
for :meth:`~object.__enter__` and :meth:`~object.__exit__`.
555558

556559
With more than one item, the context managers are processed as if multiple
557560
:keyword:`with` statements were nested::
@@ -1562,21 +1565,21 @@ The following code::
15621565

15631566
Is semantically equivalent to::
15641567

1565-
iter = (ITER)
1566-
iter = type(iter).__aiter__(iter)
1568+
iter = (ITER).__aiter__()
15671569
running = True
15681570

15691571
while running:
15701572
try:
1571-
TARGET = await type(iter).__anext__(iter)
1573+
TARGET = await iter.__anext__()
15721574
except StopAsyncIteration:
15731575
running = False
15741576
else:
15751577
SUITE
15761578
else:
15771579
SUITE2
15781580

1579-
See also :meth:`~object.__aiter__` and :meth:`~object.__anext__` for details.
1581+
except that implicit :ref:`special method lookup <special-lookup>` is used
1582+
for :meth:`~object.__aiter__` and :meth:`~object.__anext__`.
15801583

15811584
It is a :exc:`SyntaxError` to use an ``async for`` statement outside the
15821585
body of a coroutine function.
@@ -1602,23 +1605,24 @@ The following code::
16021605
is semantically equivalent to::
16031606

16041607
manager = (EXPRESSION)
1605-
aenter = type(manager).__aenter__
1606-
aexit = type(manager).__aexit__
1607-
value = await aenter(manager)
1608+
aenter = manager.__aenter__
1609+
aexit = manager.__aexit__
1610+
value = await aenter()
16081611
hit_except = False
16091612

16101613
try:
16111614
TARGET = value
16121615
SUITE
16131616
except:
16141617
hit_except = True
1615-
if not await aexit(manager, *sys.exc_info()):
1618+
if not await aexit(*sys.exc_info()):
16161619
raise
16171620
finally:
16181621
if not hit_except:
1619-
await aexit(manager, None, None, None)
1622+
await aexit(None, None, None)
16201623

1621-
See also :meth:`~object.__aenter__` and :meth:`~object.__aexit__` for details.
1624+
except that implicit :ref:`special method lookup <special-lookup>` is used
1625+
for :meth:`~object.__aenter__` and :meth:`~object.__aexit__`.
16221626

16231627
It is a :exc:`SyntaxError` to use an ``async with`` statement outside the
16241628
body of a coroutine function.

0 commit comments

Comments
 (0)