Accessing the Python interpreter from C++.
The TPython class allows for access to python objects from Cling. The current functionality is only basic: ROOT objects and builtin types can freely cross the boundary between the two interpreters, python objects can be instantiated and their methods can be called. All other cross-coding is based on strings that are run on the python interpreter.
Examples:
And with a python file MyPyClass.py like this:
one can load a python module, and use the class. Casts are necessary as the type information can not be otherwise derived.
It is possible to switch between interpreters by calling TPython::Prompt() on the Cling side, while returning with ^D (EOF). State is preserved between successive switches.
The API part provides (direct) C++ access to the bindings functionality of PyROOT. It allows verifying that you deal with a PyROOT python object in the first place (CPPInstance_Check for CPPInstance and any derived types, as well as CPPInstance_CheckExact for CPPInstance's only); and it allows conversions of void* to an CPPInstance and vice versa.
Public Member Functions | |
| virtual | ~TPython () |
| virtual TClass * | IsA () const |
| virtual void | Streamer (TBuffer &) |
| void | StreamerNVirtual (TBuffer &ClassDef_StreamerNVirtual_b) |
Static Public Member Functions | |
| static Bool_t | Bind (TObject *object, const char *label) |
| Bind a ROOT object with, at the python side, the name "label". | |
| static TClass * | Class () |
| static const char * | Class_Name () |
| static constexpr Version_t | Class_Version () |
| static void * | CPPInstance_AsVoidPtr (PyObject *pyobject) |
| Extract the object pointer held by the CPPInstance pyobject. | |
| static Bool_t | CPPInstance_Check (PyObject *pyobject) |
| Test whether the type of the given pyobject is of CPPInstance type or any derived type. | |
| static Bool_t | CPPInstance_CheckExact (PyObject *pyobject) |
| Test whether the type of the given pyobject is CPPinstance type. | |
| static PyObject * | CPPInstance_FromVoidPtr (void *addr, const char *classname, Bool_t python_owns=kFALSE) |
| Bind the addr to a python object of class defined by classname. | |
| static Bool_t | CPPOverload_Check (PyObject *pyobject) |
| Test whether the type of the given pyobject is of CPPOverload type or any derived type. | |
| static Bool_t | CPPOverload_CheckExact (PyObject *pyobject) |
| Test whether the type of the given pyobject is CPPOverload type. | |
| static const char * | DeclFileName () |
| static Bool_t | Exec (const char *cmd, std::any *result=nullptr, std::string const &resultName="_anyresult") |
| Executes a Python command within the current Python environment. | |
| static void | ExecScript (const char *name, int argc=0, const char **argv=nullptr) |
| Execute a python stand-alone script, with argv CLI arguments. | |
| static Bool_t | Import (const char *name) |
| Import the named python module and create Cling equivalents for its classes and methods. | |
| static void | LoadMacro (const char *name) |
| Execute the give python script as if it were a macro (effectively an execfile in main), and create Cling equivalents for any newly available python classes. | |
| static void | Prompt () |
| Enter an interactive python session (exit with ^D). | |
Static Private Member Functions | |
| static Bool_t | Initialize () |
| Initialization method: setup the python interpreter and load the ROOT module. | |
#include <TPython.h>
Constructor & Destructor Documentation
◆ ~TPython()
Member Function Documentation
◆ Bind()
Bind a ROOT object with, at the python side, the name "label".
Definition at line 404 of file TPython.cxx.
◆ Class()
◆ Class_Name()
◆ Class_Version()
◆ CPPInstance_AsVoidPtr()
|
static |
Extract the object pointer held by the CPPInstance pyobject.
Definition at line 509 of file TPython.cxx.
◆ CPPInstance_Check()
Test whether the type of the given pyobject is of CPPInstance type or any derived type.
Definition at line 448 of file TPython.cxx.
◆ CPPInstance_CheckExact()
Test whether the type of the given pyobject is CPPinstance type.
Definition at line 463 of file TPython.cxx.
◆ CPPInstance_FromVoidPtr()
|
static |
Bind the addr to a python object of class defined by classname.
Definition at line 524 of file TPython.cxx.
◆ CPPOverload_Check()
Test whether the type of the given pyobject is of CPPOverload type or any derived type.
Definition at line 479 of file TPython.cxx.
◆ CPPOverload_CheckExact()
Test whether the type of the given pyobject is CPPOverload type.
Definition at line 494 of file TPython.cxx.
◆ DeclFileName()
◆ Exec()
|
static |
Executes a Python command within the current Python environment.
This function initializes the Python environment if it is not already initialized. It then executes the specified Python command string using the Python C API.
In the Python command, you can change the value of a special TPyResult object returned by TPyBuffer(). If the optional result parameter is non-zero, the result parameter will be swapped with a std::any variable on the Python side. You need to define this variable yourself, and it needs to be of type std::any and its name needs to be "_anyresult" by default. Like this, you can pass information from Python back to C++.
- Parameters
-
cmd The Python command to be executed as a string. result Optional pointer to a std::any object that can be used to transfer results from Python to C++. resultName Name of the Python variable that is swapped over to the std::any result. The default value is "_anyresult".
- Returns
- bool Returns
trueif the command was successfully executed, otherwise returnsfalse.
Definition at line 380 of file TPython.cxx.
◆ ExecScript()
Execute a python stand-alone script, with argv CLI arguments.
example of use: const char* argv[] = { "1", "2", "3" }; TPython::ExecScript( "test.py", sizeof(argv)/sizeof(argv[0]), argv );
Definition at line 336 of file TPython.cxx.
◆ Import()
Import the named python module and create Cling equivalents for its classes and methods.
Definition at line 198 of file TPython.cxx.
◆ Initialize()
|
staticprivate |
Initialization method: setup the python interpreter and load the ROOT module.
Definition at line 128 of file TPython.cxx.
◆ IsA()
◆ LoadMacro()
|
static |
Execute the give python script as if it were a macro (effectively an execfile in main), and create Cling equivalents for any newly available python classes.
Definition at line 258 of file TPython.cxx.
◆ Prompt()
|
static |
Enter an interactive python session (exit with ^D).
State is preserved between successive calls.
Definition at line 431 of file TPython.cxx.
◆ Streamer()
◆ StreamerNVirtual()
The documentation for this class was generated from the following files:
- bindings/tpython/inc/TPython.h
- bindings/tpython/src/TPython.cxx

