diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2013-10-13 13:23:08 (GMT) |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2013-10-13 13:23:08 (GMT) |
commit | b4534ae704a4efb385e2343ecdbc15f232ef78b6 (patch) | |
tree | 09b95778c1851210f48b9e82526b6522b3d31ed1 /Doc/library | |
parent | ed7bb488b4d9cf6567649a9b568e8907b159c35e (diff) | |
download | cpython-b4534ae704a4efb385e2343ecdbc15f232ef78b6.zip cpython-b4534ae704a4efb385e2343ecdbc15f232ef78b6.tar.gz cpython-b4534ae704a4efb385e2343ecdbc15f232ef78b6.tar.bz2 |
Docs tweaks for contextlib additions
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/contextlib.rst | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/Doc/library/contextlib.rst b/Doc/library/contextlib.rst index cac113e..cef5f1a 100644 --- a/Doc/library/contextlib.rst +++ b/Doc/library/contextlib.rst @@ -99,22 +99,27 @@ Functions and classes provided: Return a context manager that ignores the specified exceptions if they occur in the body of a with-statement. + As with any other mechanism that completely suppresses exceptions, it + should only be used to cover very specific errors where silently + ignoring the exception is known to be the right thing to do. + For example:: from contextlib import ignore - with ignore(OSError): + with ignore(FileNotFoundError): os.remove('somefile.tmp') This code is equivalent to:: try: os.remove('somefile.tmp') - except OSError: + except FileNotFoundError: pass .. versionadded:: 3.4 + .. function:: redirect_stdout(new_target) Context manager for temporarily redirecting :data:`sys.stdout` to @@ -144,6 +149,11 @@ Functions and classes provided: with redirect_stdout(sys.stderr): help(pow) + Note that the global side effect on :data:`sys.stdout` means that this + context manager is not suitable for use in library code and most threaded + applications. It also has no effect on the output of subprocesses. + However, it is still a useful approach for many utility scripts. + .. versionadded:: 3.4 .. class:: ContextDecorator() |