diff options
Diffstat (limited to 'Lib/test/test_with.py')
| -rw-r--r-- | Lib/test/test_with.py | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py index 49645183..e8cc8c0 100644 --- a/Lib/test/test_with.py +++ b/Lib/test/test_with.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """Unit tests for the with statement specified in PEP 343.""" @@ -9,31 +9,31 @@ __email__ = "mbland at acm dot org" import sys import unittest from collections import deque -from contextlib import GeneratorContextManager, contextmanager -from test.test_support import run_unittest +from contextlib import _GeneratorContextManager, contextmanager +from test.support import run_unittest -class MockContextManager(GeneratorContextManager): - def __init__(self, gen): - GeneratorContextManager.__init__(self, gen) +class MockContextManager(_GeneratorContextManager): + def __init__(self, func, *args, **kwds): + super().__init__(func, *args, **kwds) self.enter_called = False self.exit_called = False self.exit_args = None def __enter__(self): self.enter_called = True - return GeneratorContextManager.__enter__(self) + return _GeneratorContextManager.__enter__(self) def __exit__(self, type, value, traceback): self.exit_called = True self.exit_args = (type, value, traceback) - return GeneratorContextManager.__exit__(self, type, - value, traceback) + return _GeneratorContextManager.__exit__(self, type, + value, traceback) def mock_contextmanager(func): def helper(*args, **kwds): - return MockContextManager(func(*args, **kwds)) + return MockContextManager(func, *args, **kwds) return helper @@ -86,7 +86,7 @@ class Nested(object): ex = sys.exc_info() self.entered = None if ex is not exc_info: - raise ex[0], ex[1], ex[2] + raise ex[0](ex[1]).with_traceback(ex[2]) class MockNested(Nested): @@ -186,7 +186,9 @@ class FailureTestCase(unittest.TestCase): self.assertRaises(RuntimeError, shouldThrow) class ContextmanagerAssertionMixin(object): - TEST_EXCEPTION = RuntimeError("test exception") + + def setUp(self): + self.TEST_EXCEPTION = RuntimeError("test exception") def assertInWithManagerInvariants(self, mock_manager): self.assertTrue(mock_manager.enter_called) @@ -349,7 +351,7 @@ class NestedNonexceptionalTestCase(unittest.TestCase, self.assertAfterWithManagerInvariantsNoError(mock_nested) -class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin): +class ExceptionalTestCase(ContextmanagerAssertionMixin, unittest.TestCase): def testSingleResource(self): cm = mock_contextmanager_generator() def shouldThrow(): @@ -480,7 +482,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin): def shouldThrow(): with cm(): - raise iter([]).next() + raise next(iter([])) self.assertRaises(StopIteration, shouldThrow) @@ -517,7 +519,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin): class cm(object): def __init__(self, bool_conversion): class Bool: - def __nonzero__(self): + def __bool__(self): return bool_conversion() self.exit_result = Bool() def __enter__(self): @@ -536,7 +538,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin): self.assertRaises(AssertionError, falseAsBool) def failAsBool(): - with cm(lambda: 1 // 0): + with cm(lambda: 1//0): self.fail("Should NOT see this") self.assertRaises(ZeroDivisionError, failAsBool) @@ -603,13 +605,13 @@ class AssignmentTargetTestCase(unittest.TestCase): def testSingleComplexTarget(self): targets = {1: [0, 1, 2]} with mock_contextmanager_generator() as targets[1][0]: - self.assertEqual(targets.keys(), [1]) + self.assertEqual(list(targets.keys()), [1]) self.assertEqual(targets[1][0].__class__, MockResource) - with mock_contextmanager_generator() as targets.values()[0][1]: - self.assertEqual(targets.keys(), [1]) + with mock_contextmanager_generator() as list(targets.values())[0][1]: + self.assertEqual(list(targets.keys()), [1]) self.assertEqual(targets[1][1].__class__, MockResource) with mock_contextmanager_generator() as targets[2]: - keys = targets.keys() + keys = list(targets.keys()) keys.sort() self.assertEqual(keys, [1, 2]) class C: pass @@ -624,7 +626,7 @@ class AssignmentTargetTestCase(unittest.TestCase): targets = {1: [0, 1, 2]} with C() as (targets[1][0], targets[1][1], targets[1][2]): self.assertEqual(targets, {1: [1, 2, 3]}) - with C() as (targets.values()[0][2], targets.values()[0][1], targets.values()[0][0]): + with C() as (list(targets.values())[0][2], list(targets.values())[0][1], list(targets.values())[0][0]): self.assertEqual(targets, {1: [3, 2, 1]}) with C() as (targets[1], targets[2], targets[3]): self.assertEqual(targets, {1: 1, 2: 2, 3: 3}) @@ -644,7 +646,7 @@ class ExitSwallowsExceptionTestCase(unittest.TestCase): def __exit__(self, t, v, tb): return True try: with AfricanSwallow(): - 1 // 0 + 1/0 except ZeroDivisionError: self.fail("ZeroDivisionError should have been swallowed") @@ -654,7 +656,7 @@ class ExitSwallowsExceptionTestCase(unittest.TestCase): def __exit__(self, t, v, tb): return False try: with EuropeanSwallow(): - 1 // 0 + 1/0 except ZeroDivisionError: pass else: |
