102 if ( ! Py_IsInitialized() ) {
104 #if PY_VERSION_HEX < 0x03020000 105 PyEval_InitThreads();
108 #if PY_VERSION_HEX >= 0x03020000 109 PyEval_InitThreads();
113 if ( ! Py_IsInitialized() ) {
115 std::cerr <<
"Error: python has not been intialized; returning." << std::endl;
120 #if PY_VERSION_HEX < 0x03000000 121 char* argv[] = {
const_cast< char*
>(
"root" ) };
123 wchar_t* argv[] = {
const_cast< wchar_t*
>(
L"root" ) };
125 PySys_SetArgv(
sizeof(argv)/
sizeof(argv[0]), argv );
128 PyRun_SimpleString( const_cast< char* >(
"import ROOT" ) );
134 PyImport_AddModule( const_cast< char* >(
"__main__" ) ) );
142 isInitialized =
kTRUE;
156 PyObject* mod = PyImport_ImportModule( mod_name );
169 PyObject* dct = PyModule_GetDict( mod );
172 PyObject* values = PyDict_Values( dct );
173 for (
int i = 0; i < PyList_GET_SIZE( values ); ++i ) {
174 PyObject* value = PyList_GET_ITEM( values, i );
185 if ( PyErr_Occurred() )
189 std::string fullname = mod_name;
196 Py_XDECREF( pyClName );
205 if ( PyErr_Occurred() )
225 #if PY_VERSION_HEX < 0x03000000 226 Exec( (std::string(
"execfile(\"" ) + name +
"\")").c_str() );
228 Exec( (std::string(
"__pyroot_f = open(\"" ) + name +
"\"); " 229 "exec(__pyroot_f.read()); " 230 "__pyroot_f.close(); del __pyroot_f" ).c_str() );
237 for (
int i = 0; i < PyList_GET_SIZE( current ); ++i ) {
238 PyObject* value = PyList_GET_ITEM( current, i );
241 if ( ! PySequence_Contains( old, value ) ) {
248 if ( PyErr_Occurred() )
253 if ( (pyModName && pyClName) &&\
266 Py_XDECREF( pyClName );
267 Py_XDECREF( pyModName );
274 Py_DECREF( current );
286 #
if PY_VERSION_HEX < 0x03000000
298 std::cerr <<
"Error: no file name specified." << std::endl;
302 FILE* fp = fopen( name,
"r" );
304 std::cerr <<
"Error: could not open file \"" << name <<
"\"." << std::endl;
309 PyObject* oldargv = PySys_GetObject( const_cast< char* >(
"argv" ) );
313 PyObject*
l = PyList_New( PyList_GET_SIZE( oldargv ) );
314 for (
int i = 0; i < PyList_GET_SIZE( oldargv ); ++i ) {
315 PyObject* item = PyList_GET_ITEM( oldargv, i );
317 PyList_SET_ITEM( l, i, item );
324 #if PY_VERSION_HEX < 0x03000000 325 const char** argv2 =
new const char*[ argc ];
326 for (
int i = 1; i < argc; ++i ) argv2[ i ] = argv[ i-1 ];
327 argv2[ 0 ] = Py_GetProgramName();
328 PySys_SetArgv( argc, const_cast< char** >( argv2 ) );
337 PyRun_FileEx( fp, const_cast< char* >( name ), Py_file_input, gbl, gbl, 1 );
340 Py_XDECREF( result );
345 PySys_SetObject( const_cast< char* >(
"argv" ), oldargv );
346 Py_DECREF( oldargv );
400 PyFloat_Check( result ) || PyLong_Check( result ) || PyInt_Check( result ) )
405 if ( pyclass != 0 ) {
415 Py_DECREF( pyclass );
441 TClass* klass =
object->IsA();
446 Bool_t bOk = PyDict_SetItemString(
gMainDict, const_cast< char* >( label ), bound ) == 0;
467 PyRun_InteractiveLoop( stdin, const_cast< char* >(
"\0" ) );
545 void* addr,
const char* classname,
Bool_t python_owns )
virtual const char * GetName() const
Returns name of object.
static Bool_t Exec(const char *cmd)
Execute a python statement (e.g. "import ROOT").
static Bool_t MethodProxy_Check(PyObject *pyobject)
Test whether the type of the given pyobject is of MethodProxy type or any derived type...
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 Bool_t Initialize()
static Bool_t MethodProxy_CheckExact(PyObject *pyobject)
Test whether the type of the given pyobject is MethodProxy type.
static void Prompt()
Enter an interactive python session (exit with ^D).
R__EXTERN PyObject * gRootModule
static const TPyReturn Eval(const char *expr)
Evaluate a python expression (e.g.
R__EXTERN PyObject * gModule
static constexpr double L
static PyObject * ObjectProxy_FromVoidPtr(void *addr, const char *classname, Bool_t python_owns=kFALSE)
Bind the addr to a python object of class defined by classname.
Bool_t ObjectProxy_CheckExact(T *object)
#define PyROOT_PyUnicode_AsString
static Bool_t ObjectProxy_Check(PyObject *pyobject)
Test whether the type of the given pyobject is of ObjectProxy type or any derived type...
R__EXTERN PyObject * gBases
Bool_t ObjectProxy_Check(T *object)
R__EXTERN PyObject * gClass
void Initialize(Bool_t useTMVAStyle=kTRUE)
PyObject * BindCppObjectNoCast(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, Bool_t isRef=kFALSE, Bool_t isValue=kFALSE)
only known or knowable objects will be bound (null object is ok)
The ROOT global object gROOT contains a list of all defined classes.
static PyObject * gMainDict
TCppScope_t GetScope(const std::string &scope_name)
R__EXTERN PyObject * gName
static Bool_t Import(const char *name)
Import the named python module and create Cling equivalents for its classes and methods.
#define PyROOT_PyUnicode_Check
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
R__EXTERN PyObject * gCppName
static Bool_t ObjectProxy_CheckExact(PyObject *pyobject)
Test whether the type of the given pyobject is ObjectProxy type.
Mother of all ROOT objects.
#define PyROOT_PyUnicode_CheckExact
static Bool_t Bind(TObject *object, const char *label)
Bind a ROOT object with, at the python side, the name "label".
Bool_t MethodProxy_Check(T *object)
Bool_t MethodProxy_CheckExact(T *object)
PyObject * BindCppObject(Cppyy::TCppObject_t object, Cppyy::TCppType_t klass, Bool_t isRef=kFALSE)
if the object is a null pointer, return a typed one (as needed for overloading)
static void * ObjectProxy_AsVoidPtr(PyObject *pyobject)
Extract the object pointer held by the ObjectProxy pyobject.
static void ExecScript(const char *name, int argc=0, const char **argv=0)
Execute a python stand-alone script, with argv CLI arguments.