too simple a question : forward declaration? (also, how to make Python segfault!)
Jeff Epler
jepler at unpythonic.net
Wed May 14 08:49:59 EDT 2003
On Wed, May 14, 2003 at 08:39:53AM -0400, Roy Smith wrote:
> Anyway, the way to fix it up is to delay the execution of the call to
> create the cross-class references until after both classes have been
> defined. My first attempt at this was:
>
> class foo:
> def __init__ (self):
> self.name = "My name is foo"
> self.barReference = bar()
>
> def showBar (self):
> print self.barReference.name
>
> class bar:
> def __init__ (self):
> self.name = "My name is bar"
> self.fooReference = foo()
>
> def showFoo (self):
> print self.fooReference.name
>
> f = foo()
> f.showBar()
>
> which does something I've never managed to do in 6 years of writing
> Python code -- it crashes the interpreter with a segfault!
>
> bash-2.05a$ ./circular2.py
> Segmentation fault
>
> I've been writing C++ code a bunch for the past couple of months. I get
> segfaults all the time in C++, but never in Python. Maybe some of that
> static typing compiled language type-bondage bad juju rubbed off on me?
> Nah, a few minutes of head scratching shows that it's nothing more than
> a plain old infinite recursion that eventually runs out of memory. I'm
> surprised I didn't get a MemoryError, but that's a minor point.
Well, in theory Python should show a very long traceback, ending with
File "roysmith.py", line 12, in __init__
self.fooReference = foo()
File "roysmith.py", line 4, in __init__
self.barReference = bar()
RuntimeError: maximum recursion depth exceeded
however, if sys.setrecursionlimit() has been called with a value that's
too large, then Python will happily overflow the C stack. I thought the
value for the recursion limit was supposed to be sane by default, but
maybe not on all systems..
Jeff
More information about the Python-list
mailing list