Skip to content

Commit f400fb8

Browse files
committed
Remove redundant Py_None check, add None tests, add assert
1 parent 9ecfb20 commit f400fb8

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

Lib/test/test__interpreters.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,17 @@ def test_signatures(self):
490490
with self.assertRaisesRegex(TypeError, msg):
491491
_interpreters.set___main___attrs(self.id, 1)
492492

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)
503+
493504
def test_invalid_shared_encoding(self):
494505
# See https://github.com/python/cpython/issues/127196
495506
bad_shared = {"\uD82A": 0}

Modules/_interpretersmodule.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = PyDict_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 dict");
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)