CodePy is a C metaprogramming toolkit for Python. It handles two aspects of metaprogramming:
Both capabilities are meant to be used together, but also work on their own. In particular, the code generation facilities work well in conjunction with PyCuda. Dynamic compilation and linking are so far only supported in Linux with the GNU toolchain.
This sample CodePy program builds a Boost.Python C++ module that returns the string “hello world”:
from cgen import * from codepy.bpl import BoostPythonModule mod = BoostPythonModule() mod.add_function( FunctionBody( FunctionDeclaration(Const(Pointer(Value("char", "greet"))), ), Block([Statement('return "hello world"')]) )) from codepy.toolchain import guess_toolchain cmod = mod.compile(guess_toolchain()) print cmod.greet()
You may notice that the above code snippet refers to Boost.Python. Boost.Python is a Python wrapper generator library for C++. CodePy does not depend on it being available, but its use is strongly encouraged–otherwise the generated sourcecode would need to use another wrapper generator or talk directly to the Python C API to make its functionality accessible from Python. Boost.Python is only used at run time. CodePy has no install-time dependencies
Instructions on how to install Boost.Python are available. As described in the instructions, CodePy looks for the installation location of Boost.Python in $HOME/.aksetup-defaults.py and /etc/aksetup-defaults.py.
Boost.Python is not needed at all if CodePy is used to generate code for PyCuda.