Skip to content

CPython Integration

brett hartshorn edited this page Apr 3, 2015 · 14 revisions

CPython2.7 can optionally be embedded in your final exe using the C++ backend. You include Python.h and can then import cpython and interact with the regular Python runtime. Your exe will be linked to libpython2.7.so on Linux. Methods are called on PyObjects using the special -> syntax.

cpythonintegration

Import the cpython module, first initalize this returns the python-thread-state and releases the GIL. At the end of your program you need to pass the thread state to cpython.finalize.

import cpython
def main():
  ts = cpython.initalize()
  with gil:
    do stuff with PythonVM
  cpython.finalize(ts)

Examples

Syntax

-> is special syntax for PyObjects that is used inplace of the normal dot . operator. Below calls somemethod on pyob.

pyob->somemethod()

All code that interacts with the PythonVM needs to be blocked inside a with gil: block.

with gil:
   pyob->method()

Conversion back to C++ native types. Below would convert the result of method to an int.

a = pyob->method() as int

Conversion o a C++ native type to a Python Object type, prefix py to the type.

x = 1
y = "hi"
pyob->method( x as pyint, y as pystring )

CPython Builtins

  • pytype returns the name of the object type as a string (std::string). This is the same as in normal python type(o).__name__.
  • ispyinstance returns true or false if the PyObject is that type, if used in an if-test then the test target will be converted to a native C++ type in the if-body.
  • len returns the length of the python list or other object, same as calling len(o) in regular python.

Lists and Iteration

You can iterate over a Python list using it as an iterator.

for pyob in a->somelist:

Iterate and cast to a native C++ type. The list must contain only that type

for pyob as int in a->somelist:

Iterate and cast to a native C++ type, if possible. The list may contain mixed types.

for pyob in a->somelist:
    if ispyinstance(pyob, int):
    elif ispyinstance(pyob, string):

Index a list, get and set items. Below converts a std::string to a pystring and assigns it to a python list, then the same string is fetched from the list, and converted back to a std::string.

pyob->somelist[0] = 'hello' as pystring
a = pyob->somelist[0] as string

Items can be appended to a list using the append method. If the item is a C++ native type, it must be converted with as some pytype.

pyob->somelist->append( value as pyint )

Multi-threaded CPython (no GIL)

We also want to add the option of using a forked version of CPython2.7 that removes the GIL and replaces it with fine grained locks. Work on the fork is ongoing here: https://github.com/rusthon/Rusthon/wiki/Npthon