contextlib —- Utilities for with-statement contexts

源代码Lib/contextlib.py


此模块为涉及 with 语句的常见任务提供了实用的程序。更多信息请参见 上下文管理器类型with 语句上下文管理器

工具

提供的函数和类:

3.6 新版功能.

3.7 新版功能.

  • @contextlib.contextmanager
  • This function is a decorator that can be used to define a factoryfunction for with statement 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 with contextlib.closing

An abstract example would be the following to ensure correct resourcemanagement:

  1. from contextlib import contextmanager
  2.  
  3. @contextmanager
  4. def managed_resource(*args, **kwds):
  5. # Code to acquire resource, e.g.:
  6. resource = acquire_resource(*args, **kwds)
  7. try:
  8. yield resource
  9. finally:
  10. # Code to release resource, e.g.:
  11. release_resource(resource)
  12.  
  13. >>> with managed_resource(timeout=3600) as resource:
  14. ... # Resource is released at the end of this block,
  15. ... # even if code in the block raises an exception

被装饰的函数在被调用时,必须返回一个 generator-iterator。这个迭代器必须只 yield 一个值出来,这个值会被用在 with 语句中,绑定到 as 后面的变量,如果给定了的话。

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 atryexceptfinally statement 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 the with statement thatthe exception has been handled, and execution will resume with the statementimmediately following the with statement.

contextmanager() uses ContextDecorator so the context managersit creates can be used as decorators as well as in with statements.When used as a decorator, a new generator instance is implicitly created oneach function call (this allows the otherwise "one-shot" context managerscreated by contextmanager() to meet the requirement that contextmanagers support multiple invocations in order to be used as decorators).

在 3.2 版更改: Use of ContextDecorator.

This function is a decorator that can be used to define a factoryfunction for async with statement asynchronous context managers,without needing to create a class or separate aenter() andaexit() methods. It must be applied to an asynchronousgenerator function.

A simple example:

  1. from contextlib import asynccontextmanager
  2.  
  3. @asynccontextmanager
  4. async def get_connection():
  5. conn = await acquire_db_connection()
  6. try:
  7. yield conn
  8. finally:
  9. await release_db_connection(conn)
  10.  
  11. async def get_all_users():
  12. async with get_connection() as conn:
  13. return conn.query('SELECT ...')

3.7 新版功能.

  • contextlib.closing(thing)
  • Return a context manager that closes thing upon completion of the block. Thisis basically equivalent to:
  1. from contextlib import contextmanager
  2.  
  3. @contextmanager
  4. def closing(thing):
  5. try:
  6. yield thing
  7. finally:
  8. thing.close()

And lets you write code like this:

  1. from contextlib import closing
  2. from urllib.request import urlopen
  3.  
  4. with closing(urlopen('http://www.python.org')) as page:
  5. for line in page:
  6. print(line)

without needing to explicitly close page. Even if an error occurs,page.close() will be called when the with block is exited.

  • contextlib.nullcontext(enter_result=None)
  • Return a context manager that returns enter_result from enter, butotherwise does nothing. It is intended to be used as a stand-in for anoptional context manager, for example:
  1. def myfunction(arg, ignore_exceptions=False):
  2. if ignore_exceptions:
  3. # Use suppress to ignore all exceptions.
  4. cm = contextlib.suppress(Exception)
  5. else:
  6. # Do not ignore any exceptions, cm has no effect.
  7. cm = contextlib.nullcontext()
  8. with cm:
  9. # Do something

An example using enter_result:

  1. def process_file(file_or_path):
  2. if isinstance(file_or_path, str):
  3. # If string, open file
  4. cm = open(file_or_path)
  5. else:
  6. # Caller is responsible for closing file
  7. cm = nullcontext(file_or_path)
  8.  
  9. with cm as file:
  10. # Perform processing on the file

3.7 新版功能.

  • contextlib.suppress(*exceptions)
  • Return a context manager that suppresses any of the specified exceptionsif they occur in the body of a with statement and then resumes executionwith the first statement following the end of the with statement.

As with any other mechanism that completely suppresses exceptions, thiscontext manager should be used only to cover very specific errors wheresilently continuing with program execution is known to be the rightthing to do.

例如

  1. from contextlib import suppress
  2.  
  3. with suppress(FileNotFoundError):
  4. os.remove('somefile.tmp')
  5.  
  6. with suppress(FileNotFoundError):
  7. os.remove('someotherfile.tmp')

This code is equivalent to:

  1. try:
  2. os.remove('somefile.tmp')
  3. except FileNotFoundError:
  4. pass
  5.  
  6. try:
  7. os.remove('someotherfile.tmp')
  8. except FileNotFoundError:
  9. pass

This context manager is reentrant.

3.4 新版功能.

  • contextlib.redirectstdout(_new_target)
  • Context manager for temporarily redirecting sys.stdout toanother file or file-like object.

This tool adds flexibility to existing functions or classes whose outputis hardwired to stdout.

For example, the output of help() normally is sent to sys.stdout.You can capture that output in a string by redirecting the output to anio.StringIO object:

  1. f = io.StringIO()
  2. with redirect_stdout(f):
  3. help(pow)
  4. s = f.getvalue()

To send the output of help() to a file on disk, redirect the outputto a regular file:

  1. with open('help.txt', 'w') as f:
  2. with redirect_stdout(f):
  3. help(pow)

To send the output of help() to sys.stderr:

  1. with redirect_stdout(sys.stderr):
  2. help(pow)

Note that the global side effect on sys.stdout means that thiscontext manager is not suitable for use in library code and most threadedapplications. It also has no effect on the output of subprocesses.However, it is still a useful approach for many utility scripts.

This context manager is reentrant.

3.4 新版功能.

This context manager is reentrant.

3.5 新版功能.

  • class contextlib.ContextDecorator
  • A base class that enables a context manager to also be used as a decorator.

Context managers inheriting from ContextDecorator have to implemententer and exit as normal. exit retains its optionalexception handling even when used as a decorator.

ContextDecorator is used by contextmanager(), so you get thisfunctionality automatically.

ContextDecorator 的示例:

  1. from contextlib import ContextDecorator
  2.  
  3. class mycontext(ContextDecorator):
  4. def __enter__(self):
  5. print('Starting')
  6. return self
  7.  
  8. def __exit__(self, *exc):
  9. print('Finishing')
  10. return False
  11.  
  12. >>> @mycontext()
  13. ... def function():
  14. ... print('The bit in the middle')
  15. ...
  16. >>> function()
  17. Starting
  18. The bit in the middle
  19. Finishing
  20.  
  21. >>> with mycontext():
  22. ... print('The bit in the middle')
  23. ...
  24. Starting
  25. The bit in the middle
  26. Finishing

This change is just syntactic sugar for any construct of the following form:

  1. def f():
  2. with cm():
  3. # Do stuff

ContextDecorator lets you instead write:

  1. @cm()def f():

  2. # Do stuff

It makes it clear that the cm applies to the whole function, rather thanjust a piece of it (and saving an indentation level is nice, too).

Existing context managers that already have a base class can be extended byusing ContextDecorator as a mixin class:

  1. from contextlib import ContextDecorator
  2.  
  3. class mycontext(ContextBaseClass, ContextDecorator):
  4. def __enter__(self):
  5. return self
  6.  
  7. def __exit__(self, *exc):
  8. return False

注解

As the decorated function must be able to be called multiple times, theunderlying context manager must support use in multiple withstatements. If this is not the case, then the original construct with theexplicit with statement inside the function should be used.

3.2 新版功能.

  • class contextlib.ExitStack
  • A context manager that is designed to make it easy to programmaticallycombine other context managers and cleanup functions, especially thosethat are optional or otherwise driven by input data.

For example, a set of files may easily be handled in a single withstatement as follows:

  1. with ExitStack() as stack:
  2. files = [stack.enter_context(open(fname)) for fname in filenames]
  3. # All opened files will automatically be closed at the end of
  4. # the with statement, even if attempts to open files later
  5. # in the list raise an exception

Each instance maintains a stack of registered callbacks that are called inreverse order when the instance is closed (either explicitly or implicitlyat the end of a with statement). Note that callbacks are _not_invoked implicitly when the context stack instance is garbage collected.

This stack model is used so that context managers that acquire theirresources in their init method (such as file objects) can behandled correctly.

Since registered callbacks are invoked in the reverse order ofregistration, this ends up behaving as if multiple nested withstatements had been used with the registered set of callbacks. This evenextends to exception handling - if an inner callback suppresses or replacesan exception, then outer callbacks will be passed arguments based on thatupdated state.

This is a relatively low level API that takes care of the details ofcorrectly unwinding the stack of exit callbacks. It provides a suitablefoundation for higher level context managers that manipulate the exitstack in application specific ways.

3.3 新版功能.

  • entercontext(_cm)
  • Enters a new context manager and adds its exit() method tothe callback stack. The return value is the result of the contextmanager's own enter() method.

These context managers may suppress exceptions just as they normallywould if used directly as part of a with statement.

  • push(exit)
  • Adds a context manager's exit() method to the callback stack.

As enter is not invoked, this method can be used to coverpart of an enter() implementation with a context manager's ownexit() method.

If passed an object that is not a context manager, this method assumesit is a callback with the same signature as a context manager'sexit() method and adds it directly to the callback stack.

By returning true values, these callbacks can suppress exceptions thesame way context manager exit() methods can.

The passed in object is returned from the function, allowing thismethod to be used as a function decorator.

  • callback(callback, *args, **kwds)
  • Accepts an arbitrary callback function and arguments and adds it tothe callback stack.

Unlike the other methods, callbacks added this way cannot suppressexceptions (as they are never passed the exception details).

The passed in callback is returned from the function, allowing thismethod to be used as a function decorator.

  • pop_all()
  • Transfers the callback stack to a fresh ExitStack instanceand returns it. No callbacks are invoked by this operation - instead,they will now be invoked when the new stack is closed (eitherexplicitly or implicitly at the end of a with statement).

For example, a group of files can be opened as an "all or nothing"operation as follows:

  1. with ExitStack() as stack:
  2. files = [stack.enter_context(open(fname)) for fname in filenames]
  3. # Hold onto the close method, but don't call it yet.
  4. close_files = stack.pop_all().close
  5. # If opening any file fails, all previously opened files will be
  6. # closed automatically. If all files are opened successfully,
  7. # they will remain open even after the with statement ends.
  8. # close_files() can then be invoked explicitly to close them all.
  • close()
  • Immediately unwinds the callback stack, invoking callbacks in thereverse order of registration. For any context managers and exitcallbacks registered, the arguments passed in will indicate that noexception occurred.
  • class contextlib.AsyncExitStack
  • An asynchronous context manager, similarto ExitStack, that supports combining both synchronous andasynchronous context managers, as well as having coroutines forcleanup logic.

The close() method is not implemented, aclose() must be usedinstead.

  • enterasync_context(_cm)
  • Similar to enter_context() but expects an asynchronous contextmanager.

  • pushasync_exit(_exit)

  • Similar to push() but expects either an asynchronous context manageror a coroutine function.

  • pushasync_callback(_callback, *args, **kwds)

  • Similar to callback() but expects a coroutine function.

  • aclose()

  • Similar to close() but properly handles awaitables.

Continuing the example for asynccontextmanager():

  1. async with AsyncExitStack() as stack:
  2. connections = [await stack.enter_async_context(get_connection())
  3. for i in range(5)]
  4. # All opened connections will automatically be released at the end of
  5. # the async with statement, even if attempts to open a connection
  6. # later in the list raise an exception.

3.7 新版功能.

例子和配方

This section describes some examples and recipes for making effective use ofthe tools provided by contextlib.

Supporting a variable number of context managers

The primary use case for ExitStack is the one given in the classdocumentation: supporting a variable number of context managers and othercleanup operations in a single with statement. The variabilitymay come from the number of context managers needed being driven by userinput (such as opening a user specified collection of files), or fromsome of the context managers being optional:

  1. with ExitStack() as stack:
  2. for resource in resources:
  3. stack.enter_context(resource)
  4. if need_special_resource():
  5. special = acquire_special_resource()
  6. stack.callback(release_special_resource, special)
  7. # Perform operations that use the acquired resources

As shown, ExitStack also makes it quite easy to use withstatements to manage arbitrary resources that don't natively support thecontext management protocol.

Catching exceptions from enter methods

It is occasionally desirable to catch exceptions from an entermethod implementation, without inadvertently catching exceptions fromthe with statement body or the context manager's exitmethod. By using ExitStack the steps in the context managementprotocol can be separated slightly in order to allow this:

  1. stack = ExitStack()
  2. try:
  3. x = stack.enter_context(cm)
  4. except Exception:
  5. # handle __enter__ exception
  6. else:
  7. with stack:
  8. # Handle normal case

Actually needing to do this is likely to indicate that the underlying APIshould be providing a direct resource management interface for use withtry/except/finally statements, but notall APIs are well designed in that regard. When a context manager is theonly resource management API provided, then ExitStack can make iteasier to handle various situations that can't be handled directly in awith statement.

Cleaning up in an enter implementation

As noted in the documentation of ExitStack.push(), thismethod can be useful in cleaning up an already allocated resource if latersteps in the enter() implementation fail.

Here's an example of doing this for a context manager that accepts resourceacquisition and release functions, along with an optional validation function,and maps them to the context management protocol:

  1. from contextlib import contextmanager, AbstractContextManager, ExitStack
  2.  
  3. class ResourceManager(AbstractContextManager):
  4.  
  5. def __init__(self, acquire_resource, release_resource, check_resource_ok=None):
  6. self.acquire_resource = acquire_resource
  7. self.release_resource = release_resource
  8. if check_resource_ok is None:
  9. def check_resource_ok(resource):
  10. return True
  11. self.check_resource_ok = check_resource_ok
  12.  
  13. @contextmanager
  14. def _cleanup_on_error(self):
  15. with ExitStack() as stack:
  16. stack.push(self)
  17. yield
  18. # The validation check passed and didn't raise an exception
  19. # Accordingly, we want to keep the resource, and pass it
  20. # back to our caller
  21. stack.pop_all()
  22.  
  23. def __enter__(self):
  24. resource = self.acquire_resource()
  25. with self._cleanup_on_error():
  26. if not self.check_resource_ok(resource):
  27. msg = "Failed validation for {!r}"
  28. raise RuntimeError(msg.format(resource))
  29. return resource
  30.  
  31. def __exit__(self, *exc_details):
  32. # We don't need to duplicate any of our resource release logic
  33. self.release_resource()

Replacing any use of try-finally and flag variables

A pattern you will sometimes see is a try-finally statement with a flagvariable to indicate whether or not the body of the finally clause shouldbe executed. In its simplest form (that can't already be handled just byusing an except clause instead), it looks something like this:

  1. cleanup_needed = True
  2. try:
  3. result = perform_operation()
  4. if result:
  5. cleanup_needed = False
  6. finally:
  7. if cleanup_needed:
  8. cleanup_resources()

As with any try statement based code, this can cause problems fordevelopment and review, because the setup code and the cleanup code can endup being separated by arbitrarily long sections of code.

ExitStack makes it possible to instead register a callback forexecution at the end of a with statement, and then later decide to skipexecuting that callback:

  1. from contextlib import ExitStack
  2.  
  3. with ExitStack() as stack:
  4. stack.callback(cleanup_resources)
  5. result = perform_operation()
  6. if result:
  7. stack.pop_all()

This allows the intended cleanup up behaviour to be made explicit up front,rather than requiring a separate flag variable.

If a particular application uses this pattern a lot, it can be simplifiedeven further by means of a small helper class:

  1. from contextlib import ExitStack
  2.  
  3. class Callback(ExitStack):
  4. def __init__(self, callback, *args, **kwds):
  5. super(Callback, self).__init__()
  6. self.callback(callback, *args, **kwds)
  7.  
  8. def cancel(self):
  9. self.pop_all()
  10.  
  11. with Callback(cleanup_resources) as cb:
  12. result = perform_operation()
  13. if result:
  14. cb.cancel()

If the resource cleanup isn't already neatly bundled into a standalonefunction, then it is still possible to use the decorator form ofExitStack.callback() to declare the resource cleanup inadvance:

  1. from contextlib import ExitStack
  2.  
  3. with ExitStack() as stack:
  4. @stack.callback
  5. def cleanup_resources():
  6. ...
  7. result = perform_operation()
  8. if result:
  9. stack.pop_all()

Due to the way the decorator protocol works, a callback functiondeclared this way cannot take any parameters. Instead, any resources tobe released must be accessed as closure variables.

Using a context manager as a function decorator

ContextDecorator makes it possible to use a context manager inboth an ordinary with statement and also as a function decorator.

For example, it is sometimes useful to wrap functions or groups of statementswith a logger that can track the time of entry and time of exit. Rather thanwriting both a function decorator and a context manager for the task,inheriting from ContextDecorator provides both capabilities in asingle definition:

  1. from contextlib import ContextDecorator
  2. import logging
  3.  
  4. logging.basicConfig(level=logging.INFO)
  5.  
  6. class track_entry_and_exit(ContextDecorator):
  7. def __init__(self, name):
  8. self.name = name
  9.  
  10. def __enter__(self):
  11. logging.info('Entering: %s', self.name)
  12.  
  13. def __exit__(self, exc_type, exc, exc_tb):
  14. logging.info('Exiting: %s', self.name)

Instances of this class can be used as both a context manager:

  1. with track_entry_and_exit('widget loader'):
  2. print('Some time consuming activity goes here')
  3. load_widget()

And also as a function decorator:

  1. @track_entry_and_exit('widget loader')def activity(): print('Some time consuming activity goes here') load_widget()

Note that there is one additional limitation when using context managersas function decorators: there's no way to access the return value ofenter(). If that value is needed, then it is still necessary to usean explicit with statement.

参见

  • PEP 343 - "with" 语句
  • Python with 语句的规范描述、背景和示例。

Single use, reusable and reentrant context managers

Most context managers are written in a way that means they can only beused effectively in a with statement once. These single usecontext managers must be created afresh each time they're used -attempting to use them a second time will trigger an exception orotherwise not work correctly.

This common limitation means that it is generally advisable to createcontext managers directly in the header of the with statementwhere they are used (as shown in all of the usage examples above).

Files are an example of effectively single use context managers, sincethe first with statement will close the file, preventing anyfurther IO operations using that file object.

Context managers created using contextmanager() are also single usecontext managers, and will complain about the underlying generator failingto yield if an attempt is made to use them a second time:

  1. >>> from contextlib import contextmanager
  2. >>> @contextmanager
  3. ... def singleuse():
  4. ... print("Before")
  5. ... yield
  6. ... print("After")
  7. ...
  8. >>> cm = singleuse()
  9. >>> with cm:
  10. ... pass
  11. ...
  12. Before
  13. After
  14. >>> with cm:
  15. ... pass
  16. ...
  17. Traceback (most recent call last):
  18. ...
  19. RuntimeError: generator didn't yield

Reentrant context managers

More sophisticated context managers may be "reentrant". These contextmanagers can not only be used in multiple with statements,but may also be used inside a with statement that is alreadyusing the same context manager.

threading.RLock is an example of a reentrant context manager, as aresuppress() and redirect_stdout(). Here's a very simple example ofreentrant use:

  1. >>> from contextlib import redirect_stdout
  2. >>> from io import StringIO
  3. >>> stream = StringIO()
  4. >>> write_to_stream = redirect_stdout(stream)
  5. >>> with write_to_stream:
  6. ... print("This is written to the stream rather than stdout")
  7. ... with write_to_stream:
  8. ... print("This is also written to the stream")
  9. ...
  10. >>> print("This is written directly to stdout")
  11. This is written directly to stdout
  12. >>> print(stream.getvalue())
  13. This is written to the stream rather than stdout
  14. This is also written to the stream

Real world examples of reentrancy are more likely to involve multiplefunctions calling each other and hence be far more complicated than thisexample.

Note also that being reentrant is not the same thing as being thread safe.redirect_stdout(), for example, is definitely not thread safe, as itmakes a global modification to the system state by binding sys.stdoutto a different stream.

Reusable context managers

Distinct from both single use and reentrant context managers are "reusable"context managers (or, to be completely explicit, "reusable, but notreentrant" context managers, since reentrant context managers are alsoreusable). These context managers support being used multiple times, butwill fail (or otherwise not work correctly) if the specific context managerinstance has already been used in a containing with statement.

threading.Lock is an example of a reusable, but not reentrant,context manager (for a reentrant lock, it is necessary to usethreading.RLock instead).

Another example of a reusable, but not reentrant, context manager isExitStack, as it invokes all currently registered callbackswhen leaving any with statement, regardless of where those callbackswere added:

  1. >>> from contextlib import ExitStack
  2. >>> stack = ExitStack()
  3. >>> with stack:
  4. ... stack.callback(print, "Callback: from first context")
  5. ... print("Leaving first context")
  6. ...
  7. Leaving first context
  8. Callback: from first context
  9. >>> with stack:
  10. ... stack.callback(print, "Callback: from second context")
  11. ... print("Leaving second context")
  12. ...
  13. Leaving second context
  14. Callback: from second context
  15. >>> with stack:
  16. ... stack.callback(print, "Callback: from outer context")
  17. ... with stack:
  18. ... stack.callback(print, "Callback: from inner context")
  19. ... print("Leaving inner context")
  20. ... print("Leaving outer context")
  21. ...
  22. Leaving inner context
  23. Callback: from inner context
  24. Callback: from outer context
  25. Leaving outer context

As the output from the example shows, reusing a single stack object acrossmultiple with statements works correctly, but attempting to nest themwill cause the stack to be cleared at the end of the innermost withstatement, which is unlikely to be desirable behaviour.

Using separate ExitStack instances instead of reusing a singleinstance avoids that problem:

  1. >>> from contextlib import ExitStack
  2. >>> with ExitStack() as outer_stack:
  3. ... outer_stack.callback(print, "Callback: from outer context")
  4. ... with ExitStack() as inner_stack:
  5. ... inner_stack.callback(print, "Callback: from inner context")
  6. ... print("Leaving inner context")
  7. ... print("Leaving outer context")
  8. ...
  9. Leaving inner context
  10. Callback: from inner context
  11. Leaving outer context
  12. Callback: from outer context