Index: Objects/object.c =================================================================== --- Objects/object.c (révision 69678) +++ Objects/object.c (copie de travail) @@ -1926,6 +1926,7 @@ PyObject_Dir(PyObject *obj) { PyObject * result; + Py_ssize_t i, len; if (obj == NULL) /* no object -- introspect the locals */ @@ -1934,9 +1935,25 @@ /* object -- introspect the object */ result = _dir_object(obj); - assert(result == NULL || PyList_Check(result)); + if (result == NULL) + return NULL; + + assert(PyList_Check(result)); + + len = PyList_GET_SIZE(result); + for (i = 0; i < len; i++) { + PyObject *v = PyList_GET_ITEM(result, i); + if (!PyString_Check(v)) { + PyErr_Format(PyExc_TypeError, + "__dir__() must return a list of string, " + "not a list of %.200s", + Py_TYPE(v)->tp_name); + Py_DECREF(result); + return NULL; + } + } - if (result != NULL && PyList_Sort(result) != 0) { + if (PyList_Sort(result) != 0) { /* sorting the list failed */ Py_DECREF(result); result = NULL; Index: Lib/test/test_builtin.py =================================================================== --- Lib/test/test_builtin.py (révision 69678) +++ Lib/test/test_builtin.py (copie de travail) @@ -290,6 +290,13 @@ f = Foo() self.assertRaises(TypeError, dir, f) + # issue 1056293: list of non-string + class Foo(object): + def __dir__(self): + return [7] + f = Foo() + self.assertRaises(TypeError, dir, f) + def test_divmod(self): self.assertEqual(divmod(12, 7), (1, 5)) self.assertEqual(divmod(-12, 7), (-2, 2))