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

SourceForge.net noreply at sourceforge.net
Thu Nov 23 13:11:54 CET 2006


Patches item #1591665, was opened at 2006-11-06 21:52
Message generated for change (Comment added) made by arigo
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1591665&group_id=5470

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: 5
Private: No
Submitted By: ganges master (gangesmaster)
Assigned to: Neal Norwitz (nnorwitz)
Summary: adding __dir__

Initial Comment:
in accordance with 
http://mail.python.org/pipermail/python-dev/2006-November/069865.html

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 
attributes
* for module objects, return __dict__.keys()
* for type objects, return __dict__.keys() +
dir(obj.__base__)
* for all other objects, return __dict__.keys() + 
__members__ + __methods__ + dir(obj.__class__)
* builtin_dir takes care of sorting the list

----------------------------------------------------------------------

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

Message:
Logged In: YES 
user_id=4771
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 21:31

Message:
Logged In: YES 
user_id=1406776

> PyObject_CallFunctionObjArgs(dirfunc, obj, NULL)
done

> 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 19:58

Message:
Logged In: YES 
user_id=849994

* 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 11:22

Message:
Logged In: YES 
user_id=1406776

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)


-tomer

----------------------------------------------------------------------

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

Message:
Logged In: YES 
user_id=33168

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

object.c: 
 _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
obj->ob_type 
+                  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 15:37

Message:
Logged In: YES 
user_id=1406776

okay:
* 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-06 22:52

Message:
Logged In: YES 
user_id=1038590

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: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1591665&group_id=5470


More information about the Patches mailing list