-
Notifications
You must be signed in to change notification settings - Fork 51
CPython Integration
brett hartshorn edited this page Mar 19, 2015
·
14 revisions
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)
->
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 )
- 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.