85 static PyObject* gMainDict = 0;
88 R__EXTERN PyObject* gRootModule;
93 Bool_t TPython::Initialize()
98 static Bool_t isInitialized = kFALSE;
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" ) );
133 gMainDict = PyModule_GetDict(
134 PyImport_AddModule( const_cast< char* >(
"__main__" ) ) );
135 Py_INCREF( gMainDict );
139 gROOT->AddClassGenerator(
new TPyClassGenerator );
142 isInitialized = kTRUE;
150 Bool_t TPython::Import(
const char* mod_name )
153 if ( ! Initialize() )
156 PyObject* mod = PyImport_ImportModule( mod_name );
164 PyModule_AddObject( PyROOT::gRootModule, mod_name, mod );
167 TClass::GetClass( mod_name, kTRUE );
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 );
178 if ( PyClass_Check( value ) || PyObject_HasAttr( value, PyROOT::PyStrings::gBases ) ) {
180 PyObject* pyClName = PyObject_GetAttr( value, PyROOT::PyStrings::gCppName );
182 pyClName = PyObject_GetAttr( value, PyROOT::PyStrings::gName );
185 if ( PyErr_Occurred() )
189 std::string fullname = mod_name;
191 fullname += PyROOT_PyUnicode_AsString( pyClName );
194 TClass::GetClass( fullname.c_str(), kTRUE );
196 Py_XDECREF( pyClName );
205 if ( PyErr_Occurred() )
215 void TPython::LoadMacro(
const char* name )
218 if ( ! Initialize() )
222 PyObject* old = PyDict_Values( gMainDict );
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() );
234 PyObject* current = PyDict_Values( gMainDict );
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 ) ) {
243 if ( PyClass_Check( value ) || PyObject_HasAttr( value, PyROOT::PyStrings::gBases ) ) {
245 PyObject* pyModName = PyObject_GetAttr( value, PyROOT::PyStrings::gModule );
246 PyObject* pyClName = PyObject_GetAttr( value, PyROOT::PyStrings::gName );
248 if ( PyErr_Occurred() )
253 if ( (pyModName && pyClName) &&\
254 ( (PyROOT_PyUnicode_CheckExact( pyModName ) && PyROOT_PyUnicode_CheckExact( pyClName )) ||\
255 (PyROOT_PyUnicode_Check( pyModName ) && PyROOT_PyUnicode_Check( pyClName ))\
258 std::string fullname = PyROOT_PyUnicode_AsString( pyModName );
260 fullname += PyROOT_PyUnicode_AsString( pyClName );
263 TClass::GetClass( fullname.c_str(), kTRUE );
266 Py_XDECREF( pyClName );
267 Py_XDECREF( pyModName );
274 Py_DECREF( current );
285 void TPython::ExecScript(
const char* name,
int argc,
const char**
286 #
if PY_VERSION_HEX < 0x03000000
293 if ( ! Initialize() )
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 ) );
335 PyObject* gbl = PyDict_Copy( gMainDict );
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 );
353 Bool_t TPython::Exec(
const char* cmd )
356 if ( ! Initialize() )
361 PyRun_String( const_cast< char* >( cmd ), Py_file_input, gMainDict, gMainDict );
381 const TPyReturn TPython::Eval(
const char* expr )
384 if ( ! Initialize() )
389 PyRun_String( const_cast< char* >( expr ), Py_eval_input, gMainDict, gMainDict );
398 if ( result == Py_None || PyROOT::ObjectProxy_Check( result ) ||
399 PyBytes_Check( result ) ||
400 PyFloat_Check( result ) || PyLong_Check( result ) || PyInt_Check( result ) )
401 return TPyReturn( result );
404 PyObject* pyclass = PyObject_GetAttr( result, PyROOT::PyStrings::gClass );
405 if ( pyclass != 0 ) {
407 PyObject* name = PyObject_GetAttr( pyclass, PyROOT::PyStrings::gName );
408 PyObject* module = PyObject_GetAttr( pyclass, PyROOT::PyStrings::gModule );
412 std::string( PyROOT_PyUnicode_AsString( module ) ) +
'.' + PyROOT_PyUnicode_AsString( name );
415 Py_DECREF( pyclass );
418 TClass* klass = TClass::GetClass( qname.c_str() );
422 return TPyReturn( result );
434 Bool_t TPython::Bind( TObject*
object,
const char* label )
437 if ( ! (
object && Initialize() ) )
441 TClass* klass =
object->IsA();
443 PyObject* bound = PyROOT::BindCppObject( (
void*)
object, klass->GetName() );
446 Bool_t bOk = PyDict_SetItemString( gMainDict, const_cast< char* >( label ), bound ) == 0;
460 void TPython::Prompt() {
462 if ( ! Initialize() ) {
467 PyRun_InteractiveLoop( stdin, const_cast< char* >(
"\0" ) );
474 Bool_t TPython::ObjectProxy_Check( PyObject* pyobject )
477 if ( ! Initialize() )
481 return PyROOT::ObjectProxy_Check( pyobject );
487 Bool_t TPython::ObjectProxy_CheckExact( PyObject* pyobject )
490 if ( ! Initialize() )
494 return PyROOT::ObjectProxy_CheckExact( pyobject );
501 Bool_t TPython::MethodProxy_Check( PyObject* pyobject )
504 if ( ! Initialize() )
508 return PyROOT::MethodProxy_Check( pyobject );
514 Bool_t TPython::MethodProxy_CheckExact( PyObject* pyobject )
517 if ( ! Initialize() )
521 return PyROOT::MethodProxy_CheckExact( pyobject );
527 void* TPython::ObjectProxy_AsVoidPtr( PyObject* pyobject )
530 if ( ! Initialize() )
534 if ( ! PyROOT::ObjectProxy_Check( pyobject ) )
538 return ((PyROOT::ObjectProxy*)pyobject)->GetObject();
544 PyObject* TPython::ObjectProxy_FromVoidPtr(
545 void* addr,
const char* classname, Bool_t python_owns )
548 if ( ! Initialize() )
552 PyObject* pyobject = PyROOT::BindCppObjectNoCast( addr, Cppyy::GetScope( classname ), kFALSE );
555 if ( python_owns && PyROOT::ObjectProxy_Check( pyobject ) )
556 ((PyROOT::ObjectProxy*)pyobject)->HoldOn();