[Python-Dev] Stackless Python

Christian Tismer tismer at stackless.com
Wed Jun 2 14:32:11 EDT 2004

Guido van Rossum wrote:


[cooperative multitasking]

> Right.  That PEP better explain how one writes C code that calls into
> Python without involving the C stack as well!

Quick reply to give you an idea:

A tail recursive call is rather trivial, so here the non-trivial
side of the coin:

Conventional style:

PyObject *
	PyObject *ret, ret2;

	/* do preparation things */
	/* call into Python */

	ret = PyEval_CallObject(some_args);

	/* do post-processing */
	ret2 = some_other_processing(ret);
	return ret2;

Stackless style (simplified and sketched, only):

PyObject *
	PyObject *ret, ret2;

	/* do preparation things */
	/* put parameters in miniframe */

	/* deferred call into Python */

	ret = PyEval_CallObject_defer(some_args);
	return <special value>

PyObject *
	/* reload variables from miniframe */
	/* drop miniframe */
	/* do post-processing */
	ret2 = some_other_processing(ret);
	return ret2;

So the idea is to avoid recursions by creating frame-like
tiny structures, which are then executed by a toplevel loop
which does nothing than call the next frame.
The <special value> (not specified here) passes through the
currently involved C code as a dummy which unwinds the stack
until the toplevel loop is reached. There this value is
captured and unpacked, and execution continues.

Well, this was rough and incomplete. A PEP would be nice
and also quite much work. The protocol sketched above
is a lot of work to do if you want to do it throughout all of
Python, and I didn't do it in most cases, but did the simple
tail-recursive things (which don't need that extra tinyframe)
everywhere where possible.
If adopting a similar technique for parts of Python is not
feasible, then the PEP would be a waste of time. There is
no other principal way to do it in plain C, IMHO.

cheers - chris
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  mobile +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/

More information about the Python-Dev mailing list