mirror of
https://github.com/python/cpython.git
synced 2026-04-14 07:41:00 +00:00
[3.14] Hide "object" prefix on dunders in contextlib docs & selectivly link some more (GH-145436) (#145443)
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
This commit is contained in:
parent
26a0dbad63
commit
3c19c88fa9
1 changed files with 12 additions and 11 deletions
|
|
@ -21,9 +21,9 @@ Functions and classes provided:
|
|||
.. class:: AbstractContextManager
|
||||
|
||||
An :term:`abstract base class` for classes that implement
|
||||
:meth:`object.__enter__` and :meth:`object.__exit__`. A default
|
||||
implementation for :meth:`object.__enter__` is provided which returns
|
||||
``self`` while :meth:`object.__exit__` is an abstract method which by default
|
||||
:meth:`~object.__enter__` and :meth:`~object.__exit__`. A default
|
||||
implementation for :meth:`~object.__enter__` is provided which returns
|
||||
``self`` while :meth:`~object.__exit__` is an abstract method which by default
|
||||
returns ``None``. See also the definition of :ref:`typecontextmanager`.
|
||||
|
||||
.. versionadded:: 3.6
|
||||
|
|
@ -32,9 +32,9 @@ Functions and classes provided:
|
|||
.. class:: AbstractAsyncContextManager
|
||||
|
||||
An :term:`abstract base class` for classes that implement
|
||||
:meth:`object.__aenter__` and :meth:`object.__aexit__`. A default
|
||||
implementation for :meth:`object.__aenter__` is provided which returns
|
||||
``self`` while :meth:`object.__aexit__` is an abstract method which by default
|
||||
:meth:`~object.__aenter__` and :meth:`~object.__aexit__`. A default
|
||||
implementation for :meth:`~object.__aenter__` is provided which returns
|
||||
``self`` while :meth:`~object.__aexit__` is an abstract method which by default
|
||||
returns ``None``. See also the definition of
|
||||
:ref:`async-context-managers`.
|
||||
|
||||
|
|
@ -228,7 +228,7 @@ Functions and classes provided:
|
|||
|
||||
.. function:: nullcontext(enter_result=None)
|
||||
|
||||
Return a context manager that returns *enter_result* from ``__enter__``, but
|
||||
Return a context manager that returns *enter_result* from :meth:`~object.__enter__`, but
|
||||
otherwise does nothing. It is intended to be used as a stand-in for an
|
||||
optional context manager, for example::
|
||||
|
||||
|
|
@ -335,7 +335,7 @@ Functions and classes provided:
|
|||
For example, the output of :func:`help` normally is sent to *sys.stdout*.
|
||||
You can capture that output in a string by redirecting the output to an
|
||||
:class:`io.StringIO` object. The replacement stream is returned from the
|
||||
``__enter__`` method and so is available as the target of the
|
||||
:meth:`~object.__enter__` method and so is available as the target of the
|
||||
:keyword:`with` statement::
|
||||
|
||||
with redirect_stdout(io.StringIO()) as f:
|
||||
|
|
@ -396,7 +396,8 @@ Functions and classes provided:
|
|||
A base class that enables a context manager to also be used as a decorator.
|
||||
|
||||
Context managers inheriting from ``ContextDecorator`` have to implement
|
||||
``__enter__`` and ``__exit__`` as normal. ``__exit__`` retains its optional
|
||||
:meth:`~object.__enter__` and :meth:`~object.__exit__` as normal.
|
||||
``__exit__`` retains its optional
|
||||
exception handling even when used as a decorator.
|
||||
|
||||
``ContextDecorator`` is used by :func:`contextmanager`, so you get this
|
||||
|
|
@ -697,9 +698,9 @@ context management protocol.
|
|||
Catching exceptions from ``__enter__`` methods
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
It is occasionally desirable to catch exceptions from an ``__enter__``
|
||||
It is occasionally desirable to catch exceptions from an :meth:`~object.__enter__`
|
||||
method implementation, *without* inadvertently catching exceptions from
|
||||
the :keyword:`with` statement body or the context manager's ``__exit__``
|
||||
the :keyword:`with` statement body or the context manager's :meth:`~object.__exit__`
|
||||
method. By using :class:`ExitStack` the steps in the context management
|
||||
protocol can be separated slightly in order to allow this::
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue