[Python-3000-checkins] r54265 - in python/branches/p3yk: Doc/lib/libfuncs.tex Lib/test/test_builtin.py Misc/NEWS Objects/object.c Python/bltinmodule.c

Georg Brandl g.brandl at gmx.net
Tue Mar 13 08:22:04 CET 2007


Neal Norwitz schrieb:
> I assume this is not the desired behaviour?
> 
>>>> class F:
> ...   def __dir__(self):
> ...     return [5]
> ...
>>>> dir(F())
> [5]
>>>> f = F()
>>>> dir(f)
> [5]
>>>> def __dir__(): return [10]
> ...
>>>> f.__dir__ = __dir__
>>>> dir(f)
> [5]
> 
> I think the problem is in _dir_object()
> 
> +       PyObject * dirfunc = PyObject_GetAttrString((PyObject*)obj->ob_type,
> +                                                   "__dir__");
> 
> Shouldn't the first arg just be obj, not obj->ob_type?

This is modeled after the principle that for new-style objects, __special__
methods are looked up on the type, not the instance.


 > I also wanted to point out that in the example below__dir__() is
 > returning non-strings.  Is that ok, even though attributes must be
 > strings?

Yes, I have already put an XXX in the code asking if the returned list should
be checked for string-only entries. I wasn't sure whether it is worth the
effort.

>> +"  for an other object: its attributes, its class's attributes, and\n"
>> +"    recursively the attributes of its class's base classes.");
> 
> 'for an other object' should that be for *any* other object?  There
> might have been other stuff, this patch was too hard to read.


Fixed, thanks.

Georg



More information about the Python-3000-checkins mailing list