seg fault on asscessing undefined attr in __getattr__

Michael Hudson mwh at python.net
Fri May 25 08:35:08 EDT 2001


"Stephen Hansen" <news at myNOSPAM.org> writes:

> "Oleg Broytmann" <phd at phd.fep.ru> wrote in message
> news:mailman.990786458.26390.python-list at python.org...
> > On Fri, 25 May 2001, Kong-Jei Kuan wrote:
> > > hi, i am still using python 1.52, and don't know if this happens to
> other
> > > versions, can some verify this?
> > >
> > > Python 1.5.2 (#0, Dec 27 2000, 13:59:38)  [GCC 2.95.2 20000220 (Debian
> GNU/Linux)] on linux2
> > > Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> > > >>> class A:
> > > ...   def __getattr__(self, name):
> > > ...     self.x
> > > ...
> > > >>> a=A()
> > > >>> a
> > > Segmentation fault
> >
> >    Do you understand you've put your Python into infinite recursion?
> 
>     Uh. How is this an infinate recursion? 

In ways you don't understand...

> Going strictly by the code above, it isn't. 'a' and 'A' are entirely
> different objects; the former being an instance of the
> latter. Further, typing 'a' by itself on the interpreter line should
> reveal, '<__main__.A instance at (address)>'.

The interpreter is trying to find a __repr__ method.

> And, even if it *was* in the middle of an infinate loop, a
> RuntimeError should be thrown.

Indeed, it doesn't crash here; either 1.5.2 or 2.1.  I was expecting
to crash 1.5.2 though...

> I do believe a Segfault is one of those things which is "always a
> bug"...  although in this case, i'd imagine its not with Python
> itself, but with something else which is misbehaving on KJK's
> machine. 

> There's no way Python 1.5.2 should be crashing on such simple code,

Python 1.5.2 will crash on:

>>> l = []
>>> l.append(l)
>>> m = []
>>> m.append(m)
>>> l == m
Segmentation fault (core dumped)

or:

>>> l = []
>>> l.append(l)
>>> import marshal 
>>> marshal.dumps(l)
Segmentation fault (core dumped)

There are probably others.  I don't think there are any of these in
2.1 (apart from one bug in the compiler, which is fixed in CVS).

> which hasn't actually even _called_ '__getattr__' yet, 

Beep!  Here's your mistake.

Cheers,
M.

-- 
  I have gathered a posie of other men's flowers, and nothing but
  the thread that binds them is my own.                   -- Montaigne



More information about the Python-list mailing list