Skip to content

Commit d59f11c

Browse files
AA-Turnermiss-islington
authored andcommitted
pythonGH-101100: Fix reference warnings for __enter__ and __exit__ (pythonGH-110112)
(cherry picked from commit 63acf78) Co-authored-by: Adam Turner <[email protected]>
1 parent 26a0282 commit d59f11c

File tree

11 files changed

+58
-58
lines changed

11 files changed

+58
-58
lines changed

Doc/glossary.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ Glossary
248248

249249
context manager
250250
An object which controls the environment seen in a :keyword:`with`
251-
statement by defining :meth:`__enter__` and :meth:`__exit__` methods.
251+
statement by defining :meth:`~object.__enter__` and :meth:`~object.__exit__` methods.
252252
See :pep:`343`.
253253

254254
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,
@@ -508,7 +508,7 @@ Functions and classes provided:
508508
# the with statement, even if attempts to open files later
509509
# in the list raise an exception
510510

511-
The :meth:`__enter__` method returns the :class:`ExitStack` instance, and
511+
The :meth:`~object.__enter__` method returns the :class:`ExitStack` instance, and
512512
performs no additional operations.
513513

514514
Each instance maintains a stack of registered callbacks that are called in
@@ -536,9 +536,9 @@ Functions and classes provided:
536536

537537
.. method:: enter_context(cm)
538538

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

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

550550
.. method:: push(exit)
551551

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

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

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

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

565565
The passed in object is returned from the function, allowing this
566566
method to be used as a function decorator.
@@ -707,7 +707,7 @@ Cleaning up in an ``__enter__`` implementation
707707

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

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

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

870870
.. seealso::

Doc/library/stdtypes.rst

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

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

Doc/library/test.rst

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

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

10261026

10271027
.. class:: SaveSignals()

Doc/library/unittest.mock.rst

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

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

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

Doc/reference/compound_stmts.rst

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

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

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

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

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

503503
.. note::
504504

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

511511
#. The suite is executed.
512512

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

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

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

527527
The following code::

Doc/reference/datamodel.rst

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

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

29262926

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

31933193
.. method:: object.__aenter__(self)
31943194

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

31983198
.. method:: object.__aexit__(self, exc_type, exc_value, traceback)
31993199

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

32033203
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)