Commit 33f7f6f9 authored by Stefan Behnel's avatar Stefan Behnel

split withstat_py test to make the compatible part run in Py26

--HG--
rename : tests/run/withstat_py.py => tests/run/withstat_py27.py
parent b8e37bc1
...@@ -253,8 +253,9 @@ VER_DEP_MODULES = { ...@@ -253,8 +253,9 @@ VER_DEP_MODULES = {
'memoryview.numpy_memoryview', 'memoryview.numpy_memoryview',
'memoryview.memoryviewattrs', 'memoryview.memoryviewattrs',
'memoryview.memoryview', 'memoryview.memoryview',
'run.withstat_py',
]), ]),
(2,7) : (operator.lt, lambda x: x in ['run.withstat_py', # multi context with statement (2,7) : (operator.lt, lambda x: x in ['run.withstat_py27', # multi context with statement
'run.yield_inside_lambda', 'run.yield_inside_lambda',
'run.test_dictviews', 'run.test_dictviews',
'run.pyclass_special_methods', 'run.pyclass_special_methods',
......
import sys import sys
def typename(t): def typename(t):
name = type(t).__name__ name = type(t).__name__
if sys.version_info < (2,5): if sys.version_info < (2,5):
...@@ -9,9 +10,11 @@ def typename(t): ...@@ -9,9 +10,11 @@ def typename(t):
name = 'MyException' name = 'MyException'
return "<type '%s'>" % name return "<type '%s'>" % name
class MyException(Exception): class MyException(Exception):
pass pass
class ContextManager(object): class ContextManager(object):
def __init__(self, value, exit_ret = None): def __init__(self, value, exit_ret = None):
self.value = value self.value = value
...@@ -25,6 +28,7 @@ class ContextManager(object): ...@@ -25,6 +28,7 @@ class ContextManager(object):
print("enter") print("enter")
return self.value return self.value
def no_as(): def no_as():
""" """
>>> no_as() >>> no_as()
...@@ -35,6 +39,7 @@ def no_as(): ...@@ -35,6 +39,7 @@ def no_as():
with ContextManager("value"): with ContextManager("value"):
print("hello") print("hello")
def basic(): def basic():
""" """
>>> basic() >>> basic()
...@@ -45,6 +50,7 @@ def basic(): ...@@ -45,6 +50,7 @@ def basic():
with ContextManager("value") as x: with ContextManager("value") as x:
print(x) print(x)
def with_pass(): def with_pass():
""" """
>>> with_pass() >>> with_pass()
...@@ -54,6 +60,7 @@ def with_pass(): ...@@ -54,6 +60,7 @@ def with_pass():
with ContextManager("value") as x: with ContextManager("value") as x:
pass pass
def with_return(): def with_return():
""" """
>>> print(with_return()) >>> print(with_return())
...@@ -64,6 +71,7 @@ def with_return(): ...@@ -64,6 +71,7 @@ def with_return():
with ContextManager("value") as x: with ContextManager("value") as x:
return x return x
def with_break(): def with_break():
""" """
>>> print(with_break()) >>> print(with_break())
...@@ -77,6 +85,7 @@ def with_break(): ...@@ -77,6 +85,7 @@ def with_break():
print("FAILED") print("FAILED")
return c return c
def with_continue(): def with_continue():
""" """
>>> print(with_continue()) >>> print(with_continue())
...@@ -94,6 +103,7 @@ def with_continue(): ...@@ -94,6 +103,7 @@ def with_continue():
print("FAILED") print("FAILED")
return c return c
def with_exception(exit_ret): def with_exception(exit_ret):
""" """
>>> with_exception(None) >>> with_exception(None)
...@@ -113,6 +123,7 @@ def with_exception(exit_ret): ...@@ -113,6 +123,7 @@ def with_exception(exit_ret):
except: except:
print("outer except") print("outer except")
def functions_in_with(): def functions_in_with():
""" """
>>> f = functions_in_with() >>> f = functions_in_with()
...@@ -133,6 +144,7 @@ def functions_in_with(): ...@@ -133,6 +144,7 @@ def functions_in_with():
print("outer except") print("outer except")
return f return f
def multitarget(): def multitarget():
""" """
>>> multitarget() >>> multitarget()
...@@ -143,6 +155,7 @@ def multitarget(): ...@@ -143,6 +155,7 @@ def multitarget():
with ContextManager((1, 2, (3, (4, 5)))) as (a, b, (c, (d, e))): with ContextManager((1, 2, (3, (4, 5)))) as (a, b, (c, (d, e))):
print('%s %s %s %s %s' % (a, b, c, d, e)) print('%s %s %s %s %s' % (a, b, c, d, e))
def tupletarget(): def tupletarget():
""" """
>>> tupletarget() >>> tupletarget()
...@@ -153,39 +166,12 @@ def tupletarget(): ...@@ -153,39 +166,12 @@ def tupletarget():
with ContextManager((1, 2, (3, (4, 5)))) as t: with ContextManager((1, 2, (3, (4, 5)))) as t:
print(t) print(t)
def multimanager():
"""
>>> multimanager()
enter
enter
enter
enter
enter
enter
2
value
1 2 3 4 5
nested
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
"""
with ContextManager(1), ContextManager(2) as x, ContextManager('value') as y,\
ContextManager(3), ContextManager((1, 2, (3, (4, 5)))) as (a, b, (c, (d, e))):
with ContextManager('nested') as nested:
print(x)
print(y)
print('%s %s %s %s %s' % (a, b, c, d, e))
print(nested)
class GetManager(object): class GetManager(object):
def get(self, *args): def get(self, *args):
return ContextManager(*args) return ContextManager(*args)
def manager_from_expression(): def manager_from_expression():
""" """
>>> manager_from_expression() >>> manager_from_expression()
...@@ -201,94 +187,3 @@ def manager_from_expression(): ...@@ -201,94 +187,3 @@ def manager_from_expression():
g = GetManager() g = GetManager()
with g.get(2) as x: with g.get(2) as x:
print(x) print(x)
# Tests borrowed from pyregr test_with.py,
# modified to follow the constraints of Cython.
import unittest
class Dummy(object):
def __init__(self, value=None, gobble=False):
if value is None:
value = self
self.value = value
self.gobble = gobble
self.enter_called = False
self.exit_called = False
def __enter__(self):
self.enter_called = True
return self.value
def __exit__(self, *exc_info):
self.exit_called = True
self.exc_info = exc_info
if self.gobble:
return True
class InitRaises(object):
def __init__(self): raise RuntimeError()
class EnterRaises(object):
def __enter__(self): raise RuntimeError()
def __exit__(self, *exc_info): pass
class ExitRaises(object):
def __enter__(self): pass
def __exit__(self, *exc_info): raise RuntimeError()
class NestedWith(unittest.TestCase):
"""
>>> NestedWith().runTest()
"""
def runTest(self):
self.testNoExceptions()
self.testExceptionInExprList()
self.testExceptionInEnter()
self.testExceptionInExit()
self.testEnterReturnsTuple()
def testNoExceptions(self):
with Dummy() as a, Dummy() as b:
self.assertTrue(a.enter_called)
self.assertTrue(b.enter_called)
self.assertTrue(a.exit_called)
self.assertTrue(b.exit_called)
def testExceptionInExprList(self):
try:
with Dummy() as a, InitRaises():
pass
except:
pass
self.assertTrue(a.enter_called)
self.assertTrue(a.exit_called)
def testExceptionInEnter(self):
try:
with Dummy() as a, EnterRaises():
self.fail('body of bad with executed')
except RuntimeError:
pass
else:
self.fail('RuntimeError not reraised')
self.assertTrue(a.enter_called)
self.assertTrue(a.exit_called)
def testExceptionInExit(self):
body_executed = False
with Dummy(gobble=True) as a, ExitRaises():
body_executed = True
self.assertTrue(a.enter_called)
self.assertTrue(a.exit_called)
self.assertTrue(body_executed)
self.assertNotEqual(a.exc_info[0], None)
def testEnterReturnsTuple(self):
with Dummy(value=(1,2)) as (a1, a2), \
Dummy(value=(10, 20)) as (b1, b2):
self.assertEqual(1, a1)
self.assertEqual(2, a2)
self.assertEqual(10, b1)
self.assertEqual(20, b2)
import sys
def typename(t):
name = type(t).__name__
if sys.version_info < (2,5):
if name == 'classobj' and issubclass(t, MyException):
name = 'type'
elif name == 'instance' and isinstance(t, MyException):
name = 'MyException'
return "<type '%s'>" % name
class MyException(Exception):
pass
class ContextManager(object):
def __init__(self, value, exit_ret = None):
self.value = value
self.exit_ret = exit_ret
def __exit__(self, a, b, tb):
print("exit %s %s %s" % (typename(a), typename(b), typename(tb)))
return self.exit_ret
def __enter__(self):
print("enter")
return self.value
def multimanager():
"""
>>> multimanager()
enter
enter
enter
enter
enter
enter
2
value
1 2 3 4 5
nested
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
"""
with ContextManager(1), ContextManager(2) as x, ContextManager('value') as y,\
ContextManager(3), ContextManager((1, 2, (3, (4, 5)))) as (a, b, (c, (d, e))):
with ContextManager('nested') as nested:
print(x)
print(y)
print('%s %s %s %s %s' % (a, b, c, d, e))
print(nested)
class GetManager(object):
def get(self, *args):
return ContextManager(*args)
def manager_from_expression():
"""
>>> manager_from_expression()
enter
1
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
enter
2
exit <type 'NoneType'> <type 'NoneType'> <type 'NoneType'>
"""
with GetManager().get(1) as x:
print(x)
g = GetManager()
with g.get(2) as x:
print(x)
# Tests borrowed from pyregr test_with.py,
# modified to follow the constraints of Cython.
import unittest
class Dummy(object):
def __init__(self, value=None, gobble=False):
if value is None:
value = self
self.value = value
self.gobble = gobble
self.enter_called = False
self.exit_called = False
def __enter__(self):
self.enter_called = True
return self.value
def __exit__(self, *exc_info):
self.exit_called = True
self.exc_info = exc_info
if self.gobble:
return True
class InitRaises(object):
def __init__(self): raise RuntimeError()
class EnterRaises(object):
def __enter__(self): raise RuntimeError()
def __exit__(self, *exc_info): pass
class ExitRaises(object):
def __enter__(self): pass
def __exit__(self, *exc_info): raise RuntimeError()
class NestedWith(unittest.TestCase):
"""
>>> NestedWith().runTest()
"""
def runTest(self):
self.testNoExceptions()
self.testExceptionInExprList()
self.testExceptionInEnter()
self.testExceptionInExit()
self.testEnterReturnsTuple()
def testNoExceptions(self):
with Dummy() as a, Dummy() as b:
self.assertTrue(a.enter_called)
self.assertTrue(b.enter_called)
self.assertTrue(a.exit_called)
self.assertTrue(b.exit_called)
def testExceptionInExprList(self):
try:
with Dummy() as a, InitRaises():
pass
except:
pass
self.assertTrue(a.enter_called)
self.assertTrue(a.exit_called)
def testExceptionInEnter(self):
try:
with Dummy() as a, EnterRaises():
self.fail('body of bad with executed')
except RuntimeError:
pass
else:
self.fail('RuntimeError not reraised')
self.assertTrue(a.enter_called)
self.assertTrue(a.exit_called)
def testExceptionInExit(self):
body_executed = False
with Dummy(gobble=True) as a, ExitRaises():
body_executed = True
self.assertTrue(a.enter_called)
self.assertTrue(a.exit_called)
self.assertTrue(body_executed)
self.assertNotEqual(a.exc_info[0], None)
def testEnterReturnsTuple(self):
with Dummy(value=(1,2)) as (a1, a2), \
Dummy(value=(10, 20)) as (b1, b2):
self.assertEqual(1, a1)
self.assertEqual(2, a2)
self.assertEqual(10, b1)
self.assertEqual(20, b2)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment