Grumpy is a Python to Go source code transcompiler and runtime.
The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:
Grumpy is a Python to Go source code transcompiler and runtime that is intended to be a near drop-in replacement for CPython 2.7. The key difference is that it compiles Python source code to Go source code which is then compiled to native code, rather than to bytecode. This means that Grumpy has no VM. The compiled Go source code is a series of calls to the Grumpy runtime, a Go library serving a similar purpose to the Python C API (although the API is incompatible with CPython's).
: These dynamic features of CPython are not supported by Grumpy because Grumpy modules consist of statically-compiled Go code. Supporting dynamic execution would require bundling Grumpy programs with the compilation toolchain, which would be unwieldy and impractically slow.
C extension modules: Grumpy has a different API and object layout than CPython and so supporting C extensions would be difficult. In principle it's possible to support them via an API bridge layer like the one thatJyNI provides for Jython, but it would be hard to maintain and would add significant overhead when calling into and out of extension modules.
There are three basic categories of incomplete functionality:
Builtin functions and types: There are a number of missing functions and types in
that have not yet been implemented. There are also a lot of methods on builtin types that are missing. 3. Standard library: The Python standard library is very large and much of it is pure Python, so as the language features and builtins get filled out, many modules will just work. But there are also a number of libraries in CPython that are C extension modules which will need to be rewritten.
The simplest way to execute a Grumpy program is to use
, which wraps a shell script called grumprun that takes Python code on stdin and builds and runs the code under Grumpy. All of the commands below are assumed to be run from the root directory of the Grumpy source code distribution:
echo "print 'hello, world'" | make run
For more complicated programs, you'll want to compile your Python source code to Go using grumpc (the Grumpy compiler) and then build the Go code using
. Since Grumpy programs are statically linked, all the modules in a program must be findable by the Grumpy toolchain on the GOPATH. Grumpy looks for Go packages corresponding to Python modules in the __python__ subdirectory of the GOPATH. By convention, this subdirectory is also used for staging Python source code, making it similar to the PYTHONPATH.
The first step is to set up the shell so that the Grumpy toolchain and libraries can be found. From the root directory of the Grumpy source distribution run:
make export PATH=$PWD/build/bin:$PATH export GOPATH=$PWD/build export PYTHONPATH=$PWD/build/lib/python2.7/site-packages
You will know things are working if you see the expected output from this command:
echo 'import sys; print sys.version' | grumprun
Next, we will write our simple Python module into the __python__ directory:
echo 'def hello(): print "hello, world"' \> $GOPATH/src/\_\_python\_\_/hello.py
To build a Go package from our Python script, run the following:
mkdir -p $GOPATH/src/\_\_python\_\_/hello grumpc -modname=hello $GOPATH/src/\_\_python\_\_/hello.py \> \ $GOPATH/src/\_\_python\_\_/hello/module.go
You should now be able to build a Go program that imports the package "__python__/hello". We can also import this module into Python programs that are built using grumprun:
echo 'from hello import hello; hello()' | grumprun
grumprun is doing a few things under the hood here:
on the main package generated in step 2.
There are three main components and depending on what kind of feature you're writing, you may need to change one or more of these.
Grumpy converts Python programs into Go programs and
is the tool responsible for parsing Python code and generating Go code from it.
is written in Python and uses the [
](https://github.com/m-labs/pythonparser)module to accomplish parsing.
The grumpc script itself lives at
. It is supported by a number of Python modules in the
The Go code generated by
performs operations on data structures that represent Python objects in running Grumpy programs. These data structures and operations are defined in the
Go library (source is in the runtime subdir of the source distribution). This runtime is analogous to the Python C API and many of the structures and operations defined by
have counterparts in CPython.
Much of the Python standard library is written in Python and thus "just works" in Grumpy. These parts of the standard library are copied from CPython 2.7 (possibly with light modifications). For licensing reasons, these files are kept in the
The parts of the standard library that cannot be written in pure Python, e.g. file and directory operations, are kept in the
subdir. In CPython these kinds of modules are written as C extensions. In Grumpy they are written in Python but they use native Go extensions to access facilities not otherwise available in Python.
: Python package implementating Python -> Go transcompilation logic.
: Grumpy-specific Python standard library implementation.
: Go source code for the Grumpy runtime library.
: Pure Python standard libraries copied from theOuroboros project.
: Pure Python standard libraries copied from PyPy.
: Pure Python standard libraries copied from CPython.
: Transcompilation and utility binaries.