@@ -426,150 +426,14 @@ the effects of any future statements in effect in the code calling\n\
426426compile; if absent or zero these statements do influence the compilation,\n\
427427in addition to any features explicitly specified." ;
428428
429- /* Merge the __dict__ of aclass into dict, and recursively also all
430- the __dict__s of aclass's base classes. The order of merging isn't
431- defined, as it's expected that only the final set of dict keys is
432- interesting.
433- Return 0 on success, -1 on error.
434- */
435-
436- static int
437- merge_class_dict (PyObject * dict , PyObject * aclass )
438- {
439- PyObject * classdict ;
440- PyObject * bases ;
441-
442- assert (PyDict_Check (dict ));
443- assert (aclass );
444-
445- /* Merge in the type's dict (if any). */
446- classdict = PyObject_GetAttrString (aclass , "__dict__" );
447- if (classdict == NULL )
448- PyErr_Clear ();
449- else {
450- int status = PyDict_Update (dict , classdict );
451- Py_DECREF (classdict );
452- if (status < 0 )
453- return -1 ;
454- }
455-
456- /* Recursively merge in the base types' (if any) dicts. */
457- bases = PyObject_GetAttrString (aclass , "__bases__" );
458- if (bases != NULL ) {
459- int i , n ;
460- assert (PyTuple_Check (bases ));
461- n = PyTuple_GET_SIZE (bases );
462- for (i = 0 ; i < n ; i ++ ) {
463- PyObject * base = PyTuple_GET_ITEM (bases , i );
464- if (merge_class_dict (dict , base ) < 0 ) {
465- Py_DECREF (bases );
466- return -1 ;
467- }
468- }
469- Py_DECREF (bases );
470- }
471- return 0 ;
472- }
473-
474429static PyObject *
475430builtin_dir (PyObject * self , PyObject * args )
476431{
477432 PyObject * arg = NULL ;
478- /* Set exactly one of these non-NULL before the end. */
479- PyObject * result = NULL ; /* result list */
480- PyObject * masterdict = NULL ; /* result is masterdict.keys() */
481433
482434 if (!PyArg_ParseTuple (args , "|O:dir" , & arg ))
483435 return NULL ;
484-
485- /* If no arg, return the locals. */
486- if (arg == NULL ) {
487- PyObject * locals = PyEval_GetLocals ();
488- if (locals == NULL )
489- goto error ;
490- result = PyDict_Keys (locals );
491- if (result == NULL )
492- goto error ;
493- }
494-
495- /* Elif this is some form of module, we only want its dict. */
496- else if (PyObject_TypeCheck (arg , & PyModule_Type )) {
497- masterdict = PyObject_GetAttrString (arg , "__dict__" );
498- if (masterdict == NULL )
499- goto error ;
500- assert (PyDict_Check (masterdict ));
501- }
502-
503- /* Elif some form of type or class, grab its dict and its bases.
504- We deliberately don't suck up its __class__, as methods belonging
505- to the metaclass would probably be more confusing than helpful. */
506- else if (PyType_Check (arg ) || PyClass_Check (arg )) {
507- masterdict = PyDict_New ();
508- if (masterdict == NULL )
509- goto error ;
510- if (merge_class_dict (masterdict , arg ) < 0 )
511- goto error ;
512- }
513-
514- /* Else look at its dict, and the attrs reachable from its class. */
515- else {
516- PyObject * itsclass ;
517- /* Create a dict to start with. CAUTION: Not everything
518- responding to __dict__ returns a dict! */
519- masterdict = PyObject_GetAttrString (arg , "__dict__" );
520- if (masterdict == NULL ) {
521- PyErr_Clear ();
522- masterdict = PyDict_New ();
523- }
524- else if (!PyDict_Check (masterdict )) {
525- Py_DECREF (masterdict );
526- masterdict = PyDict_New ();
527- }
528- else {
529- /* The object may have returned a reference to its
530- dict, so copy it to avoid mutating it. */
531- PyObject * temp = PyDict_Copy (masterdict );
532- Py_DECREF (masterdict );
533- masterdict = temp ;
534- }
535- if (masterdict == NULL )
536- goto error ;
537-
538- /* Merge in attrs reachable from its class.
539- CAUTION: Not all objects have a __class__ attr. */
540- itsclass = PyObject_GetAttrString (arg , "__class__" );
541- if (itsclass == NULL )
542- PyErr_Clear ();
543- else {
544- int status = merge_class_dict (masterdict , itsclass );
545- Py_DECREF (itsclass );
546- if (status < 0 )
547- goto error ;
548- }
549- }
550-
551- assert ((result == NULL ) ^ (masterdict == NULL ));
552- if (masterdict != NULL ) {
553- /* The result comes from its keys. */
554- assert (result == NULL );
555- result = PyDict_Keys (masterdict );
556- if (result == NULL )
557- goto error ;
558- }
559-
560- assert (result );
561- if (PyList_Sort (result ) != 0 )
562- goto error ;
563- else
564- goto normal_return ;
565-
566- error :
567- Py_XDECREF (result );
568- result = NULL ;
569- /* fall through */
570- normal_return :
571- Py_XDECREF (masterdict );
572- return result ;
436+ return PyObject_Dir (arg );
573437}
574438
575439static char dir_doc [] =
0 commit comments