28.7.contextlib — Utilities forwith-statement contexts¶
New in version 2.5.
Source code:Lib/contextlib.py
This module provides utilities for common tasks involving thewithstatement. For more information see alsoContext Manager Types andWith Statement Context Managers.
Functions provided:
contextlib.contextmanager(func)¶This function is adecorator that can be used to define a factoryfunction for
withstatement context managers, without needing tocreate a class or separate__enter__()and__exit__()methods.While many objects natively support use in with statements, sometimes aresource needs to be managed that isn’t a context manager in its own right,and doesn’t implement a
close()method for use withcontextlib.closingAn abstract example would be the following to ensure correct resourcemanagement:
fromcontextlibimportcontextmanager@contextmanagerdefmanaged_resource(*args,**kwds):# Code to acquire resource, e.g.:resource=acquire_resource(*args,**kwds)try:yieldresourcefinally:# Code to release resource, e.g.:release_resource(resource)>>>withmanaged_resource(timeout=3600)asresource:...# Resource is released at the end of this block,...# even if code in the block raises an exception
The function being decorated must return agenerator-iterator whencalled. This iterator must yield exactly one value, which will be bound tothe targets in the
withstatement’sasclause, if any.At the point where the generator yields, the block nested in the
withstatement is executed. The generator is then resumed after the block is exited.If an unhandled exception occurs in the block, it is reraised inside thegenerator at the point where the yield occurred. Thus, you can use atry…except…finallystatement to trapthe error (if any), or ensure that some cleanup takes place. If an exception istrapped merely in order to log it or to perform some action (rather than tosuppress it entirely), the generator must reraise that exception. Otherwise thegenerator context manager will indicate to thewithstatement thatthe exception has been handled, and execution will resume with the statementimmediately following thewithstatement.
contextlib.nested(mgr1[,mgr2[,...]])¶Combine multiple context managers into a single nested context manager.
This function has been deprecated in favour of the multiple manager formof the
withstatement.The one advantage of this function over the multiple manager form of the
withstatement is that argument unpacking allows it to beused with a variable number of context managers as follows:fromcontextlibimportnestedwithnested(*managers):do_something()
Note that if the
__exit__()method of one of the nested context managersindicates an exception should be suppressed, no exception information will bepassed to any remaining outer context managers. Similarly, if the__exit__()method of one of the nested managers raises an exception, anyprevious exception state will be lost; the new exception will be passed to the__exit__()methods of any remaining outer context managers. In general,__exit__()methods should avoid raising exceptions, and in particular theyshould not re-raise a passed-in exception.This function has two major quirks that have led to it being deprecated. Firstly,as the context managers are all constructed before the function is invoked, the
__new__()and__init__()methods of the inner context managers arenot actually covered by the scope of the outer context managers. That means, forexample, that usingnested()to open two files is a programming error as thefirst file will not be closed promptly if an exception is thrown when openingthe second file.Secondly, if the
__enter__()method of one of the inner context managersraises an exception that is caught and suppressed by the__exit__()methodof one of the outer context managers, this construct will raiseRuntimeErrorrather than skipping the body of thewithstatement.Developers that need to support nesting of a variable number of context managerscan either use the
warningsmodule to suppress the DeprecationWarningraised by this function or else use this function as a model for an applicationspecific implementation.Deprecated since version 2.7:The with-statement now supports this functionality directly (without theconfusing error prone quirks).
contextlib.closing(thing)¶Return a context manager that closesthing upon completion of the block. Thisis basically equivalent to:
fromcontextlibimportcontextmanager@contextmanagerdefclosing(thing):try:yieldthingfinally:thing.close()
And lets you write code like this:
fromcontextlibimportclosingimporturllibwithclosing(urllib.urlopen('http://www.python.org'))aspage:forlineinpage:printline
without needing to explicitly close
page. Even if an error occurs,page.close()will be called when thewithblock is exited.
