1 from __future__ import with_statement
6 name = type(t).__name__
7 if sys.version_info < (2,5):
8 if name == 'classobj' and issubclass(t, MyException):
10 elif name == 'instance' and isinstance(t, MyException):
12 return u"<type '%s'>" % name
14 class MyException(Exception):
17 class ContextManager(object):
18 def __init__(self, value, exit_ret = None):
20 self.exit_ret = exit_ret
22 def __exit__(self, a, b, tb):
23 print u"exit", typename(a), typename(b), typename(tb)
35 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
37 with ContextManager(u"value"):
45 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
47 with ContextManager(u"value") as x:
54 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
56 with ContextManager(u"value") as x:
59 def with_exception(exit_ret):
61 >>> with_exception(None)
64 exit <type 'type'> <type 'MyException'> <type 'traceback'>
66 >>> with_exception(True)
69 exit <type 'type'> <type 'MyException'> <type 'traceback'>
72 with ContextManager(u"value", exit_ret=exit_ret) as value:
83 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
85 with ContextManager((1, 2, (3, (4, 5)))) as (a, b, (c, (d, e))):
93 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
95 with ContextManager((1, 2, (3, (4, 5)))) as t:
103 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
106 c = ContextManager(255)
124 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
125 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
126 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
127 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
128 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
129 exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
131 with ContextManager(1), ContextManager(2) as x, ContextManager(u'value') as y,\
132 ContextManager(3), ContextManager((1, 2, (3, (4, 5)))) as (a, b, (c, (d, e))):
133 with ContextManager(u'nested') as nested:
139 # Tests borrowed from pyregr test_with.py,
140 # modified to follow the constraints of Cython.
144 def __init__(self, value=None, gobble=False):
149 self.enter_called = False
150 self.exit_called = False
153 self.enter_called = True
156 def __exit__(self, *exc_info):
157 self.exit_called = True
158 self.exc_info = exc_info
162 class InitRaises(object):
163 def __init__(self): raise RuntimeError()
165 class EnterRaises(object):
166 def __enter__(self): raise RuntimeError()
167 def __exit__(self, *exc_info): pass
169 class ExitRaises(object):
170 def __enter__(self): pass
171 def __exit__(self, *exc_info): raise RuntimeError()
173 class NestedWith(unittest.TestCase):
175 >>> NestedWith().runTest()
179 self.testNoExceptions()
180 self.testExceptionInExprList()
181 self.testExceptionInEnter()
182 self.testExceptionInExit()
183 self.testEnterReturnsTuple()
185 def testNoExceptions(self):
186 with Dummy() as a, Dummy() as b:
187 self.assertTrue(a.enter_called)
188 self.assertTrue(b.enter_called)
189 self.assertTrue(a.exit_called)
190 self.assertTrue(b.exit_called)
192 def testExceptionInExprList(self):
194 with Dummy() as a, InitRaises():
198 self.assertTrue(a.enter_called)
199 self.assertTrue(a.exit_called)
201 def testExceptionInEnter(self):
203 with Dummy() as a, EnterRaises():
204 self.fail('body of bad with executed')
208 self.fail('RuntimeError not reraised')
209 self.assertTrue(a.enter_called)
210 self.assertTrue(a.exit_called)
212 def testExceptionInExit(self):
213 body_executed = False
214 with Dummy(gobble=True) as a, ExitRaises():
216 self.assertTrue(a.enter_called)
217 self.assertTrue(a.exit_called)
218 self.assertTrue(body_executed)
219 self.assertNotEqual(a.exc_info[0], None)
221 def testEnterReturnsTuple(self):
222 with Dummy(value=(1,2)) as (a1, a2), \
223 Dummy(value=(10, 20)) as (b1, b2):
224 self.assertEquals(1, a1)
225 self.assertEquals(2, a2)
226 self.assertEquals(10, b1)
227 self.assertEquals(20, b2)