Mercurial > cpython-lru_cache-weakref
diff Doc/lib/libcontextlib.tex @ 38101:6bc1e83a97a6 legacy-trunk
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
| author | Nick Coghlan <ncoghlan@gmail.com> |
|---|---|
| date | Wed, 03 May 2006 13:02:47 +0000 |
| parents | a812d0fae74a |
| children | e92a8b8eec73 |
line wrap: on
line diff
--- a/Doc/lib/libcontextlib.tex Wed May 03 05:15:10 2006 +0000 +++ b/Doc/lib/libcontextlib.tex Wed May 03 13:02:47 2006 +0000 @@ -11,19 +11,20 @@ Functions provided: -\begin{funcdesc}{context}{func} +\begin{funcdesc}{contextmanager}{func} This function is a decorator that can be used to define a factory function for \keyword{with} statement context objects, without needing to create a class or separate \method{__enter__()} and \method{__exit__()} methods. -A simple example: +A simple example (this is not recommended as a real way of +generating HTML!): \begin{verbatim} from __future__ import with_statement -from contextlib import contextfactory +from contextlib import contextmanager -@contextfactory +@contextmanager def tag(name): print "<%s>" % name yield @@ -56,7 +57,7 @@ the statement immediately following the \keyword{with} statement. \end{funcdesc} -\begin{funcdesc}{nested}{ctx1\optional{, ctx2\optional{, ...}}} +\begin{funcdesc}{nested}{mgr1\optional{, mgr2\optional{, ...}}} Combine multiple context managers into a single nested context manager. Code like this: @@ -78,12 +79,12 @@ \end{verbatim} Note that if the \method{__exit__()} method of one of the nested -context objects 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 objects. Similarly, if the \method{__exit__()} method of one of the -nested context objects raises an exception, any previous exception +nested context managers raises an exception, any previous exception state will be lost; the new exception will be passed to the -\method{__exit__()} methods of any remaining outer context objects. +\method{__exit__()} methods of any remaining outer context managers. In general, \method{__exit__()} methods should avoid raising exceptions, and in particular they should not re-raise a passed-in exception. @@ -91,13 +92,13 @@ \label{context-closing} \begin{funcdesc}{closing}{thing} -Return a context that closes \var{thing} upon completion of the -block. This is basically equivalent to: +Return a context manager that closes \var{thing} upon completion of +the block. This is basically equivalent to: \begin{verbatim} -from contextlib import contextfactory +from contextlib import contextmanager -@contextfactory +@contextmanager def closing(thing): try: yield thing
