Skip to content

Commit 178721a

Browse files
committed
Replace the else clause with a finally clause
The else clause should instead be a finally clause to also handle the case when there is a non-local goto statement (break, continue, return) in suite (cf. https://stackoverflow.com/questions/59322585/what-is-the-exact-try-statement-equivalent-of-the-with-statement-in-python).
1 parent 94d2c8d commit 178721a

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

Doc/reference/compound_stmts.rst

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -811,23 +811,26 @@ able to suspend execution in its *enter* and *exit* methods.
811811

812812
The following code::
813813

814-
async with EXPR as VAR:
815-
BLOCK
814+
async with expression as target:
815+
suite
816816

817-
Is semantically equivalent to::
817+
is semantically equivalent to::
818818

819-
mgr = (EXPR)
820-
aexit = type(mgr).__aexit__
821-
aenter = type(mgr).__aenter__(mgr)
819+
manager = (expression)
820+
aexit = type(manager).__aexit__
821+
value = type(manager).__aenter__(manager)
822+
target = await value
823+
exception = False
822824

823-
VAR = await aenter
824825
try:
825-
BLOCK
826+
suite
826827
except:
827-
if not await aexit(mgr, *sys.exc_info()):
828+
exception = True
829+
if not await aexit(manager, *sys.exc_info()):
828830
raise
829-
else:
830-
await aexit(mgr, None, None, None)
831+
finally:
832+
if not exception:
833+
await aexit(manager, None, None, None)
831834

832835
See also :meth:`__aenter__` and :meth:`__aexit__` for details.
833836

0 commit comments

Comments
 (0)