Skip to content

Commit

Permalink
Formatting fixes in contextlib docs (GH-98111)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3b33c20)

Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com>
  • Loading branch information
miss-islington and slateny authored Oct 12, 2022
1 parent f94e6b4 commit f6abb33
Showing 1 changed file with 17 additions and 11 deletions.
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 @@ -379,6 +381,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 @@ -449,6 +453,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

0 comments on commit f6abb33

Please sign in to comment.