summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
Diffstat (limited to 'Doc')
-rw-r--r--Doc/lib/libcontextlib.tex47
-rw-r--r--Doc/ref/ref3.tex16
-rw-r--r--Doc/ref/ref7.tex13
3 files changed, 5 insertions, 71 deletions
diff --git a/Doc/lib/libcontextlib.tex b/Doc/lib/libcontextlib.tex
index 9ff8524..6c80a71 100644
--- a/Doc/lib/libcontextlib.tex
+++ b/Doc/lib/libcontextlib.tex
@@ -54,34 +54,6 @@ action (rather than to suppress it entirely), the generator must
reraise that exception. Otherwise the \keyword{with} statement will
treat the exception as having been handled, and resume execution with
the statement immediately following the \keyword{with} statement.
-
-Note that you can use \code{@contextfactory} to define a context
-manager's \method{__context__} method. This is usually more
-convenient than creating another class just to serve as a context
-object. For example:
-
-\begin{verbatim}
-from __future__ import with_statement
-from contextlib import contextfactory
-
-class Tag:
- def __init__(self, name):
- self.name = name
-
- @contextfactory
- def __context__(self):
- print "<%s>" % self.name
- yield self
- print "</%s>" % self.name
-
-h1 = Tag("h1")
-
->>> with h1 as me:
-... print "hello from", me
-<h1>
-hello from <__main__.Tag instance at 0x402ce8ec>
-</h1>
-\end{verbatim}
\end{funcdesc}
\begin{funcdesc}{nested}{ctx1\optional{, ctx2\optional{, ...}}}
@@ -147,25 +119,6 @@ with closing(urllib.urlopen('http://www.python.org')) as page:
without needing to explicitly close \code{page}. Even if an error
occurs, \code{page.close()} will be called when the \keyword{with}
block is exited.
-
-Context managers with a close method can use this context factory
-to easily implement their own \method{__context__()} method.
-\begin{verbatim}
-from __future__ import with_statement
-from contextlib import closing
-
-class MyClass:
- def close(self):
- print "Closing", self
- def __context__(self):
- return closing(self)
-
->>> with MyClass() as x:
-... print "Hello from", x
-...
-Hello from <__main__.MyClass instance at 0xb7df02ec>
-Closing <__main__.MyClass instance at 0xb7df02ec>
-\end{verbatim}
\end{funcdesc}
\begin{seealso}
diff --git a/Doc/ref/ref3.tex b/Doc/ref/ref3.tex
index d22448c..ba0594f 100644
--- a/Doc/ref/ref3.tex
+++ b/Doc/ref/ref3.tex
@@ -2138,22 +2138,6 @@ For more information on context managers and context objects,
see ``\ulink{Context Types}{../lib/typecontext.html}'' in the
\citetitle[../lib/lib.html]{Python Library Reference}.
-\begin{methoddesc}[context manager]{__context__}{self}
-Invoked when the object is used as the context expression of a
-\keyword{with} statement. The returned object must implement
-\method{__enter__()} and \method{__exit__()} methods.
-
-Context managers written in Python can also implement this method
-using a generator function decorated with the
-\function{contextlib.contextfactory} decorator, as this can be simpler
-than writing individual \method{__enter__()} and \method{__exit__()}
-methods on a separate object when the state to be managed is complex.
-
-\keyword{with} statement context objects also need to implement this
-method; they are required to return themselves (that is, this method
-will simply return \var{self}).
-\end{methoddesc}
-
\begin{methoddesc}[with statement context]{__enter__}{self}
Enter the runtime context related to this object. The \keyword{with}
statement will bind this method's return value to the target(s)
diff --git a/Doc/ref/ref7.tex b/Doc/ref/ref7.tex
index 4453e87..4a23133 100644
--- a/Doc/ref/ref7.tex
+++ b/Doc/ref/ref7.tex
@@ -322,21 +322,18 @@ be encapsulated for convenient reuse.
\begin{productionlist}
\production{with_stmt}
- {"with" \token{expression} ["as" target_list] ":" \token{suite}}
+ {"with" \token{expression} ["as" target] ":" \token{suite}}
\end{productionlist}
The execution of the \keyword{with} statement proceeds as follows:
\begin{enumerate}
-\item The context expression is evaluated, to obtain a context manager.
+\item The context expression is evaluated to obtain a context manager.
-\item The context manger's \method{__context__()} method is
-invoked to obtain a \keyword{with} statement context object.
+\item The context manager's \method{__enter__()} method is invoked.
-\item The context object's \method{__enter__()} method is invoked.
-
-\item If a target list was included in the \keyword{with}
+\item If a target was included in the \keyword{with}
statement, the return value from \method{__enter__()} is assigned to it.
\note{The \keyword{with} statement guarantees that if the
@@ -347,7 +344,7 @@ an error occurring within the suite would be. See step 6 below.}
\item The suite is executed.
-\item The context object's \method{__exit__()} method is invoked. If
+\item The context manager's \method{__exit__()} method is invoked. If
an exception caused the suite to be exited, its type, value, and
traceback are passed as arguments to \method{__exit__()}. Otherwise,
three \constant{None} arguments are supplied.