Skip to content

Commit 575174e

Browse files
[3.14] gh-142595: add type check for namedtuple call during decimal initialization (GH-142608) (GH-142623)
(cherry picked from commit be5e0dc) Co-authored-by: Sergey B Kirpichev <[email protected]>
1 parent f172817 commit 575174e

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added type check during initialization of the :mod:`decimal` module to
2+
prevent a crash in case of broken stdlib. Patch by Sergey B Kirpichev.

Modules/_decimal/_decimal.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6040,10 +6040,14 @@ _decimal_exec(PyObject *m)
60406040

60416041
/* DecimalTuple */
60426042
ASSIGN_PTR(collections, PyImport_ImportModule("collections"));
6043-
ASSIGN_PTR(state->DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections,
6044-
"namedtuple", "(ss)", "DecimalTuple",
6045-
"sign digits exponent"));
6046-
6043+
obj = PyObject_CallMethod(collections, "namedtuple", "(ss)", "DecimalTuple",
6044+
"sign digits exponent");
6045+
if (!PyType_Check(obj)) {
6046+
PyErr_SetString(PyExc_TypeError,
6047+
"type is expected from namedtuple call");
6048+
goto error;
6049+
}
6050+
ASSIGN_PTR(state->DecimalTuple, (PyTypeObject *)obj);
60476051
ASSIGN_PTR(obj, PyUnicode_FromString("decimal"));
60486052
CHECK_INT(PyDict_SetItemString(state->DecimalTuple->tp_dict, "__module__", obj));
60496053
Py_CLEAR(obj);

0 commit comments

Comments
 (0)