Proper tail recursion

Tim Peters tim.peters at
Sat Jul 10 09:06:00 CEST 2004

[Chris King]
> I'm trying a new implementation, making good use of a loop around the
> entirety of PyEval_EvalFrame(). Unfortunately, my changes cause the
> interpreter to segfault after a couple of tail calls. A debugger shows
> that 0xffffffff is getting stuck into a pool->freeblock in
> PyObject_Free(), and then is subsequently dereferenced in
> PyObject_Malloc() (causing the segfault).
> Could this be caused by my code Py_DECREFing an object too many times,
> but leaving a pointer to it somewhere? (My changes don't explicitly set
> anything to 0xffffffff or -1.) Or am I just in way over my head? :P

WHen fiddling with Python internals, build Python in debug mode.  That
enables many checks that will save you weeks of debugging.  For
example, if you decref an object too many times, the expansion of the
Py_DECREF macro in a debug build will catch that and complain the
instant the refcount goes negative.  In a debug build pymalloc also
pads both ends of allocated regions with special byte patterns,
initializes newly allocated memory with another special byte pattern,
and overwrites newly freed memory with a third special byte pattern. 
That's very effective at catching many kinds of problems too.  Read

And yes, of course you're in way over your head.  But that's how you
learn to swim, so enjoy it <wink>.

More information about the Python-list mailing list