[issue12248] __dir__ semantics changed in Python 2.7.2

2011-06-04 Thread Soren Hansen

Soren Hansen  added the comment:

When I first investigated this problem (I reported the original bug on 
Launchpad), my first attempt to address this issue in pymox had me quite 
stumped. The class in question has a __getattr__ method. Up until now, this 
hasn't affected the use of dir(), but it does now. I really just wanted it 
return whatever it used to return (since that has worked so far), but realising 
that this was an old-style class, I couldn't just call super(TheClass, 
self).__dir__().

So my question is: If this change stays (which seems clear given that the only 
changes proposed here are ways of relaxing the type requirement of the __dir__ 
method's return value, not reverting the change altogether), and I have an 
old-style class with a __getattr__ defined, how do I make that class return 
whatever it would have usually returned for __dir__()?

--
nosy: +soren

___
Python tracker 
<http://bugs.python.org/issue12248>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12248] __dir__ semantics changed in Python 2.7.2

2011-06-04 Thread Soren Hansen

Soren Hansen  added the comment:

2011/6/4 Benjamin Peterson :
> 2011/6/4 Soren Hansen :
>> So my question is: If this change stays (which seems clear given that the 
>> only changes proposed here are ways of relaxing the type requirement of the 
>> __dir__ method's return value, not reverting the change altogether), and I 
>> have an old-style class with a __getattr__ defined, how do I make that class 
>> return whatever it would have usually returned for __dir__()?
>
> Yes, this is a limitation of magic methods on old style classes. The
> usual method is something like this:
>
>    def __getattr__(self, name):
>        if name == "__dir__":
>            return self.__dir__
>        # Other stuff
>
> Of course, the best fix is to use new-style classes. :)

If I do this:

= test.py ==
class Foo:
def __getattr__(self, name):
if name == '__dir__':
return self.__dir__
return 'something else'

a = Foo()
print dir(a)


After a lot of this:
  File "test.py", line 4, in __getattr__
return self.__dir__
  File "test.py", line 4, in __getattr__
return self.__dir__
  File "test.py", line 4, in __getattr__
return self.__dir__

...I end up with a "RuntimeError: maximum recursion depth exceeded". I
can't say I'm surprised :)

--

___
Python tracker 
<http://bugs.python.org/issue12248>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue12248] __dir__ semantics changed in Python 2.7.2

2011-06-04 Thread Soren Hansen

Soren Hansen  added the comment:

2011/6/5 Benjamin Peterson :
> 2011/6/4 Soren Hansen :
>> ...I end up with a "RuntimeError: maximum recursion depth exceeded". I
>> can't say I'm surprised :)
> Ah, sorry I should have thought before writing that. :)
> self.__class__.__dir__.__get__(self, self.__class__) should work,
> though.

Yeah, that one does seem to work. Excellent, thanks!

--

___
Python tracker 
<http://bugs.python.org/issue12248>
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com