[Patches] [ python-Patches-1591665 ] adding __dir__

SourceForge.net noreply at sourceforge.net
Tue Dec 19 22:12:43 CET 2006

Patches item #1591665, was opened at 2006-11-06 23:52
Message generated for change (Comment added) made by gangesmaster
You can respond by visiting: 

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: Python 2.6
Status: Open
Resolution: None
Priority: 6
Private: No
Submitted By: ganges master (gangesmaster)
Assigned to: Neal Norwitz (nnorwitz)
Summary: adding __dir__

Initial Comment:
in accordance with 

i've written a patch that allows objects to define their
own introspection mechanisms, by providing __dir__.
with this patch:
* dir() returns the locals. this is done in builtin_dir()
* dir(obj) returns the attributes of obj, 
by invoking PyObject_Dir()
* if obj->ob_type has "__dir__", it is used. 
note that it must return a list!
* otherwise, use default the mechanism of collecting 
* for module objects, return __dict__.keys()
* for type objects, return __dict__.keys() +
* for all other objects, return __dict__.keys() + 
__members__ + __methods__ + dir(obj.__class__)
* builtin_dir takes care of sorting the list


>Comment By: ganges master (gangesmaster)
Date: 2006-12-19 23:12

Logged In: YES 
Originator: YES

i guess the demo isn't updated/relevant anymore. 
instead, concrete tests were added to lib/tests/test_builtin.py


Comment By: Armin Rigo (arigo)
Date: 2006-11-23 14:11

Logged In: YES 
Originator: NO

Line 20 in demo.py:

    assert "__getitem__" in dir(x)

looks strange to me...  Foo doesn't inherit from
any sequence or mapping type.


Comment By: ganges master (gangesmaster)
Date: 2006-11-11 23:31

Logged In: YES 

> PyObject_CallFunctionObjArgs(dirfunc, obj, NULL)

> Couldn't __dir__ also be allowed to return a tuple?
no, because tuples are not sortable, and i don't want to 
over complicate the c-side code of PyObject_Dir. 
having __dir__ returning only a list is equivalent to 
__repr__ returning only strings.


Comment By: Georg Brandl (gbrandl)
Date: 2006-11-11 21:58

Logged In: YES 

* Instead of doing PyObject_CallFunction(dirfunc, "O", obj)
you should
  do PyObject_CallFunctionObjArgs(dirfunc, obj, NULL).
* Couldn't __dir__ also be allowed to return a tuple?


Comment By: ganges master (gangesmaster)
Date: 2006-11-08 13:22

Logged In: YES 

i like to init all my locals ("just in case"), but
if the rest of the code does not adhere my style, 
i'll change that.
anyway, i made the changes to the code, updated the docs, 
and  added full tests (the original dir() wasn't test 
so thoroughly)



Comment By: Neal Norwitz (nnorwitz)
Date: 2006-11-08 07:53

Logged In: YES 

tomer, do you know about configuring with --pydebug?  That
helps track down refleaks when running regrtest -R ::.

 _dir_locals: result is not necessary and locals doesn't
need to be initialized as it's set on the next line.  You
could just declare and set it all on one line.

_specialized_dir_type should be static.  No need to init
dict.  Either don't init result or remove else result =
NULL.  I'd prefer removing the else and leaving the init.

_specialized_dir_module should be static.  No need to init
dict.  Can you get the name of the module and use that in
the error msg: PyModule_GetName()?  That would hopefully
provide a nicer error msg.

_generic_dir: No need to init dict.  

+               /* XXX api change: how about falling to
+                  XXX if no __class__ exists? */

Do you mean falling *back*?  Also, we've been using
XXX(username): as the format for such comments.  So this
would be better as:

 /* XXX(tomer): Perhaps fall back to obj->ob_type if no
__class__ exists? */

_dir_object: No need to init dirfunc.  

PyObject_Dir: No need to init result.

Are there tests for all conditions?  At least:
 * dir()
 * dir(obj)
 * dir(obj_with_no_dict)
 * dir(obj_with_no__class__)
 * dir(obj_with__methods__)
 * dir(obj_with__members__)
 * dir(module)
 * dir(module_with_no__dict__)
 * dir(module_with_invalid__dict__)

There also need to be updates to Doc/lib/libfuncs.tex.  If
you can't deal with the markup, just do the best you can in
text and someone else will fixup the markup.

Thanks for attaching the patch as a single file, it's easier
to deal with.


Comment By: ganges master (gangesmaster)
Date: 2006-11-07 17:37

Logged In: YES 

* builtin_dir directly calls PyObject_Dir
* PyObject_Dir handles NULL argument and sorting
* it is now completely compatible with the 2.5 API
* fixed several refcount bugs (i wish we had a tracing gc :)


Comment By: Nick Coghlan (ncoghlan)
Date: 2006-11-07 00:52

Logged In: YES 

The retrieval of locals on a NULL argument and the sorting
step need to move back inside PyObject_Dir to avoid changing
the C API.

If the standard library's current C API tests didn't break
on this version of the patch, then the final version of the
patch should include enhanced tests for PyObject_Dir that
pass both before and after the patch is applied to PyObject_Dir.

Other than that, I didn't see any major problems on reading
the code (i.e. refcounts and error handling looked pretty
reasonable). I haven't actually run it though.


You can respond by visiting: 

More information about the Patches mailing list