Skip to content

Commit 9ecfb20

Browse files
committed
Fix _interpreters.set___main___attrs SystemError when passing non-dict
1 parent b3ae769 commit 9ecfb20

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Lib/test/test__interpreters.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,10 @@ def test_signatures(self):
485485
msg = r'_interpreters.run_func\(\) argument 3 must be dict, not int'
486486
with self.assertRaisesRegex(TypeError, msg):
487487
_interpreters.run_func(self.id, lambda: None, shared=1)
488+
# See https://github.com/python/cpython/issues/135855
489+
msg = r'_interpreters.set___main___attrs\(\) argument 2 must be dict, not int'
490+
with self.assertRaisesRegex(TypeError, msg):
491+
_interpreters.set___main___attrs(self.id, 1)
488492

489493
def test_invalid_shared_encoding(self):
490494
# See https://github.com/python/cpython/issues/127196
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Raise :exc:`TypeError` instead of :exc:`SystemError` when
2+
:func:`!_interpreters.set___main___attrs` is passed a non-dict object.
3+
Patch by Brian Schubert.

Modules/_interpretersmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,8 +1039,8 @@ interp_set___main___attrs(PyObject *self, PyObject *args, PyObject *kwargs)
10391039
PyObject *id, *updates;
10401040
int restricted = 0;
10411041
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
1042-
"OO|$p:" MODULE_NAME_STR ".set___main___attrs",
1043-
kwlist, &id, &updates, &restricted))
1042+
"OO!|$p:" MODULE_NAME_STR ".set___main___attrs",
1043+
kwlist, &id, &PyDict_Type, &updates, &restricted))
10441044
{
10451045
return NULL;
10461046
}
@@ -1055,13 +1055,13 @@ interp_set___main___attrs(PyObject *self, PyObject *args, PyObject *kwargs)
10551055

10561056
// Check the updates.
10571057
if (updates != Py_None) {
1058-
Py_ssize_t size = PyObject_Size(updates);
1058+
Py_ssize_t size = PyDict_Size(updates);
10591059
if (size < 0) {
10601060
return NULL;
10611061
}
10621062
if (size == 0) {
10631063
PyErr_SetString(PyExc_ValueError,
1064-
"arg 2 must be a non-empty mapping");
1064+
"arg 2 must be a non-empty dict");
10651065
return NULL;
10661066
}
10671067
}

0 commit comments

Comments
 (0)