Backport of decimal module context management updates from rev 51694 to 2.5 release branch
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py
index 2cf39ae..747785d 100644
--- a/Lib/test/test_contextlib.py
+++ b/Lib/test/test_contextlib.py
@@ -330,32 +330,6 @@
return True
self.boilerPlate(lock, locked)
-class DecimalContextTestCase(unittest.TestCase):
-
- # XXX Somebody should write more thorough tests for this
-
- def testBasic(self):
- ctx = decimal.getcontext()
- orig_context = ctx.copy()
- try:
- ctx.prec = save_prec = decimal.ExtendedContext.prec + 5
- with decimal.ExtendedContext.get_manager():
- self.assertEqual(decimal.getcontext().prec,
- decimal.ExtendedContext.prec)
- self.assertEqual(decimal.getcontext().prec, save_prec)
- try:
- with decimal.ExtendedContext.get_manager():
- self.assertEqual(decimal.getcontext().prec,
- decimal.ExtendedContext.prec)
- 1/0
- except ZeroDivisionError:
- self.assertEqual(decimal.getcontext().prec, save_prec)
- else:
- self.fail("Didn't raise ZeroDivisionError")
- finally:
- decimal.setcontext(orig_context)
-
-
# This is needed to make the test actually run under regrtest.py!
def test_main():
run_suite(
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index f3f9215..841ea6f 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -23,6 +23,7 @@
you're working through IDLE, you can import this test module and call test_main()
with the corresponding argument.
"""
+from __future__ import with_statement
import unittest
import glob
@@ -1064,6 +1065,32 @@
self.assertNotEqual(id(c.flags), id(d.flags))
self.assertNotEqual(id(c.traps), id(d.traps))
+class WithStatementTest(unittest.TestCase):
+ # Can't do these as docstrings until Python 2.6
+ # as doctest can't handle __future__ statements
+
+ def test_localcontext(self):
+ # Use a copy of the current context in the block
+ orig_ctx = getcontext()
+ with localcontext() as enter_ctx:
+ set_ctx = getcontext()
+ final_ctx = getcontext()
+ self.assert_(orig_ctx is final_ctx, 'did not restore context correctly')
+ self.assert_(orig_ctx is not set_ctx, 'did not copy the context')
+ self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context')
+
+ def test_localcontextarg(self):
+ # Use a copy of the supplied context in the block
+ orig_ctx = getcontext()
+ new_ctx = Context(prec=42)
+ with localcontext(new_ctx) as enter_ctx:
+ set_ctx = getcontext()
+ final_ctx = getcontext()
+ self.assert_(orig_ctx is final_ctx, 'did not restore context correctly')
+ self.assert_(set_ctx.prec == new_ctx.prec, 'did not set correct context')
+ self.assert_(new_ctx is not set_ctx, 'did not copy the context')
+ self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context')
+
def test_main(arith=False, verbose=None):
""" Execute the tests.
@@ -1084,6 +1111,7 @@
DecimalPythonAPItests,
ContextAPItests,
DecimalTest,
+ WithStatementTest,
]
try: