summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_decimal.py70
-rw-r--r--Modules/_decimal/_decimal.c8
2 files changed, 78 insertions, 0 deletions
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 694b959..9278dad 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -56,6 +56,76 @@ sys.modules['decimal'] = C
fractions = {C:cfractions, P:pfractions}
sys.modules['decimal'] = orig_sys_decimal
+############ RunFirst ############
+class RunFirst(unittest.TestCase):
+
+ def setUp(self):
+ self.save_default = self.decimal.DefaultContext.copy()
+
+ def tearDown(self):
+ DefaultContext = self.decimal.DefaultContext
+
+ DefaultContext.prec = self.save_default.prec
+ DefaultContext.rounding = self.save_default.rounding
+ DefaultContext.Emax = self.save_default.Emax
+ DefaultContext.Emin = self.save_default.Emin
+ DefaultContext.capitals = self.save_default.capitals
+ DefaultContext.clamp = self.save_default.clamp
+ DefaultContext.flags = self.save_default.flags
+ DefaultContext.traps = self.save_default.traps
+
+ self.decimal.setcontext(self.decimal.DefaultContext)
+
+ def test_00default_context(self):
+ # The test depends on the fact that getcontext() is called
+ # for the first time.
+ DefaultContext = self.decimal.DefaultContext
+ ROUND_05UP = self.decimal.ROUND_05UP
+ Clamped = self.decimal.Clamped
+ InvalidOperation = self.decimal.InvalidOperation
+
+ DefaultContext.prec = 5001
+ DefaultContext.rounding = ROUND_05UP
+ DefaultContext.Emax = 10025
+ DefaultContext.Emin = -10025
+ DefaultContext.capitals = 0
+ DefaultContext.clamp = 1
+ DefaultContext.flags[InvalidOperation] = True
+ DefaultContext.clear_traps()
+ DefaultContext.traps[Clamped] = True
+
+ # implicit initialization on first access
+ c = self.decimal.getcontext()
+
+ self.assertEqual(c.prec, 5001)
+ self.assertEqual(c.rounding, ROUND_05UP)
+ self.assertEqual(c.Emax, 10025)
+ self.assertEqual(c.Emin, -10025)
+ self.assertEqual(c.capitals, 0)
+ self.assertEqual(c.clamp, 1)
+ for k in c.flags:
+ self.assertFalse(c.flags[k])
+ for k in c.traps:
+ if k is Clamped:
+ self.assertTrue(c.traps[k])
+ else:
+ self.assertFalse(c.traps[k])
+
+ # explicit initialization
+ self.decimal.setcontext(DefaultContext)
+ c = self.decimal.getcontext()
+ for k in c.flags:
+ self.assertFalse(c.flags[k])
+
+class CRunFirst(RunFirst):
+ decimal = C
+class PyRunFirst(RunFirst):
+ decimal = P
+if C:
+ run_unittest(CRunFirst, PyRunFirst)
+else:
+ run_unittest(PyRunFirst)
+############ END RunFirst ############
# Useful Test Constant
Signals = {
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index bb2df44..d5d1134 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -1494,6 +1494,10 @@ current_context(void)
}
*CTX(module_context) = *CTX(default_context_template);
+ CTX(module_context)->status = 0;
+ CTX(module_context)->newtrap = 0;
+ CtxCaps(module_context) = CtxCaps(default_context_template);
+
module_context_set = 1;
return module_context;
}
@@ -1533,6 +1537,7 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
if (v == NULL) {
return NULL;
}
+ CTX(v)->status = 0;
}
else {
Py_INCREF(v);
@@ -1581,6 +1586,8 @@ current_context(void)
if (tl_context == NULL) {
return NULL;
}
+ CTX(tl_context)->status = 0;
+
if (PyDict_SetItem(dict, tls_context_key, tl_context) < 0) {
Py_DECREF(tl_context);
return NULL;
@@ -1646,6 +1653,7 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
if (v == NULL) {
return NULL;
}
+ CTX(v)->status = 0;
}
else {
Py_INCREF(v);