]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-135855: Raise TypeError When Passing Non-dict Object to `_interpreters.set___main_...
authorBrian Schubert <brianm.schubert@gmail.com>
Tue, 24 Jun 2025 18:53:14 +0000 (14:53 -0400)
committerGitHub <noreply@github.com>
Tue, 24 Jun 2025 18:53:14 +0000 (12:53 -0600)
Lib/test/test__interpreters.py
Misc/NEWS.d/next/Library/2025-06-23-10-19-11.gh-issue-135855.-J0AGF.rst [new file with mode: 0644]
Modules/_interpretersmodule.c
Python/crossinterp.c

index ad3ebbfdff64a767904e09141331728531487c73..a32d5d81d2bf2da7fd1b5c75810d83c7121bb888 100644 (file)
@@ -485,6 +485,21 @@ class CommonTests(TestBase):
         msg = r'_interpreters.run_func\(\) argument 3 must be dict, not int'
         with self.assertRaisesRegex(TypeError, msg):
             _interpreters.run_func(self.id, lambda: None, shared=1)
+        # See https://github.com/python/cpython/issues/135855
+        msg = r'_interpreters.set___main___attrs\(\) argument 2 must be dict, not int'
+        with self.assertRaisesRegex(TypeError, msg):
+            _interpreters.set___main___attrs(self.id, 1)
+
+    def test_invalid_shared_none(self):
+        msg = r'must be dict, not None'
+        with self.assertRaisesRegex(TypeError, msg):
+            _interpreters.exec(self.id, 'a', shared=None)
+        with self.assertRaisesRegex(TypeError, msg):
+            _interpreters.run_string(self.id, 'a', shared=None)
+        with self.assertRaisesRegex(TypeError, msg):
+            _interpreters.run_func(self.id, lambda: None, shared=None)
+        with self.assertRaisesRegex(TypeError, msg):
+            _interpreters.set___main___attrs(self.id, None)
 
     def test_invalid_shared_encoding(self):
         # See https://github.com/python/cpython/issues/127196
diff --git a/Misc/NEWS.d/next/Library/2025-06-23-10-19-11.gh-issue-135855.-J0AGF.rst b/Misc/NEWS.d/next/Library/2025-06-23-10-19-11.gh-issue-135855.-J0AGF.rst
new file mode 100644 (file)
index 0000000..fcf495b
--- /dev/null
@@ -0,0 +1,3 @@
+Raise :exc:`TypeError` instead of :exc:`SystemError` when
+:func:`!_interpreters.set___main___attrs` is passed a non-dict object.
+Patch by Brian Schubert.
index e201ccd53daf8a11304db64b231879c64b6ff6e6..b920c32474f4e62b6c3662a65177e71ef6862931 100644 (file)
@@ -1039,8 +1039,8 @@ interp_set___main___attrs(PyObject *self, PyObject *args, PyObject *kwargs)
     PyObject *id, *updates;
     int restricted = 0;
     if (!PyArg_ParseTupleAndKeywords(args, kwargs,
-                                     "OO|$p:" MODULE_NAME_STR ".set___main___attrs",
-                                     kwlist, &id, &updates, &restricted))
+                                     "OO!|$p:" MODULE_NAME_STR ".set___main___attrs",
+                                     kwlist, &id, &PyDict_Type, &updates, &restricted))
     {
         return NULL;
     }
@@ -1054,16 +1054,14 @@ interp_set___main___attrs(PyObject *self, PyObject *args, PyObject *kwargs)
     }
 
     // Check the updates.
-    if (updates != Py_None) {
-        Py_ssize_t size = PyObject_Size(updates);
-        if (size < 0) {
-            return NULL;
-        }
-        if (size == 0) {
-            PyErr_SetString(PyExc_ValueError,
-                            "arg 2 must be a non-empty mapping");
-            return NULL;
-        }
+    Py_ssize_t size = PyDict_Size(updates);
+    if (size < 0) {
+        return NULL;
+    }
+    if (size == 0) {
+        PyErr_SetString(PyExc_ValueError,
+                        "arg 2 must be a non-empty dict");
+        return NULL;
     }
 
     _PyXI_session *session = _PyXI_NewSession();
index c44bcd559000b3f9a82bd020c927c142fe1f5800..16a23f0351cd263966a03c5151d77d8b43dbb0f4 100644 (file)
@@ -2617,6 +2617,7 @@ _PyXI_Enter(_PyXI_session *session,
     // Convert the attrs for cross-interpreter use.
     _PyXI_namespace *sharedns = NULL;
     if (nsupdates != NULL) {
+        assert(PyDict_Check(nsupdates));
         Py_ssize_t len = PyDict_Size(nsupdates);
         if (len < 0) {
             if (result != NULL) {