Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-144386: Update equivalent code for "with", "async with" and "async for"#144472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
serhiy-storchaka wants to merge1 commit intopython:main
base:main
Choose a base branch
Loading
fromserhiy-storchaka:docs-special-methods-lookup
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletionsDoc/reference/compound_stmts.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -544,21 +544,24 @@ The following code::
is semantically equivalent to::

manager = (EXPRESSION)
enter =type(manager).__enter__
exit =type(manager).__exit__
value = enter(manager)
enter = manager.__enter__
exit = manager.__exit__
value = enter()
hit_except = False

try:
TARGET = value
SUITE
except:
hit_except = True
if not exit(manager,*sys.exc_info()):
if not exit(*sys.exc_info()):
raise
finally:
if not hit_except:
exit(manager, None, None, None)
exit(None, None, None)

except that implicit :ref:`special method lookup <special-lookup>` is used
for :meth:`~object.__enter__` and :meth:`~object.__exit__`.

With more than one item, the context managers are processed as if multiple
:keyword:`with` statements were nested::
Expand DownExpand Up@@ -1679,21 +1682,21 @@ The following code::

Is semantically equivalent to::

iter = (ITER)
iter = type(iter).__aiter__(iter)
iter = (ITER).__aiter__()
running = True

while running:
try:
TARGET = awaittype(iter).__anext__(iter)
TARGET = await iter.__anext__()
except StopAsyncIteration:
running = False
else:
SUITE
else:
SUITE2

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

It is a :exc:`SyntaxError` to use an ``async for`` statement outside the
body of a coroutine function.
Expand All@@ -1719,23 +1722,24 @@ The following code::
is semantically equivalent to::

manager = (EXPRESSION)
aenter =type(manager).__aenter__
aexit =type(manager).__aexit__
value = await aenter(manager)
aenter = manager.__aenter__
aexit = manager.__aexit__
value = await aenter()
hit_except = False

try:
TARGET = value
SUITE
except:
hit_except = True
if not await aexit(manager,*sys.exc_info()):
if not await aexit(*sys.exc_info()):
raise
finally:
if not hit_except:
await aexit(manager,None, None, None)
await aexit(None, None, None)

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

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

[8]ページ先頭

©2009-2026 Movatter.jp