Issue #21374: Fix pickling of DecimalTuple.

This commit is contained in:
Stefan Krah 2014-04-29 18:24:50 +02:00
parent da25109fbc
commit 8fb74a35da
2 changed files with 26 additions and 4 deletions

View File

@ -2431,6 +2431,23 @@ class PythonAPItests(unittest.TestCase):
self.assertIsInstance(r, C.Decimal) self.assertIsInstance(r, C.Decimal)
self.assertEqual(r, x) 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 sys.modules['decimal'] = savedecimal
def test_int(self): def test_int(self):

View File

@ -3542,7 +3542,7 @@ PyDec_Round(PyObject *dec, PyObject *args)
} }
} }
static PyObject *DecimalTuple = NULL; static PyTypeObject *DecimalTuple = NULL;
/* Return the DecimalTuple representation of a PyDecObject. */ /* Return the DecimalTuple representation of a PyDecObject. */
static PyObject * static PyObject *
PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED) PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED)
@ -3625,7 +3625,7 @@ PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED)
} }
} }
result = PyObject_CallFunctionObjArgs(DecimalTuple, result = PyObject_CallFunctionObjArgs((PyObject *)DecimalTuple,
sign, coeff, expt, NULL); sign, coeff, expt, NULL);
out: out:
@ -5562,9 +5562,14 @@ PyInit__decimal(void)
/* DecimalTuple */ /* DecimalTuple */
ASSIGN_PTR(collections, PyImport_ImportModule("collections")); ASSIGN_PTR(collections, PyImport_ImportModule("collections"));
ASSIGN_PTR(DecimalTuple, PyObject_CallMethod(collections, ASSIGN_PTR(DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections,
"namedtuple", "(ss)", "DecimalTuple", "namedtuple", "(ss)", "DecimalTuple",
"sign digits exponent")); "sign digits exponent"));
ASSIGN_PTR(obj, PyUnicode_FromString("decimal"));
CHECK_INT(PyDict_SetItemString(DecimalTuple->tp_dict, "__module__", obj));
Py_CLEAR(obj);
/* MutableMapping */ /* MutableMapping */
ASSIGN_PTR(MutableMapping, PyObject_GetAttrString(collections, ASSIGN_PTR(MutableMapping, PyObject_GetAttrString(collections,
"MutableMapping")); "MutableMapping"));
@ -5591,7 +5596,7 @@ PyInit__decimal(void)
CHECK_INT(PyModule_AddObject(m, "Context", CHECK_INT(PyModule_AddObject(m, "Context",
(PyObject *)&PyDecContext_Type)); (PyObject *)&PyDecContext_Type));
Py_INCREF(DecimalTuple); Py_INCREF(DecimalTuple);
CHECK_INT(PyModule_AddObject(m, "DecimalTuple", DecimalTuple)); CHECK_INT(PyModule_AddObject(m, "DecimalTuple", (PyObject *)DecimalTuple));
/* Create top level exception */ /* Create top level exception */