Skip to content

Commit e6ecf11

Browse files
Check that the name is a string.
1 parent 65713ce commit e6ecf11

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Lib/test/test_capi/test_misc.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,8 +1107,7 @@ def test_sys_getattr(self):
11071107

11081108
with self.assertRaisesRegex(RuntimeError, r'lost sys\.nonexisting'):
11091109
sys_getattr('nonexisting')
1110-
with self.assertRaisesRegex(RuntimeError, r'lost sys\.1'):
1111-
sys_getattr(1)
1110+
self.assertRaises(TypeError, sys_getattr, 1)
11121111
self.assertRaises(TypeError, sys_getattr, [])
11131112
# CRASHES sys_getattr(NULL)
11141113

@@ -1132,7 +1131,7 @@ def test_sys_getoptionalattr(self):
11321131
self.assertEqual(getoptionalattr('\U0001f40d'), 42)
11331132

11341133
self.assertIs(getoptionalattr('nonexisting'), AttributeError)
1135-
self.assertIs(getoptionalattr(1), AttributeError)
1134+
self.assertRaises(TypeError, getoptionalattr, 1)
11361135
self.assertRaises(TypeError, getoptionalattr, [])
11371136
# CRASHES getoptionalattr(NULL)
11381137

Python/sysmodule.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ module sys
7474
PyObject *
7575
PySys_GetAttr(PyObject *name)
7676
{
77+
if (!PyUnicode_Check(name)) {
78+
PyErr_Format(PyExc_TypeError,
79+
"attribute name must be string, not '%.200s'",
80+
Py_TYPE(name)->tp_name);
81+
return NULL;
82+
}
7783
PyThreadState *tstate = _PyThreadState_GET();
7884
PyObject *sysdict = tstate->interp->sysdict;
7985
if (sysdict == NULL) {
@@ -106,6 +112,13 @@ PySys_GetAttrString(const char *name)
106112
int
107113
PySys_GetOptionalAttr(PyObject *name, PyObject **value)
108114
{
115+
if (!PyUnicode_Check(name)) {
116+
PyErr_Format(PyExc_TypeError,
117+
"attribute name must be string, not '%.200s'",
118+
Py_TYPE(name)->tp_name);
119+
*value = NULL;
120+
return -1;
121+
}
109122
PyThreadState *tstate = _PyThreadState_GET();
110123
PyObject *sysdict = tstate->interp->sysdict;
111124
if (sysdict == NULL) {

0 commit comments

Comments
 (0)