Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8fb74a3

Browse files
author
Stefan Krah
committed
Issue #21374: Fix pickling of DecimalTuple.
1 parent da25109 commit 8fb74a3

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

Lib/test/test_decimal.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,6 +2431,23 @@ def test_pickle(self):
24312431
self.assertIsInstance(r, C.Decimal)
24322432
self.assertEqual(r, x)
24332433

2434+
x = C.Decimal('-3.123e81723').as_tuple()
2435+
y = P.Decimal('-3.123e81723').as_tuple()
2436+
2437+
sys.modules['decimal'] = C
2438+
sx = pickle.dumps(x)
2439+
sys.modules['decimal'] = P
2440+
r = pickle.loads(sx)
2441+
self.assertIsInstance(r, P.DecimalTuple)
2442+
self.assertEqual(r, y)
2443+
2444+
sys.modules['decimal'] = P
2445+
sy = pickle.dumps(y)
2446+
sys.modules['decimal'] = C
2447+
r = pickle.loads(sy)
2448+
self.assertIsInstance(r, C.DecimalTuple)
2449+
self.assertEqual(r, x)
2450+
24342451
sys.modules['decimal'] = savedecimal
24352452

24362453
def test_int(self):

Modules/_decimal/_decimal.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,7 +3542,7 @@ PyDec_Round(PyObject *dec, PyObject *args)
35423542
}
35433543
}
35443544

3545-
static PyObject *DecimalTuple = NULL;
3545+
static PyTypeObject *DecimalTuple = NULL;
35463546
/* Return the DecimalTuple representation of a PyDecObject. */
35473547
static PyObject *
35483548
PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED)
@@ -3625,7 +3625,7 @@ PyDec_AsTuple(PyObject *dec, PyObject *dummy UNUSED)
36253625
}
36263626
}
36273627

3628-
result = PyObject_CallFunctionObjArgs(DecimalTuple,
3628+
result = PyObject_CallFunctionObjArgs((PyObject *)DecimalTuple,
36293629
sign, coeff, expt, NULL);
36303630

36313631
out:
@@ -5562,9 +5562,14 @@ PyInit__decimal(void)
55625562

55635563
/* DecimalTuple */
55645564
ASSIGN_PTR(collections, PyImport_ImportModule("collections"));
5565-
ASSIGN_PTR(DecimalTuple, PyObject_CallMethod(collections,
5565+
ASSIGN_PTR(DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections,
55665566
"namedtuple", "(ss)", "DecimalTuple",
55675567
"sign digits exponent"));
5568+
5569+
ASSIGN_PTR(obj, PyUnicode_FromString("decimal"));
5570+
CHECK_INT(PyDict_SetItemString(DecimalTuple->tp_dict, "__module__", obj));
5571+
Py_CLEAR(obj);
5572+
55685573
/* MutableMapping */
55695574
ASSIGN_PTR(MutableMapping, PyObject_GetAttrString(collections,
55705575
"MutableMapping"));
@@ -5591,7 +5596,7 @@ PyInit__decimal(void)
55915596
CHECK_INT(PyModule_AddObject(m, "Context",
55925597
(PyObject *)&PyDecContext_Type));
55935598
Py_INCREF(DecimalTuple);
5594-
CHECK_INT(PyModule_AddObject(m, "DecimalTuple", DecimalTuple));
5599+
CHECK_INT(PyModule_AddObject(m, "DecimalTuple", (PyObject *)DecimalTuple));
55955600

55965601

55975602
/* Create top level exception */

0 commit comments

Comments
 (0)