Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions
test.test_contextlib.ContextManagerTestCase Class Reference

List of all members.

Public Member Functions

def test_contextmanager_plain
def test_contextmanager_finally
def test_contextmanager_no_reraise
def test_contextmanager_trap_yield_after_throw
def test_contextmanager_except
def test_contextmanager_attribs
def test_contextmanager_doc_attrib

Private Member Functions

def _create_contextmanager_attribs

Detailed Description

Definition at line 14 of file test_contextlib.py.


Member Function Documentation

Definition at line 85 of file test_contextlib.py.

00085 
00086     def _create_contextmanager_attribs(self):
00087         def attribs(**kw):
00088             def decorate(func):
00089                 for k,v in kw.items():
00090                     setattr(func,k,v)
00091                 return func
00092             return decorate
00093         @contextmanager
00094         @attribs(foo='bar')
00095         def baz(spam):
00096             """Whee!"""
00097         return baz

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 98 of file test_contextlib.py.

00098 
00099     def test_contextmanager_attribs(self):
00100         baz = self._create_contextmanager_attribs()
00101         self.assertEqual(baz.__name__,'baz')
00102         self.assertEqual(baz.foo, 'bar')

Here is the call graph for this function:

Definition at line 105 of file test_contextlib.py.

00105 
00106     def test_contextmanager_doc_attrib(self):
00107         baz = self._create_contextmanager_attribs()
00108         self.assertEqual(baz.__doc__, "Whee!")

Here is the call graph for this function:

Definition at line 68 of file test_contextlib.py.

00068 
00069     def test_contextmanager_except(self):
00070         state = []
00071         @contextmanager
00072         def woohoo():
00073             state.append(1)
00074             try:
00075                 yield 42
00076             except ZeroDivisionError as e:
00077                 state.append(e.args[0])
00078                 self.assertEqual(state, [1, 42, 999])
00079         with woohoo() as x:
00080             self.assertEqual(state, [1])
00081             self.assertEqual(x, 42)
00082             state.append(x)
00083             raise ZeroDivisionError(999)
00084         self.assertEqual(state, [1, 42, 999])

Here is the call graph for this function:

Definition at line 29 of file test_contextlib.py.

00029 
00030     def test_contextmanager_finally(self):
00031         state = []
00032         @contextmanager
00033         def woohoo():
00034             state.append(1)
00035             try:
00036                 yield 42
00037             finally:
00038                 state.append(999)
00039         with self.assertRaises(ZeroDivisionError):
00040             with woohoo() as x:
00041                 self.assertEqual(state, [1])
00042                 self.assertEqual(x, 42)
00043                 state.append(x)
00044                 raise ZeroDivisionError()
00045         self.assertEqual(state, [1, 42, 999])

Here is the call graph for this function:

Definition at line 46 of file test_contextlib.py.

00046 
00047     def test_contextmanager_no_reraise(self):
00048         @contextmanager
00049         def whee():
00050             yield
00051         ctx = whee()
00052         ctx.__enter__()
00053         # Calling __exit__ should not result in an exception
00054         self.assertFalse(ctx.__exit__(TypeError, TypeError("foo"), None))

Definition at line 16 of file test_contextlib.py.

00016 
00017     def test_contextmanager_plain(self):
00018         state = []
00019         @contextmanager
00020         def woohoo():
00021             state.append(1)
00022             yield 42
00023             state.append(999)
00024         with woohoo() as x:
00025             self.assertEqual(state, [1])
00026             self.assertEqual(x, 42)
00027             state.append(x)
00028         self.assertEqual(state, [1, 42, 999])

Here is the call graph for this function:

Definition at line 55 of file test_contextlib.py.

00055 
00056     def test_contextmanager_trap_yield_after_throw(self):
00057         @contextmanager
00058         def whoo():
00059             try:
00060                 yield
00061             except:
00062                 yield
00063         ctx = whoo()
00064         ctx.__enter__()
00065         self.assertRaises(
00066             RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None
00067         )

Here is the call graph for this function:


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