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

Skip to content 8000

Commit 3e43628

Browse files
[3.13] gh-135855: Raise TypeError When Passing Non-dict Object to _interpreters.set___main___attrs (gh-135903)
(cherry picked from commit 4e6f0d1, AKA gh-135856)
1 parent 469f69d commit 3e43628

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

Lib/test/test__interpreters.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,22 @@ def test_signatures(self):
567567
_interpreters.run_string(self.id, 'a', shared=1)
568568
with self.assertRaisesRegex(TypeError, msg):
569569
_interpreters.run_func(self.id, lambda: None, shared=1)
570+
# See https://github.com/python/cpython/issues/135855
571+
msg = r'_interpreters.set___main___attrs\(\) argument 2 must be dict, not int'
572+
with self.assertRaisesRegex(TypeError, msg):
573+
_interpreters.set___main___attrs(self.id, 1)
574+
575+
def test_invalid_shared_none(self):
576+
msg = "expected 'shared' to be a dict"
577+
with self.assertRaisesRegex(TypeError, msg):
578+
_interpreters.exec(self.id, 'a', shared=None)
579+
with self.assertRaisesRegex(TypeError, msg):
580+
_interpreters.run_string(self.id, 'a', shared=None)
581+
with self.assertRaisesRegex(TypeError, msg):
582+
_interpreters.run_func(self.id, lambda: None, shared=None)
583+
msg = "must be dict, not None"
584+
with self.assertRaisesRegex(TypeError, msg):
585+
_interpreters.set___main___attrs(self.id, None)
570586

571587
def test_invalid_shared_encoding(self):
572588
# 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
@@ -811,8 +811,8 @@ interp_set___main___attrs(PyObject *self, PyObject *args, PyObject *kwargs)
811811
PyObject *id, *updates;
812812
int restricted = 0;
813813
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
814-
"OO|$p:" MODULE_NAME_STR ".set___main___attrs",
815-
kwlist, &id, &updates, &restricted))
814+
"OO!|$p:" MODULE_NAME_STR ".set___main___attrs",
815+
kwlist, &id, &PyDict_Type, &updates, &restricted))
816816
{
817817
return NULL;
818818
}
@@ -826,16 +826,14 @@ interp_set___main___attrs(PyObject *self, PyObject *args, PyObject *kwargs)
826826
}
827827

828828
// Check the updates.
829-
if (updates != Py_None) {
830-
Py_ssize_t size = PyObject_Size(updates);
831-
if (size < 0) {
832-
return NULL;
833-
}
834-
if (size == 0) {
835-
PyErr_SetString(PyExc_ValueError,
836-
"arg 2 must be a non-empty mapping");
837-
return NULL;
838-
}
829+
Py_ssize_t size = PyDict_Size(updates);
830+
if (size < 0) {
831+
return NULL;
832+
}
833+
if (size == 0) {
834+
PyErr_SetString(PyExc_ValueError,
835+
"arg 2 must be a non-empty dict");
836+
return NULL;
839837
}
840838

841839
_PyXI_session session = {0};

Python/crossinterp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,7 @@ _PyXI_Enter(_PyXI_session *session,
17041704
// Convert the attrs for cross-interpreter use.
17051705
_PyXI_namespace *sharedns = NULL;
17061706
if (nsupdates != NULL) {
1707+
assert(PyDict_Check(nsupdates));
17071708
sharedns = _PyXI_NamespaceFromDict(nsupdates, NULL);
17081709
if (sharedns == NULL && PyErr_Occurred()) {
17091710
assert(session->error == NULL);

0 commit comments

Comments
 (0)
0