Stackless Python and Python 2.x
Tim Peters
tim.one at home.com
Mon Sep 3 20:06:09 EDT 2001
[Paul Rubin]
> OK, I got your response and read PEP 252. It's impressive.
In that case, you should try reading PEP 255 <wink>.
> But now I'm wondering from the opposite end. If the regular Python
> implementation puts function call frames on the heap, then what's
> the difference between regular Python and Stackless Python?
The C stack. The CPython interpreter is written via recursive C calls, and
a Python-level call has both the platform C stack and Python frameobjects in
the mix. Since Simple Generators always yield to their immediate invoker,
there's never a C stack frame "stuck in the middle" (yielding *returns* from
the internal C-level call). Stackless Python is a major (yet still
incomplete) reworking of the Python implementation to avoid mixing C-level
calls into Python-level calls. More detail at:
http://www.stackless.com/spcpaper.htm
> I also noticed somewhere that the compiler does something special if
> it sees a yield statement in a block.
I suspect you've got an inappropriate meaning of "block" in mind. In
Python, a single code block covers, e.g., an entire function.
> What happens if something evals to a yield statement?
eval() refuses to evaluate any statement; it only allows expressions. exec
handles statements, but a yield is exactly like a return in the sense that
both can only appear in functions -- you'll simply get a SyntaxError if you
try to exec a string that contains a yield or return without an enclosing
def statement also in that string.
> Worse, what happens if there's a finally clause:
>
> x = "yield blah()"
>
> try:
> exec x
> finally:
> mumble()
The exec there fails with a SyntaxError (see last paragraph); "exec" isn't a
textual macro substitution gimmick, it executes a complete code block. It's
impossible to exec a string containing a yield (or return) unless the string
also contains a function enclosing the yield (or return):
>>> from __future__ import generators
>>> def f():
... exec "yield 1"
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in f
File "<string>", line 1
SyntaxError: 'yield' outside function
>>>
More on that here:
http://www.python.org/doc/current/ref/execframes.html
More information about the Python-list
mailing list