Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TPython.h
Go to the documentation of this file.
1// Author: Enric Tejedor CERN 08/2019
2// Original PyROOT code by Wim Lavrijsen, LBL
3
4/*************************************************************************
5 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TPython
13#define ROOT_TPython
14
15//////////////////////////////////////////////////////////////////////////////
16// //
17// TPython //
18// //
19// Access to the python interpreter and API onto PyROOT. //
20// //
21//////////////////////////////////////////////////////////////////////////////
22
23// ROOT
24#include "TObject.h"
25
26#include <any>
27#include <cstdint>
28
29// Python
30struct _object;
31typedef _object PyObject;
32
33namespace ROOT {
34namespace Internal {
35
36// Internal helper for PyROOT to swap with an object is at a specific address.
37template<class T>
38inline void SwapWithObjAtAddr(T &a, std::intptr_t b) { std::swap(a, *reinterpret_cast<T*>(b)); }
39
40}
41}
42
43class TPython {
44
45private:
46 static Bool_t Initialize();
47
48public:
49 // import a python module, making its classes available
50 static Bool_t Import(const char *name);
51
52 // load a python script as if it were a macro
53 static void LoadMacro(const char *name);
54
55 // execute a python stand-alone script, with argv CLI arguments
56 static void ExecScript(const char *name, int argc = 0, const char **argv = nullptr);
57
58 // execute a python statement (e.g. "import ROOT" )
59 static Bool_t Exec(const char *cmd, std::any *result = nullptr, std::string const& resultName="_anyresult");
60
61 // bind a ROOT object with, at the python side, the name "label"
62 static Bool_t Bind(TObject *object, const char *label);
63
64 // enter an interactive python session (exit with ^D)
65 static void Prompt();
66
67 // type verifiers for CPPInstance
68 static Bool_t CPPInstance_Check(PyObject *pyobject);
69 static Bool_t CPPInstance_CheckExact(PyObject *pyobject);
70
71 // type verifiers for CPPOverload
72 static Bool_t CPPOverload_Check(PyObject *pyobject);
73 static Bool_t CPPOverload_CheckExact(PyObject *pyobject);
74
75 // CPPInstance to void* conversion
76 static void *CPPInstance_AsVoidPtr(PyObject *pyobject);
77
78 // void* to CPPInstance conversion, returns a new reference
79 static PyObject *CPPInstance_FromVoidPtr(void *addr, const char *classname, Bool_t python_owns = kFALSE);
80
81 virtual ~TPython() {}
82 ClassDef(TPython, 0) // Access to the python interpreter
83};
84
85#endif
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
#define ClassDef(name, id)
Definition Rtypes.h:342
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char name[80]
Definition TGX11.cxx:110
_object PyObject
Definition TPython.h:31
Mother of all ROOT objects.
Definition TObject.h:41
Accessing the Python interpreter from C++.
Definition TPython.h:43
static void Prompt()
Enter an interactive python session (exit with ^D).
Definition TPython.cxx:487
static Bool_t CPPOverload_Check(PyObject *pyobject)
Test whether the type of the given pyobject is of CPPOverload type or any derived type.
Definition TPython.cxx:535
static void * CPPInstance_AsVoidPtr(PyObject *pyobject)
Extract the object pointer held by the CPPInstance pyobject.
Definition TPython.cxx:565
static void ExecScript(const char *name, int argc=0, const char **argv=nullptr)
Execute a python stand-alone script, with argv CLI arguments.
Definition TPython.cxx:381
static Bool_t Import(const char *name)
Import the named python module and create Cling equivalents for its classes and methods.
Definition TPython.cxx:237
static Bool_t CPPInstance_CheckExact(PyObject *pyobject)
Test whether the type of the given pyobject is CPPinstance type.
Definition TPython.cxx:519
static Bool_t Bind(TObject *object, const char *label)
Bind a ROOT object with, at the python side, the name "label".
Definition TPython.cxx:459
static void LoadMacro(const char *name)
Execute the give python script as if it were a macro (effectively an execfile in main),...
Definition TPython.cxx:305
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.
Definition TPython.cxx:425
static Bool_t CPPOverload_CheckExact(PyObject *pyobject)
Test whether the type of the given pyobject is CPPOverload type.
Definition TPython.cxx:550
static Bool_t Initialize()
Initialization method: setup the python interpreter and load the ROOT module.
Definition TPython.cxx:137
virtual ~TPython()
Definition TPython.h:81
static Bool_t CPPInstance_Check(PyObject *pyobject)
Test whether the type of the given pyobject is of CPPInstance type or any derived type.
Definition TPython.cxx:504
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.
Definition TPython.cxx:580
void SwapWithObjAtAddr(T &a, std::intptr_t b)
Definition TPython.h:38
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...