python compiler package questions

Phil Schmidt pschmidt at omnimn.com
Wed Jan 8 14:45:46 CET 2003


Background:
I do embedded development for a living on little 8-bit and 16-bit
micros. Most of these have C compilers available, so I do much of my
coding in C. However, I've been playing with Python for a year or so
now, and really like it. I'd like to try to come up with a subset of
Python that could be used on little micros, with most of my interest
being in the object-oriented-ness of Python.

I started looking at the Python compiler package, and it occurred to
me that I could possibly create C code from the Python AST, which
could then be compiled by the little micro's C compiler. Thus, I could
make a language with Python syntax, and with semantics as much like
Python as is practical for the little buggers.

Simple questions:
Does this sound like a practical approach? If not, what would you
suggest instead?

Technical questions:
I used the following code to play with the compiler (using Python
2.2.1 in Windows):
--------------------------
import compiler
s=compiler.parse("""def afunc(a=0, h):
    print a
    a = a * 2
    print a, 'hello'
""")
--------------------------

I found that the code parsed ok as shown above - i.e., no error with
the argument list to afunc - though I think this should produce an
error. Then, when I change the argument list to "(a, h=0)", this also
parsed ok, and produced exactly the same AST as the first case. This
seems wrong! Adding a comma after h in the first example caused the
parser to produce an AST different from the second case, and which I
believe to be correct.

I traced this down to transformer.py in the compiler package, and, I
think, fixed it.

So, is this really a bug, or did the parser do the right thing?

If it IS a bug, will this be fixed in 2.3? Should I "officially"
report it?


Thanks for any comments!




More information about the Python-list mailing list