102 if ( ! Py_IsInitialized() ) {
104 PyEval_InitThreads();
108 if ( ! Py_IsInitialized() ) {
110 std::cerr <<
"Error: python has not been intialized; returning." << std::endl;
115 #if PY_VERSION_HEX < 0x03000000
116 char* argv[] = {
const_cast< char*
>(
"root" ) };
118 wchar_t* argv[] = {
const_cast< wchar_t*
>(
L"root" ) };
120 PySys_SetArgv(
sizeof(argv)/
sizeof(argv[0]), argv );
123 PyRun_SimpleString( const_cast< char* >(
"import ROOT" ) );
128 gMainDict = PyModule_GetDict(
129 PyImport_AddModule( const_cast< char* >(
"__main__" ) ) );
130 Py_INCREF( gMainDict );
137 isInitialized =
kTRUE;
151 PyObject* mod = PyImport_ImportModule( mod_name );
164 PyObject* dct = PyModule_GetDict( mod );
168 for (
int i = 0; i < PyList_GET_SIZE( values ); ++i ) {
177 if ( PyErr_Occurred() )
181 std::string fullname = mod_name;
188 Py_XDECREF( pyClName );
197 if ( PyErr_Occurred() )
214 PyObject* old = PyDict_Values( gMainDict );
217 Exec( (std::string(
"execfile(\"" ) + name +
"\")").c_str() );
223 for (
int i = 0; i < PyList_GET_SIZE( current ); ++i ) {
227 if ( ! PySequence_Contains( old, value ) ) {
234 if ( PyErr_Occurred() )
239 if ( (pyModName && pyClName) &&\
252 Py_XDECREF( pyClName );
253 Py_XDECREF( pyModName );
260 Py_DECREF( current );
272 #
if PY_VERSION_HEX < 0x03000000
284 std::cerr <<
"Error: no file name specified." << std::endl;
288 FILE* fp = fopen( name,
"r" );
290 std::cerr <<
"Error: could not open file \"" << name <<
"\"." << std::endl;
295 PyObject* oldargv = PySys_GetObject( const_cast< char* >(
"argv" ) );
299 PyObject*
l = PyList_New( PyList_GET_SIZE( oldargv ) );
300 for (
int i = 0; i < PyList_GET_SIZE( oldargv ); ++i ) {
301 PyObject* item = PyList_GET_ITEM( oldargv, i );
303 PyList_SET_ITEM( l, i, item );
310 #if PY_VERSION_HEX < 0x03000000
311 const char** argv2 =
new const char*[ argc ];
312 for (
int i = 1; i < argc; ++i ) argv2[ i ] = argv[ i-1 ];
313 argv2[ 0 ] = Py_GetProgramName();
314 PySys_SetArgv( argc, const_cast< char** >( argv2 ) );
323 PyRun_FileEx( fp, const_cast< char* >( name ), Py_file_input, gbl, gbl, 1 );
326 Py_XDECREF( result );
331 PySys_SetObject( const_cast< char* >(
"argv" ), oldargv );
332 Py_DECREF( oldargv );
347 PyRun_String( const_cast< char* >( cmd ), Py_file_input, gMainDict, gMainDict );
375 PyRun_String( const_cast< char* >( expr ), Py_eval_input, gMainDict, gMainDict );
386 PyFloat_Check( result ) || PyLong_Check( result ) || PyInt_Check( result ) )
391 if ( pyclass != 0 ) {
401 Py_DECREF( pyclass );
427 TClass* klass =
object->IsA();
432 Bool_t bOk = PyDict_SetItemString( gMainDict, const_cast< char* >( label ), bound ) == 0;
453 PyRun_InteractiveLoop( stdin, const_cast< char* >(
"\0" ) );
531 void* addr,
const char* classname,
Bool_t python_owns )
#define PyBytes_CheckExact
static Bool_t Exec(const char *cmd)
Execute a python statement (e.g. "import ROOT").
RooArgList L(const RooAbsArg &v1)
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.
std::vector< double > values
struct staticInitHelper gbl
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 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
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)
virtual const char * GetName() const
Returns name of object.
The ROOT global object gROOT contains a list of all defined classes.
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.
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.
static Bool_t ObjectProxy_CheckExact(PyObject *pyobject)
Test whether the type of the given pyobject is ObjectProxy type.
Mother of all ROOT objects.
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)
ClassImp(TPython) static PyObject *gMainDict=0
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.