Skip to content

Commit 01ad5f6

Browse files
serhiy-storchakamiss-islington
authored andcommitted
gh-144386: Update equivalent code for "with", "async with" and "async for" (GH-144472)
They use special method lookup for special methods. (cherry picked from commit 9e8fa2d) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent a81554f commit 01ad5f6

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
@@ -544,21 +544,24 @@ The following code::
544544
is semantically equivalent to::
545545

546546
manager = (EXPRESSION)
547-
enter = type(manager).__enter__
548-
exit = type(manager).__exit__
549-
value = enter(manager)
547+
enter = manager.__enter__
548+
exit = manager.__exit__
549+
value = enter()
550550
hit_except = False
551551

552552
try:
553553
TARGET = value
554554
SUITE
555555
except:
556556
hit_except = True
557-
if not exit(manager, *sys.exc_info()):
557+
if not exit(*sys.exc_info()):
558558
raise
559559
finally:
560560
if not hit_except:
561-
exit(manager, None, None, None)
561+
exit(None, None, None)
562+
563+
except that implicit :ref:`special method lookup <special-lookup>` is used
564+
for :meth:`~object.__enter__` and :meth:`~object.__exit__`.
562565

563566
With more than one item, the context managers are processed as if multiple
564567
:keyword:`with` statements were nested::
@@ -1563,21 +1566,21 @@ The following code::
15631566

15641567
Is semantically equivalent to::
15651568

1566-
iter = (ITER)
1567-
iter = type(iter).__aiter__(iter)
1569+
iter = (ITER).__aiter__()
15681570
running = True
15691571

15701572
while running:
15711573
try:
1572-
TARGET = await type(iter).__anext__(iter)
1574+
TARGET = await iter.__anext__()
15731575
except StopAsyncIteration:
15741576
running = False
15751577
else:
15761578
SUITE
15771579
else:
15781580
SUITE2
15791581

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

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

16051608
manager = (EXPRESSION)
1606-
aenter = type(manager).__aenter__
1607-
aexit = type(manager).__aexit__
1608-
value = await aenter(manager)
1609+
aenter = manager.__aenter__
1610+
aexit = manager.__aexit__
1611+
value = await aenter()
16091612
hit_except = False
16101613

16111614
try:
16121615
TARGET = value
16131616
SUITE
16141617
except:
16151618
hit_except = True
1616-
if not await aexit(manager, *sys.exc_info()):
1619+
if not await aexit(*sys.exc_info()):
16171620
raise
16181621
finally:
16191622
if not hit_except:
1620-
await aexit(manager, None, None, None)
1623+
await aexit(None, None, None)
16211624

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

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

0 commit comments

Comments
 (0)