Skip to content

Commit f666849

Browse files
[3.13] gh-142595: add type check for namedtuple call during decimal initialization (GH-142608) (GH-142622)
(cherry picked from commit be5e0dc) Co-authored-by: Sergey B Kirpichev <[email protected]>
1 parent 8ebda57 commit f666849

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
@@ -5922,10 +5922,14 @@ _decimal_exec(PyObject *m)
59225922

59235923
/* DecimalTuple */
59245924
ASSIGN_PTR(collections, PyImport_ImportModule("collections"));
5925-
ASSIGN_PTR(state->DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections,
5926-
"namedtuple", "(ss)", "DecimalTuple",
5927-
"sign digits exponent"));
5928-
5925+
obj = PyObject_CallMethod(collections, "namedtuple", "(ss)", "DecimalTuple",
5926+
"sign digits exponent");
5927+
if (!PyType_Check(obj)) {
5928+
PyErr_SetString(PyExc_TypeError,
5929+
"type is expected from namedtuple call");
5930+
goto error;
5931+
}
5932+
ASSIGN_PTR(state->DecimalTuple, (PyTypeObject *)obj);
59295933
ASSIGN_PTR(obj, PyUnicode_FromString("decimal"));
59305934
CHECK_INT(PyDict_SetItemString(state->DecimalTuple->tp_dict, "__module__", obj));
59315935
Py_CLEAR(obj);

0 commit comments

Comments
 (0)