This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: adding __dir__
Type: Stage:
Components: Interpreter Core Versions: Python 2.6
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: arigo, gangesmaster, georg.brandl, ncoghlan, nnorwitz
Priority: high Keywords: patch

Created on 2006-11-06 21:52 by gangesmaster, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit gangesmaster, 2006-11-07 15:38 a little test/demo
dir.patch gangesmaster, 2006-11-11 21:31
Messages (12)
msg51325 - (view) Author: ganges master (gangesmaster) Date: 2006-11-06 21:52
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
msg51326 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2006-11-06 22: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.
msg51327 - (view) Author: ganges master (gangesmaster) Date: 2006-11-07 15: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 :)
msg51328 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2006-11-08 05: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.
msg51329 - (view) Author: ganges master (gangesmaster) Date: 2006-11-08 11: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)

msg51330 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-11-11 19: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?
msg51331 - (view) Author: ganges master (gangesmaster) Date: 2006-11-11 21: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.
msg51332 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2006-11-23 12:11
Line 20 in

    assert "__getitem__" in dir(x)

looks strange to me...  Foo doesn't inherit from
any sequence or mapping type.
msg51333 - (view) Author: ganges master (gangesmaster) Date: 2006-12-19 21:12
i guess the demo isn't updated/relevant anymore. 
instead, concrete tests were added to lib/tests/
msg51334 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-10 22:27
Applied to Python 3000 branch in rev. 54265.
Leaving open for the case that there's interest in backporting to 2.6.
msg51335 - (view) Author: Neal Norwitz (nnorwitz) * (Python committer) Date: 2007-03-11 02:36
I think this should be backported to 2.6.
msg51336 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-03-12 13:18
Done in rev. 54287.
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44203
2006-11-06 21:52:36gangesmastercreate