Search code examples
pythoncompiler-constructionpyc

Compilers targeting .pyc files?


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)


Solution

  • 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.