]> granicus.if.org Git - python/commitdiff
Document objects that can be used with the ``with`` statement.
authorPhillip J. Eby <pje@telecommunity.com>
Tue, 28 Mar 2006 00:13:10 +0000 (00:13 +0000)
committerPhillip J. Eby <pje@telecommunity.com>
Tue, 28 Mar 2006 00:13:10 +0000 (00:13 +0000)
Doc/lib/libdecimal.tex
Doc/lib/libstdtypes.tex
Doc/lib/libthread.tex
Doc/lib/libthreading.tex

index 092f0388756864bf87eb65ccf8a6da7cf04b2576..ffc33630b85ae60478d2a6cd666bfefa8230eed1 100644 (file)
@@ -442,9 +442,33 @@ the \function{getcontext()} and \function{setcontext()} functions:
   Set the current context for the active thread to \var{c}.                                          
 \end{funcdesc}  
 
-New contexts can formed using the \class{Context} constructor described below.
-In addition, the module provides three pre-made contexts:                                          
+Beginning with Python 2.5, you can also use the \keyword{with} statement
+to temporarily change the active context. For example the following code
+increases the current decimal precision by 2 places, performs a
+calculation, and then automatically restores the previous context:
 
+\begin{verbatim}
+from __future__ import with_statement
+import decimal
+
+with decimal.getcontext() as ctx:
+    ctx.prec += 2   # add 2 more digits of precision
+    calculate_something()
+\end{verbatim}
+
+The context that's active in the body of the \keyword{with} statement is
+a \emph{copy} of the context you provided to the \keyword{with}
+statement, so modifying its attributes doesn't affect anything except
+that temporary copy.
+
+You can use any decimal context in a \keyword{with} statement, but if
+you just want to make a temporary change to some aspect of the current
+context, it's easiest to just use \function{getcontext()} as shown
+above.
+
+New contexts can also be created using the \class{Context} constructor
+described below. In addition, the module provides three pre-made
+contexts:
 
 \begin{classdesc*}{BasicContext}
   This is a standard context defined by the General Decimal Arithmetic
index a1fa6f0e51abd6d7f78008bd0a8ba0af9e5fb44c..017b08048b64d5d5ac9e604c6184d2327f05f03a 100644 (file)
@@ -1500,6 +1500,38 @@ Files have the following methods:
   Any operation which requires that the file be open will raise a
   \exception{ValueError} after the file has been closed.  Calling
   \method{close()} more than once is allowed.
+
+  As of Python 2.5, you can avoid having to call this method explicitly
+  if you use the \keyword{with} statement.  For example, the following
+  code will automatically close \code{f} when the \keyword{with} block
+  is exited:
+
+\begin{verbatim}
+from __future__ import with_statement
+
+with open("hello.txt") as f:
+    for line in f:
+        print line
+\end{verbatim}
+
+  In older versions of Python, you would have needed to do this to get
+  the same effect:
+
+\begin{verbatim}
+f = open("hello.txt")
+try:
+    for line in f:
+        print line
+finally:
+    f.close()
+\end{verbatim}
+
+  \note{Not all ``file-like'' types in Python support use as a context
+  manager for the \keyword{with} statement.  If your code is intended to
+  work with any file-like object, you can use the \function{closing()}
+  function in the \module{contextlib} module instead of using the object
+  directly.  See section~\ref{context-closing} for details.}
+  
 \end{methoddesc}
 
 \begin{methoddesc}[file]{flush}{}
index 4914948d6fb9a485fb2ff7952fdd6a25f1a37d86..9e0c2025b161f92641dccadcd39dba67be27cfe1 100644 (file)
@@ -100,6 +100,19 @@ Return the status of the lock:\ \code{True} if it has been acquired by
 some thread, \code{False} if not.
 \end{methoddesc}
 
+In addition to these methods, lock objects can also be used via the
+\keyword{with} statement, e.g.:
+
+\begin{verbatim}
+from __future__ import with_statement
+import thread
+
+a_lock = thread.allocate_lock()
+
+with a_lock:
+    print "a_lock is locked while this executes"
+\end{verbatim}
+
 \strong{Caveats:}
 
 \begin{itemize}
index 33839a4306a130d8fd08c9e30e2d2036bcbc9886..8fb3137744f99b88dc99fc95cacf2c6c244ca0a3 100644 (file)
@@ -675,3 +675,26 @@ keyword arguments \var{kwargs}, after \var{interval} seconds have passed.
 Stop the timer, and cancel the execution of the timer's action.  This
 will only work if the timer is still in its waiting stage.
 \end{methoddesc}
+
+\subsection{Using locks, conditions, and semaphores in the \keyword{with}
+statement \label{with-locks}}
+
+All of the objects provided by this module that have \method{acquire()} and
+\method{release()} methods can be used as context managers for a \keyword{with}
+statement.  The \method{acquire()} method will be called when the block is
+entered, and \method{release()} will be called when the block is exited.
+
+Currently, \class{Lock}, \class{RLock}, \class{Condition}, \class{Semaphore},
+and \class{BoundedSemaphore} objects may be used as \keyword{with}
+statement context managers.  For example:
+
+\begin{verbatim}
+from __future__ import with_statement
+import threading
+
+some_rlock = threading.RLock()
+
+with some_rlock:
+    print "some_rlock is locked while this executes"
+\end{verbatim}
+