Skip to content

CPython Integration

brett hartshorn edited this page Mar 19, 2015 · 14 revisions

cpythonintegration

Embed CPython in C++ Exe

Links your final executeable to libpython.

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 )

Sidebar

Clone this wiki locally