Kaydet (Commit) d8accb32 authored tarafından Nick Coghlan's avatar Nick Coghlan

Get rid of a couple more context object references, fix some markup and clarify…

Get rid of a couple more context object references, fix some markup and clarify what happens when a generator context function swallows an exception.
üst afd5e63e
...@@ -13,7 +13,7 @@ Functions provided: ...@@ -13,7 +13,7 @@ Functions provided:
\begin{funcdesc}{contextmanager}{func} \begin{funcdesc}{contextmanager}{func}
This function is a decorator that can be used to define a factory This function is a decorator that can be used to define a factory
function for \keyword{with} statement context objects, without function for \keyword{with} statement context managers, without
needing to create a class or separate \method{__enter__()} and needing to create a class or separate \method{__enter__()} and
\method{__exit__()} methods. \method{__exit__()} methods.
...@@ -52,9 +52,10 @@ occurred. Thus, you can use a ...@@ -52,9 +52,10 @@ occurred. Thus, you can use a
the error (if any), or ensure that some cleanup takes place. If an the error (if any), or ensure that some cleanup takes place. If an
exception is trapped merely in order to log it or to perform some exception is trapped merely in order to log it or to perform some
action (rather than to suppress it entirely), the generator must action (rather than to suppress it entirely), the generator must
reraise that exception. Otherwise the \keyword{with} statement will reraise that exception. Otherwise the generator context manager will
treat the exception as having been handled, and resume execution with indicate to the \keyword{with} statement that the exception has been
the statement immediately following the \keyword{with} statement. handled, and execution will resume with the statement immediately
following the \keyword{with} statement.
\end{funcdesc} \end{funcdesc}
\begin{funcdesc}{nested}{mgr1\optional{, mgr2\optional{, ...}}} \begin{funcdesc}{nested}{mgr1\optional{, mgr2\optional{, ...}}}
...@@ -81,9 +82,9 @@ with A as X: ...@@ -81,9 +82,9 @@ with A as X:
Note that if the \method{__exit__()} method of one of the nested Note that if the \method{__exit__()} method of one of the nested
context managers indicates an exception should be suppressed, no context managers indicates an exception should be suppressed, no
exception information will be passed to any remaining outer context exception information will be passed to any remaining outer context
objects. Similarly, if the \method{__exit__()} method of one of the managers. Similarly, if the \method{__exit__()} method of one of the
nested context managers raises an exception, any previous exception nested managers raises an exception, any previous exception state will
state will be lost; the new exception will be passed to the be lost; the new exception will be passed to the
\method{__exit__()} methods of any remaining outer context managers. \method{__exit__()} methods of any remaining outer context managers.
In general, \method{__exit__()} methods should avoid raising In general, \method{__exit__()} methods should avoid raising
exceptions, and in particular they should not re-raise a exceptions, and in particular they should not re-raise a
......
...@@ -1778,8 +1778,8 @@ define a runtime context: ...@@ -1778,8 +1778,8 @@ define a runtime context:
An example of a context manager that returns itself is a file object. An example of a context manager that returns itself is a file object.
File objects return themselves from __enter__() to allow File objects return themselves from __enter__() to allow
\function{open()} to be used as the context expression in a with \function{open()} to be used as the context expression in a
statement. \keyword{with} statement.
An example of a context manager that returns a related An example of a context manager that returns a related
object is the one returned by \code{decimal.Context.get_manager()}. object is the one returned by \code{decimal.Context.get_manager()}.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment