diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2006-05-03 13:02:47 +0000 |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2006-05-03 13:02:47 +0000 |
commit | afd5e63e243b600e5344a34760d9e6565dafe1a9 (patch) | |
tree | 6fdd6c7dd056fd5d4ce1e67e7d0b430b374dd270 /Lib/test | |
parent | 1b06a1d4e30729434630e9fa37b041926a5766f3 (diff) | |
download | cpython-git-afd5e63e243b600e5344a34760d9e6565dafe1a9.tar.gz |
Finish bringing SVN into line with latest version of PEP 343 by getting rid of all remaining references to context objects that I could find. Without a __context__() method context objects no longer exist. Also get test_with working again, and adopt a suggestion from Neal for decimal.Context.get_manager()
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_contextlib.py | 50 | ||||
-rw-r--r-- | Lib/test/test_with.py | 15 |
2 files changed, 33 insertions, 32 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 8d3dd1a72e..2cf39ae667 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -13,9 +13,9 @@ from test.test_support import run_suite class ContextManagerTestCase(unittest.TestCase): - def test_contextfactory_plain(self): + def test_contextmanager_plain(self): state = [] - @contextfactory + @contextmanager def woohoo(): state.append(1) yield 42 @@ -26,9 +26,9 @@ class ContextManagerTestCase(unittest.TestCase): state.append(x) self.assertEqual(state, [1, 42, 999]) - def test_contextfactory_finally(self): + def test_contextmanager_finally(self): state = [] - @contextfactory + @contextmanager def woohoo(): state.append(1) try: @@ -47,8 +47,8 @@ class ContextManagerTestCase(unittest.TestCase): self.fail("Expected ZeroDivisionError") self.assertEqual(state, [1, 42, 999]) - def test_contextfactory_no_reraise(self): - @contextfactory + def test_contextmanager_no_reraise(self): + @contextmanager def whee(): yield ctx = whee() @@ -56,8 +56,8 @@ class ContextManagerTestCase(unittest.TestCase): # Calling __exit__ should not result in an exception self.failIf(ctx.__exit__(TypeError, TypeError("foo"), None)) - def test_contextfactory_trap_yield_after_throw(self): - @contextfactory + def test_contextmanager_trap_yield_after_throw(self): + @contextmanager def whoo(): try: yield @@ -69,9 +69,9 @@ class ContextManagerTestCase(unittest.TestCase): RuntimeError, ctx.__exit__, TypeError, TypeError("foo"), None ) - def test_contextfactory_except(self): + def test_contextmanager_except(self): state = [] - @contextfactory + @contextmanager def woohoo(): state.append(1) try: @@ -86,14 +86,14 @@ class ContextManagerTestCase(unittest.TestCase): raise ZeroDivisionError(999) self.assertEqual(state, [1, 42, 999]) - def test_contextfactory_attribs(self): + def test_contextmanager_attribs(self): def attribs(**kw): def decorate(func): for k,v in kw.items(): setattr(func,k,v) return func return decorate - @contextfactory + @contextmanager @attribs(foo='bar') def baz(spam): """Whee!""" @@ -106,13 +106,13 @@ class NestedTestCase(unittest.TestCase): # XXX This needs more work def test_nested(self): - @contextfactory + @contextmanager def a(): yield 1 - @contextfactory + @contextmanager def b(): yield 2 - @contextfactory + @contextmanager def c(): yield 3 with nested(a(), b(), c()) as (x, y, z): @@ -122,14 +122,14 @@ class NestedTestCase(unittest.TestCase): def test_nested_cleanup(self): state = [] - @contextfactory + @contextmanager def a(): state.append(1) try: yield 2 finally: state.append(3) - @contextfactory + @contextmanager def b(): state.append(4) try: @@ -148,7 +148,7 @@ class NestedTestCase(unittest.TestCase): def test_nested_right_exception(self): state = [] - @contextfactory + @contextmanager def a(): yield 1 class b(object): @@ -170,10 +170,10 @@ class NestedTestCase(unittest.TestCase): self.fail("Didn't raise ZeroDivisionError") def test_nested_b_swallows(self): - @contextfactory + @contextmanager def a(): yield - @contextfactory + @contextmanager def b(): try: yield @@ -187,7 +187,7 @@ class NestedTestCase(unittest.TestCase): self.fail("Didn't swallow ZeroDivisionError") def test_nested_break(self): - @contextfactory + @contextmanager def a(): yield state = 0 @@ -199,7 +199,7 @@ class NestedTestCase(unittest.TestCase): self.assertEqual(state, 1) def test_nested_continue(self): - @contextfactory + @contextmanager def a(): yield state = 0 @@ -211,7 +211,7 @@ class NestedTestCase(unittest.TestCase): self.assertEqual(state, 3) def test_nested_return(self): - @contextfactory + @contextmanager def a(): try: yield @@ -339,12 +339,12 @@ class DecimalContextTestCase(unittest.TestCase): orig_context = ctx.copy() try: ctx.prec = save_prec = decimal.ExtendedContext.prec + 5 - with decimal.ExtendedContext.context_manager(): + with decimal.ExtendedContext.get_manager(): self.assertEqual(decimal.getcontext().prec, decimal.ExtendedContext.prec) self.assertEqual(decimal.getcontext().prec, save_prec) try: - with decimal.ExtendedContext.context_manager(): + with decimal.ExtendedContext.get_manager(): self.assertEqual(decimal.getcontext().prec, decimal.ExtendedContext.prec) 1/0 diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py index 765bfec397..57505085dd 100644 --- a/Lib/test/test_with.py +++ b/Lib/test/test_with.py @@ -10,25 +10,26 @@ __email__ = "mbland at acm dot org" import sys import unittest from collections import deque -from contextlib import GeneratorContext, contextfactory +from contextlib import GeneratorContextManager, contextmanager from test.test_support import run_unittest -class MockContextManager(GeneratorContext): +class MockContextManager(GeneratorContextManager): def __init__(self, gen): - GeneratorContext.__init__(self, gen) + GeneratorContextManager.__init__(self, gen) self.enter_called = False self.exit_called = False self.exit_args = None def __enter__(self): self.enter_called = True - return GeneratorContext.__enter__(self) + return GeneratorContextManager.__enter__(self) def __exit__(self, type, value, traceback): self.exit_called = True self.exit_args = (type, value, traceback) - return GeneratorContext.__exit__(self, type, value, traceback) + return GeneratorContextManager.__exit__(self, type, + value, traceback) def mock_contextmanager(func): @@ -439,7 +440,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin): self.assertAfterWithGeneratorInvariantsNoError(self.bar) def testRaisedStopIteration1(self): - @contextfactory + @contextmanager def cm(): yield @@ -463,7 +464,7 @@ class ExceptionalTestCase(unittest.TestCase, ContextmanagerAssertionMixin): self.assertRaises(StopIteration, shouldThrow) def testRaisedGeneratorExit1(self): - @contextfactory + @contextmanager def cm(): yield |