How do greenlets work?
<p>How are <a href="http://pypi.python.org/pypi/greenlet">greenlets</a> implemented? Python uses the C stack for the interpreter and it heap-allocates Python stack frames, but beyond that, how does it allocate/swap stacks, how does it hook into the interpreter and function call mechanisms, and how does this interact with C extensions? (Any quirks)?</p>

<p>There are some comments at the top of greenlet.c in the source, but they're a bit opaque. FWIW I'm coming from the perspective of someone who is unfamiliar with CPython internals but is very familiar with low-level systems programming, C, threads, events, coroutines/cooperative threads, kernel programming, etc.</p>

<p>(One data point: they <a href="http://code.google.com/p/coev/wiki/VersusGreenlets">don't use ucontext.h</a>.)</p>
