@@ -1205,6 +1205,10 @@ _dir_locals(void)
12051205 Py_DECREF (names );
12061206 return NULL ;
12071207 }
1208+ if (PyList_Sort (names )) {
1209+ Py_DECREF (names );
1210+ return NULL ;
1211+ }
12081212 /* the locals don't need to be DECREF'd */
12091213 return names ;
12101214}
@@ -1213,7 +1217,7 @@ _dir_locals(void)
12131217static PyObject *
12141218_dir_object (PyObject * obj )
12151219{
1216- PyObject * result ;
1220+ PyObject * result , * sorted ;
12171221 static PyObject * dir_str = NULL ;
12181222 PyObject * dirfunc = _PyObject_LookupSpecial (obj , "__dir__" , & dir_str );
12191223
@@ -1228,18 +1232,16 @@ _dir_object(PyObject *obj)
12281232 Py_DECREF (dirfunc );
12291233 if (result == NULL )
12301234 return NULL ;
1231-
1232- /* result must be a list */
1233- /* XXX(gbrandl): could also check if all items are strings */
1234- if (!PyList_Check (result )) {
1235- PyErr_Format (PyExc_TypeError ,
1236- "__dir__() must return a list, not %.200s" ,
1237- Py_TYPE (result )-> tp_name );
1238- Py_DECREF (result );
1239- result = NULL ;
1235+ /* return sorted(result) */
1236+ sorted = PySequence_List (result );
1237+ Py_DECREF (result );
1238+ if (sorted == NULL )
1239+ return NULL ;
1240+ if (PyList_Sort (sorted )) {
1241+ Py_DECREF (sorted );
1242+ return NULL ;
12401243 }
1241-
1242- return result ;
1244+ return sorted ;
12431245}
12441246
12451247/* Implementation of dir() -- if obj is NULL, returns the names in the current
@@ -1249,24 +1251,7 @@ _dir_object(PyObject *obj)
12491251PyObject *
12501252PyObject_Dir (PyObject * obj )
12511253{
1252- PyObject * result ;
1253-
1254- if (obj == NULL )
1255- /* no object -- introspect the locals */
1256- result = _dir_locals ();
1257- else
1258- /* object -- introspect the object */
1259- result = _dir_object (obj );
1260-
1261- assert (result == NULL || PyList_Check (result ));
1262-
1263- if (result != NULL && PyList_Sort (result ) != 0 ) {
1264- /* sorting the list failed */
1265- Py_DECREF (result );
1266- result = NULL ;
1267- }
1268-
1269- return result ;
1254+ return (obj == NULL ) ? _dir_locals () : _dir_object (obj );
12701255}
12711256
12721257/*
0 commit comments