Skip to content
Open
8 changes: 8 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4072,6 +4072,14 @@ class A:
actual = self.get_suggestion(A(), 'bluch')
self.assertNotIn("blech", actual)

def test_suggestions_do_not_trigger_with_non_string_candidates(self):
def run_module_code():
import runpy
runpy._run_module_code("blech", {0: "", "bluch": ""}, "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
runpy._run_module_code("blech", {0: "", "bluch": ""}, "")
# non-string global variable
runpy._run_module_code("blech", {0: "", "bluch": ""}, "")


actual = self.get_exception(run_module_code, slice_start=-1, slice_end=None)
self.assertNotIn("bluch", actual[0])

def test_getattr_suggestions_no_args(self):
class A:
blech = None
Expand Down
4 changes: 4 additions & 0 deletions Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ _Py_CalculateSuggestions(PyObject *dir,
}
for (Py_ssize_t i = 0; i < dir_size; ++i) {
PyObject *item = PyList_GET_ITEM(dir, i);
if (!PyUnicode_Check(item)) {
PyMem_Free(buffer);
return NULL;
}
if (_PyUnicode_Equal(name, item)) {
continue;
}
Expand Down
Loading