473,320 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

__getattr__ possible loop

I have tried this, with Psyco it segfaults, and with Python 2.5 (on
Win) hangs the interpreter, is it possible to improve the situation?

class T(object):
def __getattr__(self, x): dir(self)
#import psyco
#psyco.full()
T().method()

(Probably dir calls __getattr__).

Bye,
bearophile

Dec 28 '06 #1
7 1595
be************@lycos.com wrote:
I have tried this, with Psyco it segfaults, and with Python 2.5 (on
Win) hangs the interpreter, is it possible to improve the situation?

class T(object):
def __getattr__(self, x): dir(self)
#import psyco
#psyco.full()
T().method()

(Probably dir calls __getattr__).

Bye,
bearophile
how to improve the situation depends on what do you expect to get by calling "T().method()"

dir calls __getattr__ with the value '__members__', for example you can write:

def __getattr__(self, x):

if x == '__members__':
return ('method1', 'field1', )

--
Maksim Kasimov
Dec 28 '06 #2
Maksim Kasimov:
how to improve the situation depends on what do you expect to get by calling "T().method()"
You are right, sorry for being cryptic. I think that's a kind of bug of
Python (produced maybe by an infinite loop), so an improvement can be a
traceback or some automatic breaking of that loop. Note that my problem
comes from using Psyco that segfaults in that situation (if you have
Psyco installed you can decomment two lines to see that). I think that
using normal code Python+Psyco don't have to segfault, but I know this
is tricky situation, so if no simple "solutions" can be found, then
it's not a important thing and it can be ignored.

Bye,
bearophile

Dec 28 '06 #3
On 28 Dec 2006 07:45:05 -0800, be************@lycos.com
<be************@lycos.comwrote:
Maksim Kasimov:
how to improve the situation depends on what do you expect to get by calling "T().method()"

You are right, sorry for being cryptic. I think that's a kind of bug of
Python (produced maybe by an infinite loop), so an improvement can be a
traceback or some automatic breaking of that loop. Note that my problem
comes from using Psyco that segfaults in that situation (if you have
Psyco installed you can decomment two lines to see that). I think that
using normal code Python+Psyco don't have to segfault, but I know this
is tricky situation, so if no simple "solutions" can be found, then
it's not a important thing and it can be ignored.
What I find most interesting is that you don't crash out because of
hitting the recursion limit. My brief testing shows something odd
going on - when the stack depth gets to almost 1000 (the recursion
limit on my system) it knocks some stuff off the stack and the stack
limit never gets to the limit. Some sort of built in tail recursion?
Dec 28 '06 #4
On 12/28/06, Chris Mellon <ar*****@gmail.comwrote:
On 28 Dec 2006 07:45:05 -0800, be************@lycos.com
<be************@lycos.comwrote:
Maksim Kasimov:
how to improve the situation depends on what do you expect to get by calling "T().method()"
You are right, sorry for being cryptic. I think that's a kind of bug of
Python (produced maybe by an infinite loop), so an improvement can be a
traceback or some automatic breaking of that loop. Note that my problem
comes from using Psyco that segfaults in that situation (if you have
Psyco installed you can decomment two lines to see that). I think that
using normal code Python+Psyco don't have to segfault, but I know this
is tricky situation, so if no simple "solutions" can be found, then
it's not a important thing and it can be ignored.

What I find most interesting is that you don't crash out because of
hitting the recursion limit. My brief testing shows something odd
going on - when the stack depth gets to almost 1000 (the recursion
limit on my system) it knocks some stuff off the stack and the stack
limit never gets to the limit. Some sort of built in tail recursion?
Nothing so clever. dir() eats and ignores all exceptions, so when you
hit the recursion limit it eats the RecursionLimitExceeded exception
and continues merrily along the way. This is probably not good
behavior...

class Foo:
def __getattr__(self, attr):
raise SystemExit, "Don't call me, again, ever"

f = Foo()
f.method() #dies correctly
dir(f) #continues happily
Dec 28 '06 #5
be************@lycos.com wrote:
Maksim Kasimov:
>how to improve the situation depends on what do you expect to get by calling "T().method()"

You are right, sorry for being cryptic. I think that's a kind of bug of
Python (produced maybe by an infinite loop), so an improvement can be a
traceback or some automatic breaking of that loop. Note that my problem
comes from using Psyco that segfaults in that situation (if you have
Psyco installed you can decomment two lines to see that). I think that
using normal code Python+Psyco don't have to segfault, but I know this
is tricky situation, so if no simple "solutions" can be found, then
it's not a important thing and it can be ignored.

Bye,
bearophile
it is neither a bug of python neither it comes from using Psyco,
the problem is here:
def __getattr__(self, x): dir(self)

the "__getattr__" method calls "dir",
but then "dir" calls "__getattr__" method of the object (self) - it makes endless loop
(see the code in my previous message)
--
Maksim Kasimov
Dec 28 '06 #6
Chris Mellon wrote:
>
Nothing so clever. dir() eats and ignores all exceptions, so when you
hit the recursion limit it eats the RecursionLimitExceeded exception
and continues merrily along the way. This is probably not good
behavior...

class Foo:
def __getattr__(self, attr):
raise SystemExit, "Don't call me, again, ever"

f = Foo()
f.method() #dies correctly
dir(f) #continues happily
can't understand - what kind of problem you tried to fix in that way?
if __getattr__ just raise some exception, it needless to define it at all.

--
Maksim Kasimov
Dec 28 '06 #7
At Thursday 28/12/2006 14:01, Maksim Kasimov wrote:
Nothing so clever. dir() eats and ignores all exceptions, so when you
hit the recursion limit it eats the RecursionLimitExceeded exception
and continues merrily along the way. This is probably not good
behavior...

class Foo:
def __getattr__(self, attr):
raise SystemExit, "Don't call me, again, ever"

f = Foo()
f.method() #dies correctly
dir(f) #continues happily

can't understand - what kind of problem you tried to fix in that way?
if __getattr__ just raise some exception, it needless to define it at all.
The problem is, dir() blindly eats *all* exceptions, including the
RecursionLimitExceeded on the original post, this SystemExit,
KeyboardInterrupt, etc. It shouldn't.
Perhaps this example (esencially the same thing) is a bit more clear:

import sys
class Foo:
def __getattr__(self, attr):
print "About to exit program"
sys.exit(1)

f = Foo()
dir(f)
print "You should not see this line"
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Dec 29 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Greg Brunet | last post by:
In adding the ability to refer to field values using dbfFile.field notation, I learned how to use __getattr__ and __setattr__ . After some trial and error, I got it working. But as part of my...
5
by: Laszlo Nagy | last post by:
Hello, This is from the Python documentation (fragment): __getattr__( self, name) Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an...
4
by: Enrico | last post by:
Hi there, I have the following situation (I tryed to minimize the code to concentrate on the issue): def __getattr__(self, name): print 'A.__getattr__' if name == 'a': return 1 raise...
2
by: mwojc | last post by:
Hi! My class with implemented __getattr__ and __setattr__ methods cannot be pickled because of the Error: ====================================================================== ERROR:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.