]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-142595: add type check for namedtuple call during decimal initialization...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 15 Dec 2025 14:04:11 +0000 (15:04 +0100)
committerGitHub <noreply@github.com>
Mon, 15 Dec 2025 14:04:11 +0000 (15:04 +0100)
(cherry picked from commit be5e0dcdedb63185aaa3e2f711d14dc828cb3640)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Misc/NEWS.d/next/Library/2025-12-12-02-56-26.gh-issue-142595.wHvTqq.rst [new file with mode: 0644]
Modules/_decimal/_decimal.c

diff --git a/Misc/NEWS.d/next/Library/2025-12-12-02-56-26.gh-issue-142595.wHvTqq.rst b/Misc/NEWS.d/next/Library/2025-12-12-02-56-26.gh-issue-142595.wHvTqq.rst
new file mode 100644 (file)
index 0000000..987e1ae
--- /dev/null
@@ -0,0 +1,2 @@
+Added type check during initialization of the :mod:`decimal` module to
+prevent a crash in case of broken stdlib.  Patch by Sergey B Kirpichev.
index d1fbfd7d30317b3a24aa24edb96ec0e404f8fb52..9736e71efbbe573e32f9c79fb060f4c8fa8956b4 100644 (file)
@@ -5922,10 +5922,14 @@ _decimal_exec(PyObject *m)
 
     /* DecimalTuple */
     ASSIGN_PTR(collections, PyImport_ImportModule("collections"));
-    ASSIGN_PTR(state->DecimalTuple, (PyTypeObject *)PyObject_CallMethod(collections,
-                                 "namedtuple", "(ss)", "DecimalTuple",
-                                 "sign digits exponent"));
-
+    obj = PyObject_CallMethod(collections, "namedtuple", "(ss)", "DecimalTuple",
+                              "sign digits exponent");
+    if (!PyType_Check(obj)) {
+        PyErr_SetString(PyExc_TypeError,
+                        "type is expected from namedtuple call");
+        goto error;
+    }
+    ASSIGN_PTR(state->DecimalTuple, (PyTypeObject *)obj);
     ASSIGN_PTR(obj, PyUnicode_FromString("decimal"));
     CHECK_INT(PyDict_SetItemString(state->DecimalTuple->tp_dict, "__module__", obj));
     Py_CLEAR(obj);