diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -313,9 +313,9 @@ # dir(obj_using __dir__) class Foo(object): def __dir__(self): - return ["kan", "ga", "roo"] + return ('xyz', 'pdq', 'lmno') f = Foo() - self.assertTrue(dir(f) == ["ga", "kan", "roo"]) + self.assertTrue(dir(f) == ['lmno', 'pdq', 'xyz']) # dir(obj__dir__not_list) class Foo(object): diff --git a/Objects/object.c b/Objects/object.c --- a/Objects/object.c +++ b/Objects/object.c @@ -1907,6 +1907,7 @@ PyObject *result = NULL; static PyObject *dir_str = NULL; PyObject *dirfunc; + PyObject *oldresult; assert(obj); if (PyInstance_Check(obj)) { @@ -1940,13 +1941,10 @@ return NULL; /* result must be a list */ - /* XXX(gbrandl): could also check if all items are strings */ if (!PyList_Check(result)) { - PyErr_Format(PyExc_TypeError, - "__dir__() must return a list, not %.200s", - Py_TYPE(result)->tp_name); - Py_DECREF(result); - result = NULL; + oldresult = result; + result = PySequence_List(result); + Py_DECREF(oldresult); } }