[Python-checkins] cpython (3.4): Issue #21374: Fix pickling of DecimalTuple.
stefan.krah
python-checkins at python.org
Tue Apr 29 18:26:19 CEST 2014
http://hg.python.org/cpython/rev/25919f35241e
changeset: 90521:25919f35241e
branch: 3.4
parent: 90518:cc2345e6e9ff
user: Stefan Krah <skrah at bytereef.org>
date: Tue Apr 29 18:24:50 2014 +0200
summary:
Issue #21374: Fix pickling of DecimalTuple.
files:
Lib/test/test_decimal.py | 17 +++++++++++++++++
Modules/_decimal/_decimal.c | 13 +++++++++----
2 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -2431,6 +2431,23 @@
self.assertIsInstance(r, C.Decimal)
self.assertEqual(r, x)
+ x = C.Decimal('-3.123e81723').as_tuple()
+ y = P.Decimal('-3.123e81723').as_tuple()
+
+ sys.modules['decimal'] = C
+ sx = pickle.dumps(x)
+ sys.modules['decimal'] = P
+ r = pickle.loads(sx)
+ self.assertIsInstance(r, P.DecimalTuple)
+ self.assertEqual(r, y)
+
+ sys.modules['decimal'] = P
+ sy = pickle.dumps(y)
+ sys.modules['decimal'] = C
+ r = pickle.loads(sy)
+ self.assertIsInstance(r, C.DecimalTuple)
+ self.assertEqual(r, x)
+
sys.modules['decimal'] = savedecimal
def test_int(self):
diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -3542,7 +3542,7 @@
}
}
-static PyObject *DecimalTuple = NULL;
+static PyTypeObject *DecimalTuple = NULL;
/* Return the DecimalTuple representation of a PyDecObject. */
static PyObject *
PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED)
@@ -3625,7 +3625,7 @@
}
}
- result = PyObject_CallFunctionObjArgs(DecimalTuple,
+ result = PyObject_CallFunctionObjArgs((PyObject *)DecimalTuple,
sign, coeff, expt, NULL);
out:
@@ -5562,9 +5562,14 @@
/* DecimalTuple */
ASSIGN_PTR(collections, PyImport_ImportModule("collections"));
- ASSIGN_PTR(DecimalTuple, PyObject_CallMethod(collections,
+ ASSIGN_PTR(DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections,
"namedtuple", "(ss)", "DecimalTuple",
"sign digits exponent"));
+
+ ASSIGN_PTR(obj, PyUnicode_FromString("decimal"));
+ CHECK_INT(PyDict_SetItemString(DecimalTuple->tp_dict, "__module__", obj));
+ Py_CLEAR(obj);
+
/* MutableMapping */
ASSIGN_PTR(MutableMapping, PyObject_GetAttrString(collections,
"MutableMapping"));
@@ -5591,7 +5596,7 @@
CHECK_INT(PyModule_AddObject(m, "Context",
(PyObject *)&PyDecContext_Type));
Py_INCREF(DecimalTuple);
- CHECK_INT(PyModule_AddObject(m, "DecimalTuple", DecimalTuple));
+ CHECK_INT(PyModule_AddObject(m, "DecimalTuple", (PyObject *)DecimalTuple));
/* Create top level exception */
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list