@@ -269,15 +269,15 @@ structure is::
269
269
with-block
270
270
271
271
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__ `
273
273
methods).
274
274
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
276
276
therefore can run set-up code. It also may return a value that is bound to the
277
277
name *variable *, if given. (Note carefully that *variable * is *not * assigned
278
278
the result of *expression *.)
279
279
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__ `
281
281
method is called, even if the block raised an exception, and can therefore run
282
282
clean-up code.
283
283
@@ -296,7 +296,7 @@ part-way through the block.
296
296
.. note ::
297
297
298
298
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 *.
300
300
301
301
The :mod: `threading ` module's locks and condition variables also support the
302
302
':keyword: `with `' statement::
@@ -339,16 +339,16 @@ underlying implementation and should keep reading.
339
339
A high-level explanation of the context management protocol is:
340
340
341
341
* 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__ `
343
343
methods.
344
344
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
346
346
is assigned to *VAR *. If no ``as VAR `` clause is present, the value is simply
347
347
discarded.
348
348
349
349
* The code in *BLOCK * is executed.
350
350
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
352
352
is called with three arguments, the exception details (``type, value, traceback ``,
353
353
the same values returned by :func: `sys.exc_info `, which can also be ``None ``
354
354
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:
357
357
if you do the author of the code containing the ':keyword: `with `' statement will
358
358
never realize anything went wrong.
359
359
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
361
361
called, but *type *, *value *, and *traceback * are all ``None ``.
362
362
363
363
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
391
391
def rollback(self):
392
392
"Rolls back current transaction"
393
393
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
395
395
transaction. For this application the resulting cursor object would be a useful
396
396
result, so the method will return it. The user can then add ``as cursor `` to
397
397
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. ::
403
403
cursor = self.cursor()
404
404
return cursor
405
405
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
407
407
the work has to be done. The method has to check if an exception occurred. If
408
408
there was no exception, the transaction is committed. The transaction is rolled
409
409
back if there was an exception.
@@ -436,10 +436,10 @@ are useful when writing objects for use with the ':keyword:`with`' statement.
436
436
The decorator is called :func: `contextmanager `, and lets you write a single
437
437
generator function instead of defining a new class. The generator should yield
438
438
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
440
440
value that will get bound to the variable in the ':keyword: `with `' statement's
441
441
: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
443
443
be raised by the :keyword: `!yield ` statement.
444
444
445
445
Using this decorator, our database example from the previous section
@@ -1737,7 +1737,7 @@ Optimizations
1737
1737
(Contributed by Antoine Pitrou.) Memory usage is reduced
1738
1738
by using pymalloc for the Unicode string's data.
1739
1739
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,
1741
1741
producing a small speedup. (Implemented by Jeffrey Yasskin.)
1742
1742
1743
1743
* To reduce memory usage, the garbage collector will now clear internal
0 commit comments