8000 [3.14] gh-135855: Raise TypeError When Passing Non-dict Object to `_i… · python/cpython@090a42b · GitHub
[go: up one dir, main page]

Skip to content

Commit 090a42b

Browse files
[3.14] gh-135855: Raise TypeError When Passing Non-dict Object to _interpreters.set___main___attrs (gh-135900)
(cherry picked from commit 4e6f0d1, AKA gh-135856) Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
1 parent fa74331 commit 090a42b

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

Lib/test/test__interpreters.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,21 @@ 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)
492+
493+
def test_invalid_shared_none(self):
494+
msg = r'must be dict, not None'
495+
with self.assertRaisesRegex(TypeError, msg):
496+
_interpreters.exec(self.id, 'a', shared=None)
497+
with self.assertRaisesRegex(TypeError, msg):
498+
_interpreters.run_string(self.id, 'a', shared=None)
499+
with self.assertRaisesRegex(TypeError, msg):
500+
_interpreters.run_func(self.id, lambda: None, shared=None)
501+
with self.assertRaisesRegex(TypeError, msg):
502+
_interpreters.set___main___attrs(self.id, None)
488503

489504
def test_invalid_shared_encoding(self):
490505
# 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: 10 additions & 12 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
}
@@ -1054,16 +1054,14 @@ interp_set___main___attrs(PyObject *self, PyObject *args, PyObject *kwargs)
10541054
}
10551055

10561056
// Check the updates.
1057-
if (updates != Py_None) {
1058-
Py_ssize_t size = PyObject_Size(updates);
1059-
if (size < 0) {
1060-
return NULL;
1061-
}
1062-
if (size == 0) {
1063-
PyErr_SetString(PyExc_ValueError,
1064-
"arg 2 must be a non-empty mapping");
1065-
return NULL;
1066-
}
1057+
Py_ssize_t size = PyDict_Size(updates);
1058+
if (size < 0) {
1059+
return NULL;
1060+
}
1061+
if (size == 0) {
1062+
PyErr_SetString(PyExc_ValueError,
1063+
"arg 2 must be a non-empty dict");
1064+
return NULL;
10671065
}
10681066

10691067
_PyXI_session *session = _PyXI_NewSession();

Python/crossinterp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2617,6 +2617,7 @@ _PyXI_Enter(_PyXI_session *session,
26172617
// Convert the attrs for cross-interpreter use.
26182618
_PyXI_namespace *sharedns = NULL;
26192619
if (nsupdates != NULL) {
2620+
assert(PyDict_Check(nsupdates));
26202621
Py_ssize_t len = PyDict_Size(nsupdates);
26212622
if (len < 0) {
26222623
if (result != NULL) {

0 commit comments

Comments
 (0)
0