Fix the wrongheaded implementation of context management in the decimal module and add unit tests. (python-dev discussion is ongoing regarding what we do about Python 2.5)
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index f3f9215..6e7faf5 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,43 @@
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_ContextManager(self):
+ # The basic context manager uses the supplied context
+ # without making a copy of it
+ orig_ctx = getcontext()
+ new_ctx = Context()
+ with ContextManager(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_(new_ctx is set_ctx, 'did not set correct context')
+ self.assert_(set_ctx is enter_ctx, '__enter__ returned wrong context')
+
+ def test_localcontext(self):
+ # The helper function makes a copy of the supplied context
+ 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):
+ # The helper function makes a copy of the supplied context
+ 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 +1122,7 @@
DecimalPythonAPItests,
ContextAPItests,
DecimalTest,
+ WithStatementTest,
]
try: