views:

267

answers:

4

Out of curiosity, are there many compilers out there which target .pyc files?

After a bit of Googling, the only two I can find are:

  • unholy: why_'s Ruby-to-pyc compiler
  • Python: The PSF's Python to pyc compiler

So… Are there any more?

(as a side note, I got thinking about this because I want to write a Scheme-to-pyc compiler)

(as a second side note, I'm not under any illusion that a Scheme-to-pyc compiler would be useful, but it would give me an incredible excuse to learn some internals of both Scheme and Python)

+4  A: 

"I want to write a Scheme-to-pyc compiler".

My brain hurts! Why would you want to do that? Python byte code is an intermediate language specifically designed to meet the needs of the Python language and designed to run on Python virtual machines that, again, have been tailored to the needs of Python. Some of the most important areas of Python development these days are moving Python to other "virtual machines", such as Jython (JVM), IronPython (.NET), PyPy and the Unladen Swallow project (moving CPython to an LLVM-based representation). Trying to squeeze the syntax and semantics of another, very different language (Scheme) into the intermediate representation of another high-level language seems to be attacking the problem (whatever the problem is) at the wrong level. So, in general, it doesn't seem like there would be many .pyc compilers out there and there's a good reason for that.

Ned Deily
Why would I want to do that? What's *not* to like about it?! I get to learn about the internals of both Scheme AND Python (albeit at different levels… But still)!
David Wolever
But, more seriously, you've got a good point that I didn't consider: pyc, unlike general bytecode (.swf or .class?), is designed only to suit Python… So it doesn't make much sense to target it.
David Wolever
At least, it doesn't make sense if your goal is to produce something useful :P
David Wolever
+1  A: 

I suggest you focus on CPython.

http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html

Rather than a Scheme to .pyc translator, I suggest you write a Scheme to Python translator, and then let CPython handle the conversion to .pyc. (There is precedent for doing it this way; the first C++ compiler was Cfront which translated C++ into C, and then let the system C compiler do the rest.)

From what I know of Scheme, it wouldn't be that difficult to translate Scheme to Python.

One warning: the Python virtual machine is probably not as fast for Scheme as Scheme itself. For example, Python doesn't automatically turn tail recursion into iteration; and Python has a relatively shallow stack, so you would actually need to turn tail recursion to iteration for your translator.

As a bonus, once Unladen Swallow speeds up Python, your Scheme-to-Python translator would benefit, and at that point might even become practical!

If this seems like a fun project to you, I say go for it. Not every project has to be immediately practical.

P.S. If you want a project that is somewhat more practical, you might want to write an AWK to Python translator. That way, people with legacy AWK scripts could easily make the leap forward to Python!

steveha
Um, he already has CPython in his list: that's the PSF's Python to pyc compiler.
Ned Deily
Hhhmm… Awk to Python… That's even more disgusting… But *does* have the drawback of being potentially useful…
David Wolever
The main reason I suggested it is, I answered a question about making a standalone binary from an AWK file. Python has several ways to make a standalone binary (e.g. for Windows, Py2Exe: http://www.py2exe.org/). If you could translate AWK to Python you could then make a standalone binary!
steveha
@Ned Deily: you are only saying that because it's true... fine, I'll edit my answer.
steveha
+4  A: 

I wrote a compiler several years ago which accepted a lisp-like language called "Noodle" and produced Python bytecode. While it never became particularly useful, it was a tremendously good learning experience both for understanding Common Lisp better (I copied several of its features) and for understanding Python better.

I can think of two particular cases when it might be useful to target Python bytecode directly, instead of producing Python and passing it on to a Python compiler:

  1. Full closures: in Python before 3.0 (before the nonlocal keyword), you can't modify the value of a closed-over variable without resorting to bytecode hackery. You can mutate values instead, so it's common practice to have a closure referencing a list, for example, and changing the first element in it from the inner scope. That can get real annoying. The restriction is part of the syntax, though, not the Python VM. My language had explicit variable declaration, so it successfully provided "normal" closures with modifiable closed-over values.
  2. Getting at a traceback object without referencing any builtins. Real niche case, for sure, but I used it to break an early version of the "safelite" jail. See my posting about it.

So yeah, it's probably way more work than it's worth, but I enjoyed it, and you might too.

the paul
Thanks for the comment, Paul! I'm glad to know that I'm not insane :) Also, welcome to Stack Overflow.
David Wolever
+1  A: 

Just for your interest, I have written a toy compiler from a simple LISP to Python. Practically, this is a LISP to pyc compiler.

Have a look: sinC - The tiniest LISP compiler

Bernhard Kausler
Cool – that looks neat. Thanks.
David Wolever