Skip to content

Commit 63acf78

Browse files
authored
GH-101100: Fix reference warnings for __enter__ and __exit__ (#110112)
1 parent da99133 commit 63acf78

11 files changed

+58
-58
lines changed

Doc/glossary.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ Glossary
239239

240240
context manager
241241
An object which controls the environment seen in a :keyword:`with`
242-
statement by defining :meth:`__enter__` and :meth:`__exit__` methods.
242+
statement by defining :meth:`~object.__enter__` and :meth:`~object.__exit__` methods.
243243
See :pep:`343`.
244244

245245
context variable

Doc/library/contextlib.rst

+11-11
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Functions and classes provided:
4545

4646
This function is a :term:`decorator` that can be used to define a factory
4747
function for :keyword:`with` statement context managers, without needing to
48-
create a class or separate :meth:`__enter__` and :meth:`__exit__` methods.
48+
create a class or separate :meth:`~object.__enter__` and :meth:`~object.__exit__` methods.
4949

5050
While many objects natively support use in with statements, sometimes a
5151
resource needs to be managed that isn't a context manager in its own right,
@@ -515,7 +515,7 @@ Functions and classes provided:
515515
# the with statement, even if attempts to open files later
516516
# in the list raise an exception
517517

518-
The :meth:`__enter__` method returns the :class:`ExitStack` instance, and
518+
The :meth:`~object.__enter__` method returns the :class:`ExitStack` instance, and
519519
performs no additional operations.
520520

521521
Each instance maintains a stack of registered callbacks that are called in
@@ -543,9 +543,9 @@ Functions and classes provided:
543543

544544
.. method:: enter_context(cm)
545545

546-
Enters a new context manager and adds its :meth:`__exit__` method to
546+
Enters a new context manager and adds its :meth:`~object.__exit__` method to
547547
the callback stack. The return value is the result of the context
548-
manager's own :meth:`__enter__` method.
548+
manager's own :meth:`~object.__enter__` method.
549549

550550
These context managers may suppress exceptions just as they normally
551551
would if used directly as part of a :keyword:`with` statement.
@@ -556,18 +556,18 @@ Functions and classes provided:
556556

557557
.. method:: push(exit)
558558

559-
Adds a context manager's :meth:`__exit__` method to the callback stack.
559+
Adds a context manager's :meth:`~object.__exit__` method to the callback stack.
560560

561561
As ``__enter__`` is *not* invoked, this method can be used to cover
562-
part of an :meth:`__enter__` implementation with a context manager's own
563-
:meth:`__exit__` method.
562+
part of an :meth:`~object.__enter__` implementation with a context manager's own
563+
:meth:`~object.__exit__` method.
564564

565565
If passed an object that is not a context manager, this method assumes
566566
it is a callback with the same signature as a context manager's
567-
:meth:`__exit__` method and adds it directly to the callback stack.
567+
:meth:`~object.__exit__` method and adds it directly to the callback stack.
568568

569569
By returning true values, these callbacks can suppress exceptions the
570-
same way context manager :meth:`__exit__` methods can.
570+
same way context manager :meth:`~object.__exit__` methods can.
571571

572572
The passed in object is returned from the function, allowing this
573573
method to be used as a function decorator.
@@ -714,7 +714,7 @@ Cleaning up in an ``__enter__`` implementation
714714

715715
As noted in the documentation of :meth:`ExitStack.push`, this
716716
method can be useful in cleaning up an already allocated resource if later
717-
steps in the :meth:`__enter__` implementation fail.
717+
steps in the :meth:`~object.__enter__` implementation fail.
718718

719719
Here's an example of doing this for a context manager that accepts resource
720720
acquisition and release functions, along with an optional validation function,
@@ -871,7 +871,7 @@ And also as a function decorator::
871871

872872
Note that there is one additional limitation when using context managers
873873
as function decorators: there's no way to access the return value of
874-
:meth:`__enter__`. If that value is needed, then it is still necessary to use
874+
:meth:`~object.__enter__`. If that value is needed, then it is still necessary to use
875875
an explicit ``with`` statement.
876876

877877
.. seealso::

Doc/library/stdtypes.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -4857,7 +4857,7 @@ before the statement body is executed and exited when the statement ends:
48574857
The exception passed in should never be reraised explicitly - instead, this
48584858
method should return a false value to indicate that the method completed
48594859
successfully and does not want to suppress the raised exception. This allows
4860-
context management code to easily detect whether or not an :meth:`__exit__`
4860+
context management code to easily detect whether or not an :meth:`~object.__exit__`
48614861
method has actually failed.
48624862

48634863
Python defines several context managers to support easy thread synchronisation,

Doc/library/test.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ The :mod:`test.support` module defines the following classes:
10431043
:const:`resource.RLIMIT_CORE`'s soft limit to 0 to prevent coredump file
10441044
creation.
10451045

1046-
On both platforms, the old value is restored by :meth:`__exit__`.
1046+
On both platforms, the old value is restored by :meth:`~object.__exit__`.
10471047

10481048

10491049
.. class:: SaveSignals()

Doc/library/unittest.mock.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -2531,8 +2531,8 @@ are closed properly and is becoming common::
25312531
f.write('something')
25322532

25332533
The issue is that even if you mock out the call to :func:`open` it is the
2534-
*returned object* that is used as a context manager (and has :meth:`__enter__` and
2535-
:meth:`__exit__` called).
2534+
*returned object* that is used as a context manager (and has :meth:`~object.__enter__` and
2535+
:meth:`~object.__exit__` called).
25362536

25372537
Mocking context managers with a :class:`MagicMock` is common enough and fiddly
25382538
enough that a helper function is useful. ::

Doc/reference/compound_stmts.rst

+10-10
Original file line numberDiff line numberDiff line change
@@ -489,37 +489,37 @@ The execution of the :keyword:`with` statement with one "item" proceeds as follo
489489
#. The context expression (the expression given in the
490490
:token:`~python-grammar:with_item`) is evaluated to obtain a context manager.
491491

492-
#. The context manager's :meth:`__enter__` is loaded for later use.
492+
#. The context manager's :meth:`~object.__enter__` is loaded for later use.
493493

494-
#. The context manager's :meth:`__exit__` is loaded for later use.
494+
#. The context manager's :meth:`~object.__exit__` is loaded for later use.
495495

496-
#. The context manager's :meth:`__enter__` method is invoked.
496+
#. The context manager's :meth:`~object.__enter__` method is invoked.
497497

498498
#. If a target was included in the :keyword:`with` statement, the return value
499-
from :meth:`__enter__` is assigned to it.
499+
from :meth:`~object.__enter__` is assigned to it.
500500

501501
.. note::
502502

503-
The :keyword:`with` statement guarantees that if the :meth:`__enter__`
504-
method returns without an error, then :meth:`__exit__` will always be
503+
The :keyword:`with` statement guarantees that if the :meth:`~object.__enter__`
504+
method returns without an error, then :meth:`~object.__exit__` will always be
505505
called. Thus, if an error occurs during the assignment to the target list,
506506
it will be treated the same as an error occurring within the suite would
507507
be. See step 7 below.
508508

509509
#. The suite is executed.
510510

511-
#. The context manager's :meth:`__exit__` method is invoked. If an exception
511+
#. The context manager's :meth:`~object.__exit__` method is invoked. If an exception
512512
caused the suite to be exited, its type, value, and traceback are passed as
513-
arguments to :meth:`__exit__`. Otherwise, three :const:`None` arguments are
513+
arguments to :meth:`~object.__exit__`. Otherwise, three :const:`None` arguments are
514514
supplied.
515515

516516
If the suite was exited due to an exception, and the return value from the
517-
:meth:`__exit__` method was false, the exception is reraised. If the return
517+
:meth:`~object.__exit__` method was false, the exception is reraised. If the return
518518
value was true, the exception is suppressed, and execution continues with the
519519
statement following the :keyword:`with` statement.
520520

521521
If the suite was exited for any reason other than an exception, the return
522-
value from :meth:`__exit__` is ignored, and execution proceeds at the normal
522+
value from :meth:`~object.__exit__` is ignored, and execution proceeds at the normal
523523
location for the kind of exit that was taken.
524524

525525
The following code::

Doc/reference/datamodel.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -2939,7 +2939,7 @@ For more information on context managers, see :ref:`typecontextmanager`.
29392939
(i.e., prevent it from being propagated), it should return a true value.
29402940
Otherwise, the exception will be processed normally upon exit from this method.
29412941

2942-
Note that :meth:`__exit__` methods should not reraise the passed-in exception;
2942+
Note that :meth:`~object.__exit__` methods should not reraise the passed-in exception;
29432943
this is the caller's responsibility.
29442944

29452945

@@ -3257,12 +3257,12 @@ Asynchronous context managers can be used in an :keyword:`async with` statement.
32573257

32583258
.. method:: object.__aenter__(self)
32593259

3260-
Semantically similar to :meth:`__enter__`, the only
3260+
Semantically similar to :meth:`~object.__enter__`, the only
32613261
difference being that it must return an *awaitable*.
32623262

32633263
.. method:: object.__aexit__(self, exc_type, exc_value, traceback)
32643264

3265-
Semantically similar to :meth:`__exit__`, the only
3265+
Semantically similar to :meth:`~object.__exit__`, the only
32663266
difference being that it must return an *awaitable*.
32673267

32683268
An example of an asynchronous context manager class::

Doc/whatsnew/2.5.rst

+11-11
Original file line numberDiff line numberDiff line change
@@ -575,15 +575,15 @@ structure is::
575575
with-block
576576

577577
The expression is evaluated, and it should result in an object that supports the
578-
context management protocol (that is, has :meth:`__enter__` and :meth:`__exit__`
578+
context management protocol (that is, has :meth:`~object.__enter__` and :meth:`~object.__exit__`
579579
methods.
580580

581-
The object's :meth:`__enter__` is called before *with-block* is executed and
581+
The object's :meth:`~object.__enter__` is called before *with-block* is executed and
582582
therefore can run set-up code. It also may return a value that is bound to the
583583
name *variable*, if given. (Note carefully that *variable* is *not* assigned
584584
the result of *expression*.)
585585

586-
After execution of the *with-block* is finished, the object's :meth:`__exit__`
586+
After execution of the *with-block* is finished, the object's :meth:`~object.__exit__`
587587
method is called, even if the block raised an exception, and can therefore run
588588
clean-up code.
589589

@@ -609,7 +609,7 @@ part-way through the block.
609609
.. note::
610610

611611
In this case, *f* is the same object created by :func:`open`, because
612-
:meth:`file.__enter__` returns *self*.
612+
:meth:`~object.__enter__` returns *self*.
613613

614614
The :mod:`threading` module's locks and condition variables also support the
615615
':keyword:`with`' statement::
@@ -652,10 +652,10 @@ underlying implementation and should keep reading.
652652
A high-level explanation of the context management protocol is:
653653

654654
* The expression is evaluated and should result in an object called a "context
655-
manager". The context manager must have :meth:`__enter__` and :meth:`__exit__`
655+
manager". The context manager must have :meth:`~object.__enter__` and :meth:`~object.__exit__`
656656
methods.
657657

658-
* The context manager's :meth:`__enter__` method is called. The value returned
658+
* The context manager's :meth:`~object.__enter__` method is called. The value returned
659659
is assigned to *VAR*. If no ``'as VAR'`` clause is present, the value is simply
660660
discarded.
661661

@@ -669,7 +669,7 @@ A high-level explanation of the context management protocol is:
669669
if you do the author of the code containing the ':keyword:`with`' statement will
670670
never realize anything went wrong.
671671

672-
* If *BLOCK* didn't raise an exception, the :meth:`__exit__` method is still
672+
* If *BLOCK* didn't raise an exception, the :meth:`~object.__exit__` method is still
673673
called, but *type*, *value*, and *traceback* are all ``None``.
674674

675675
Let's think through an example. I won't present detailed code but will only
@@ -703,7 +703,7 @@ rolled back if there's an exception. Here's the basic interface for
703703
def rollback (self):
704704
"Rolls back current transaction"
705705

706-
The :meth:`__enter__` method is pretty easy, having only to start a new
706+
The :meth:`~object.__enter__` method is pretty easy, having only to start a new
707707
transaction. For this application the resulting cursor object would be a useful
708708
result, so the method will return it. The user can then add ``as cursor`` to
709709
their ':keyword:`with`' statement to bind the cursor to a variable name. ::
@@ -715,7 +715,7 @@ their ':keyword:`with`' statement to bind the cursor to a variable name. ::
715715
cursor = self.cursor()
716716
return cursor
717717

718-
The :meth:`__exit__` method is the most complicated because it's where most of
718+
The :meth:`~object.__exit__` method is the most complicated because it's where most of
719719
the work has to be done. The method has to check if an exception occurred. If
720720
there was no exception, the transaction is committed. The transaction is rolled
721721
back if there was an exception.
@@ -748,10 +748,10 @@ are useful for writing objects for use with the ':keyword:`with`' statement.
748748
The decorator is called :func:`contextmanager`, and lets you write a single
749749
generator function instead of defining a new class. The generator should yield
750750
exactly one value. The code up to the :keyword:`yield` will be executed as the
751-
:meth:`__enter__` method, and the value yielded will be the method's return
751+
:meth:`~object.__enter__` method, and the value yielded will be the method's return
752752
value that will get bound to the variable in the ':keyword:`with`' statement's
753753
:keyword:`!as` clause, if any. The code after the :keyword:`yield` will be
754-
executed in the :meth:`__exit__` method. Any exception raised in the block will
754+
executed in the :meth:`~object.__exit__` method. Any exception raised in the block will
755755
be raised by the :keyword:`!yield` statement.
756756

757757
Our database example from the previous section could be written using this

Doc/whatsnew/2.6.rst

+13-13
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,15 @@ structure is::
269269
with-block
270270

271271
The expression is evaluated, and it should result in an object that supports the
272-
context management protocol (that is, has :meth:`__enter__` and :meth:`__exit__`
272+
context management protocol (that is, has :meth:`~object.__enter__` and :meth:`~object.__exit__`
273273
methods).
274274

275-
The object's :meth:`__enter__` is called before *with-block* is executed and
275+
The object's :meth:`~object.__enter__` is called before *with-block* is executed and
276276
therefore can run set-up code. It also may return a value that is bound to the
277277
name *variable*, if given. (Note carefully that *variable* is *not* assigned
278278
the result of *expression*.)
279279

280-
After execution of the *with-block* is finished, the object's :meth:`__exit__`
280+
After execution of the *with-block* is finished, the object's :meth:`~object.__exit__`
281281
method is called, even if the block raised an exception, and can therefore run
282282
clean-up code.
283283

@@ -296,7 +296,7 @@ part-way through the block.
296296
.. note::
297297

298298
In this case, *f* is the same object created by :func:`open`, because
299-
:meth:`file.__enter__` returns *self*.
299+
:meth:`~object.__enter__` returns *self*.
300300

301301
The :mod:`threading` module's locks and condition variables also support the
302302
':keyword:`with`' statement::
@@ -339,16 +339,16 @@ underlying implementation and should keep reading.
339339
A high-level explanation of the context management protocol is:
340340

341341
* The expression is evaluated and should result in an object called a "context
342-
manager". The context manager must have :meth:`__enter__` and :meth:`__exit__`
342+
manager". The context manager must have :meth:`~object.__enter__` and :meth:`~object.__exit__`
343343
methods.
344344

345-
* The context manager's :meth:`__enter__` method is called. The value returned
345+
* The context manager's :meth:`~object.__enter__` method is called. The value returned
346346
is assigned to *VAR*. If no ``as VAR`` clause is present, the value is simply
347347
discarded.
348348

349349
* The code in *BLOCK* is executed.
350350

351-
* If *BLOCK* raises an exception, the context manager's :meth:`__exit__` method
351+
* If *BLOCK* raises an exception, the context manager's :meth:`~object.__exit__` method
352352
is called with three arguments, the exception details (``type, value, traceback``,
353353
the same values returned by :func:`sys.exc_info`, which can also be ``None``
354354
if no exception occurred). The method's return value controls whether an exception
@@ -357,7 +357,7 @@ A high-level explanation of the context management protocol is:
357357
if you do the author of the code containing the ':keyword:`with`' statement will
358358
never realize anything went wrong.
359359

360-
* If *BLOCK* didn't raise an exception, the :meth:`__exit__` method is still
360+
* If *BLOCK* didn't raise an exception, the :meth:`~object.__exit__` method is still
361361
called, but *type*, *value*, and *traceback* are all ``None``.
362362

363363
Let's think through an example. I won't present detailed code but will only
@@ -391,7 +391,7 @@ rolled back if there's an exception. Here's the basic interface for
391391
def rollback(self):
392392
"Rolls back current transaction"
393393

394-
The :meth:`__enter__` method is pretty easy, having only to start a new
394+
The :meth:`~object.__enter__` method is pretty easy, having only to start a new
395395
transaction. For this application the resulting cursor object would be a useful
396396
result, so the method will return it. The user can then add ``as cursor`` to
397397
their ':keyword:`with`' statement to bind the cursor to a variable name. ::
@@ -403,7 +403,7 @@ their ':keyword:`with`' statement to bind the cursor to a variable name. ::
403403
cursor = self.cursor()
404404
return cursor
405405

406-
The :meth:`__exit__` method is the most complicated because it's where most of
406+
The :meth:`~object.__exit__` method is the most complicated because it's where most of
407407
the work has to be done. The method has to check if an exception occurred. If
408408
there was no exception, the transaction is committed. The transaction is rolled
409409
back if there was an exception.
@@ -436,10 +436,10 @@ are useful when writing objects for use with the ':keyword:`with`' statement.
436436
The decorator is called :func:`contextmanager`, and lets you write a single
437437
generator function instead of defining a new class. The generator should yield
438438
exactly one value. The code up to the :keyword:`yield` will be executed as the
439-
:meth:`__enter__` method, and the value yielded will be the method's return
439+
:meth:`~object.__enter__` method, and the value yielded will be the method's return
440440
value that will get bound to the variable in the ':keyword:`with`' statement's
441441
:keyword:`!as` clause, if any. The code after the :keyword:`!yield` will be
442-
executed in the :meth:`__exit__` method. Any exception raised in the block will
442+
executed in the :meth:`~object.__exit__` method. Any exception raised in the block will
443443
be raised by the :keyword:`!yield` statement.
444444

445445
Using this decorator, our database example from the previous section
@@ -1737,7 +1737,7 @@ Optimizations
17371737
(Contributed by Antoine Pitrou.) Memory usage is reduced
17381738
by using pymalloc for the Unicode string's data.
17391739

1740-
* The ``with`` statement now stores the :meth:`__exit__` method on the stack,
1740+
* The ``with`` statement now stores the :meth:`~object.__exit__` method on the stack,
17411741
producing a small speedup. (Implemented by Jeffrey Yasskin.)
17421742

17431743
* To reduce memory usage, the garbage collector will now clear internal

0 commit comments

Comments
 (0)