Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
contextlib._GeneratorContextManager Class Reference
Inheritance diagram for contextlib._GeneratorContextManager:
Inheritance graph
Collaboration diagram for contextlib._GeneratorContextManager:
Collaboration graph

List of all members.

Public Member Functions

def __init__
def __enter__
def __exit__
def __call__

Public Attributes

_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Private Member Functions

def _recreate_cm

Detailed Description

Helper for @contextmanager decorator.

Definition at line 33 of file

Constructor & Destructor Documentation

def contextlib._GeneratorContextManager.__init__ (   self,

Reimplemented in test.test_with.MockContextManager.

Definition at line 36 of file

00037     def __init__(self, func, *args, **kwds):
00038         self.gen = func(*args, **kwds)
00039         self.func, self.args, self.kwds = func, args, kwds

Here is the caller graph for this function:

Member Function Documentation

def contextlib.ContextDecorator.__call__ (   self,
) [inherited]

Definition at line 25 of file

00026     def __call__(self, func):
00027         @wraps(func)
00028         def inner(*args, **kwds):
00029             with self._recreate_cm():
00030                 return func(*args, **kwds)
00031         return inner

Here is the call graph for this function:

Reimplemented in test.test_with.MockContextManager.

Definition at line 46 of file

00047     def __enter__(self):
00048         try:
00049             return next(self.gen)
00050         except StopIteration:
00051             raise RuntimeError("generator didn't yield")

def contextlib._GeneratorContextManager.__exit__ (   self,

Reimplemented in test.test_with.MockContextManager.

Definition at line 52 of file

00053     def __exit__(self, type, value, traceback):
00054         if type is None:
00055             try:
00056                 next(self.gen)
00057             except StopIteration:
00058                 return
00059             else:
00060                 raise RuntimeError("generator didn't stop")
00061         else:
00062             if value is None:
00063                 # Need to force instantiation so we can reliably
00064                 # tell if we get the same exception back
00065                 value = type()
00066             try:
00067                 self.gen.throw(type, value, traceback)
00068                 raise RuntimeError("generator didn't stop after throw()")
00069             except StopIteration as exc:
00070                 # Suppress the exception *unless* it's the same exception that
00071                 # was passed to throw().  This prevents a StopIteration
00072                 # raised inside the "with" statement from being suppressed
00073                 return exc is not value
00074             except:
00075                 # only re-raise if it's *not* the exception that was
00076                 # passed to throw(), because __exit__() must not raise
00077                 # an exception unless __exit__() itself failed.  But throw()
00078                 # has to raise the exception to signal propagation, so this
00079                 # fixes the impedance mismatch between the throw() protocol
00080                 # and the __exit__() protocol.
00081                 #
00082                 if sys.exc_info()[1] is not value:
00083                     raise

Here is the caller graph for this function:

Return a recreated instance of self.

Allows otherwise one-shot context managers like
_GeneratorContextManager to support use as
decorators via implicit recreation.

Note: this is a private interface just for _GCM in 3.2 but will be
renamed and documented for third party use in 3.3

Reimplemented from contextlib.ContextDecorator.

Definition at line 40 of file

00041     def _recreate_cm(self):
00042         # _GCM instances are one-shot context managers, so the
00043         # CM must be recreated each time a decorated function is
00044         # called
00045         return self.__class__(self.func, *self.args, **self.kwds)

Member Data Documentation

Definition at line 37 of file

Definition at line 38 of file

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

The documentation for this class was generated from the following file: