Skip to content

CPython Integration

brett hartshorn edited this page Mar 14, 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)

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

https://github.com/rusthon/Rusthon/blob/master/examples/cpython_embed.md

https://github.com/rusthon/Rusthon/blob/master/examples/cpython_multithreaded.md

Sidebar

Clone this wiki locally