python compiler package questions

Bengt Richter bokr at oz.net
Wed Jan 8 11:32:57 EST 2003


On 8 Jan 2003 05:45:46 -0800, pschmidt at omnimn.com (Phil Schmidt) wrote:

>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?
Maybe have a look at pippy (google for python pippy)

>
>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?
>
It looks to me like a bug. Note that compiler.compile doesn't agree
with the builtin compile (2.2.2 on windows):

 >>> code = compiler.compile("""\
 ... def afunc(a=0, h):
 ...     print a
 ...     a = a * 2
 ...     print a, 'hello'
 ... """,'<string to compiler.compile>','exec')

 >>> bico = compile("""\
 ... def afunc(a=0, h):
 ...     print a
 ...     a = a * 2
 ...     print a, 'hello'
 ... """,'<string to builtin compile>','exec')
 Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<string to builtin compile>", line 1
 SyntaxError: non-default argument follows default argument

Regards,
Bengt Richter




More information about the Python-list mailing list