What do we call "stack" in Python? Is it the C stack of CPython? I read that Python stackframes are allocated in a heap. But I thought the goal of a stack was... to stack stackframes. What does the stack do then?
Oversimplifying slightly:
In CPython, when PyEval_EvalFrameEx
is evaluating a Python stack frame's code, and comes to a direct function call, it allocates a new Python stack frame, links it up… and then recursively calls PyEval_EvalFrameEx
on that new frame.
So, the C stack is a stack of recursive calls of the interpreter loop.
The Python stack is a stack of Python frame objects, implemented as a simple linked list of heap-allocated objects.
They're not completely unrelated, but they're not the same thing.
When you use generators, this gets slightly more confusing, because those Python stack frames can be unlinked and relinked in different places when they're resumed. Which is why the two stacks are separate. (See Ned's answer, which explains this better than I could.)