Andrew Kuchling committed 8863fed

Rework context terminology

Comments (0)

Files changed (1)


 \item The expression is evaluated and should result in an object
-with a \method{__context__()} method (called a ``context specifier'').
+with a \method{__context__()} method (called a ``context manager'').
 \item The context specifier's \method{__context__()} method is called, 
-and must return another object (called a ``context manager'') that has
+and must return another object (called a ``with-statement context object'') that has
 \method{__enter__()} and \method{__exit__()} methods.
-\item The context manager's \method{__enter__()} method is called.  The value
+\item The context object's \method{__enter__()} method is called.  The value
 returned is assigned to \var{VAR}.  If no \code{'as \var{VAR}'} clause
 is present, the value is simply discarded.
 \function{sys.exc_info()}.  The method's return value controls whether
 the exception is re-raised: any false value re-raises the exception,
 and \code{True} will result in suppressing it.  You'll only rarely
-want to suppress the exception; the author of the code containing the
+want to suppress the exception, because if you do
+the author of the code containing the
 '\keyword{with}' statement will never realize anything went wrong.
 \item If \var{BLOCK} didn't raise an exception, 
 method.  Sometimes an object can simply return \code{self}; the
 \module{threading} module's lock objects do this, for example.  For
 our database example, though, we need to create a new object; I'll
-call this class \class{DatabaseContextMgr}.  Our \method{__context__()}
+call this class \class{DatabaseContext}.  Our \method{__context__()}
 method must therefore look like this:
 class DatabaseConnection:
     def __context__ (self):
-        return DatabaseContextMgr(self)
+        return DatabaseContext(self)
     # Database interface
     def cursor (self):
         "Rolls back current transaction"
-Instance of \class{DatabaseContextMgr} need the connection object so that
+Instances of \class{DatabaseContext} need the connection object so that
 the connection object's \method{commit()} or \method{rollback()}
 methods can be called:
-class DatabaseContextMgr:
+class DatabaseContext:
     def __init__ (self, connection):
         self.connection = connection
 the cursor to a variable name.
-class DatabaseContextMgr:
+class DatabaseContext:
     def __enter__ (self):
         # Code to start a new transaction
 statement at the marked location.
-class DatabaseContextMgr:
+class DatabaseContext:
     def __exit__ (self, type, value, tb):
         if tb is None:
 decorator that are useful for writing objects for use with the
 '\keyword{with}' statement.
-The decorator is called \function{contextmanager}, and lets you write
-a simple context manager as a generator function.  The generator
+The decorator is called \function{contextfactory}, and lets you write
+a single generator function instead of defining a new class.  The generator
 should yield exactly one value.  The code up to the \keyword{yield}
 will be executed as the \method{__enter__()} method, and the value
 yielded will be the method's return value that will get bound to the
 using this decorator as:
-from contextlib import contextmanager
+from contextlib import contextfactory
 def db_transaction (connection):
     cursor = connection.cursor()
 You can also use this decorator to write the \method{__context__()}
-method for a class without having to create a new class representing
-the context manager:
+method for a class:
 class DatabaseConnection:
-    @contextmanager
+    @contextfactory
     def __context__ (self):
 	cursor = self.cursor()
-There's a \function{nested(\var{mgr1}, \var{mgr2}, ...)} function that
-combines a number of contexts so you don't need to write
-nested '\keyword{with}' statements.  This example statement does two
-things, starting a database transaction and acquiring a thread lock:
+The \module{contextlib} module also has a \function{nested(\var{mgr1},
+\var{mgr2}, ...)} function that combines a number of contexts so you
+don't need to write nested '\keyword{with}' statements.  In this
+example, the single '\keyword{with}' statement both starts a database
+transaction and acquires a thread lock:
 lock = threading.Lock()