Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.11] Formatting fixes in contextlib docs (GH-98111) #98205

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Changes from all 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
28 changes: 17 additions & 11 deletions Doc/library/contextlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Functions and classes provided:
# Code to release resource, e.g.:
release_resource(resource)

The function can then be used like this::

>>> with managed_resource(timeout=3600) as resource:
... # Resource is released at the end of this block,
... # even if code in the block raises an exception
Expand Down Expand Up @@ -140,9 +142,9 @@ Functions and classes provided:
finally:
print(f'it took {time.monotonic() - now}s to run')

@timeit()
async def main():
# ... async code ...
@timeit()
async def main():
# ... async code ...

When used as a decorator, a new generator instance is implicitly created on
each function call. This allows the otherwise "one-shot" context managers
Expand Down Expand Up @@ -249,15 +251,15 @@ Functions and classes provided:
:ref:`asynchronous context managers <async-context-managers>`::

async def send_http(session=None):
if not session:
# If no http session, create it with aiohttp
cm = aiohttp.ClientSession()
else:
# Caller is responsible for closing the session
cm = nullcontext(session)
if not session:
# If no http session, create it with aiohttp
cm = aiohttp.ClientSession()
else:
# Caller is responsible for closing the session
cm = nullcontext(session)

async with cm as session:
# Send http requests with session
async with cm as session:
# Send http requests with session

.. versionadded:: 3.7

Expand Down Expand Up @@ -396,6 +398,8 @@ Functions and classes provided:
print('Finishing')
return False

The class can then be used like this::

>>> @mycontext()
... def function():
... print('The bit in the middle')
Expand Down Expand Up @@ -466,6 +470,8 @@ Functions and classes provided:
print('Finishing')
return False

The class can then be used like this::

>>> @mycontext()
... async def function():
... print('The bit in the middle')
Expand Down