00001
00002
00003
00004
00005 #include "PyROOT.h"
00006 #include "PyStrings.h"
00007 #include "TPython.h"
00008 #include "ObjectProxy.h"
00009 #include "RootWrapper.h"
00010 #include "TPyClassGenerator.h"
00011
00012
00013 #include "TROOT.h"
00014 #include "TObject.h"
00015
00016
00017 #include <stdio.h>
00018 #include <Riostream.h>
00019 #include <string>
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 ClassImp(TPython)
00082 static PyObject* gMainDict = 0;
00083
00084
00085
00086 Bool_t TPython::Initialize()
00087 {
00088
00089
00090
00091 static Bool_t isInitialized = kFALSE;
00092 if ( isInitialized )
00093 return kTRUE;
00094
00095 if ( ! Py_IsInitialized() ) {
00096
00097 PyEval_InitThreads();
00098 Py_Initialize();
00099
00100
00101 if ( ! Py_IsInitialized() ) {
00102
00103 std::cerr << "Error: python has not been intialized; returning." << std::endl;
00104 return kFALSE;
00105 }
00106
00107
00108 #if PY_VERSION_HEX < 0x03000000
00109 char* argv[] = { const_cast< char* >( "root" ) };
00110 #else
00111 wchar_t* argv[] = { const_cast< wchar_t* >( L"root" ) };
00112 #endif
00113 PySys_SetArgv( sizeof(argv)/sizeof(argv[0]), argv );
00114
00115
00116 PyRun_SimpleString( const_cast< char* >( "import ROOT" ) );
00117 }
00118
00119 if ( ! gMainDict ) {
00120
00121 gMainDict = PyModule_GetDict(
00122 PyImport_AddModule( const_cast< char* >( "__main__" ) ) );
00123 Py_INCREF( gMainDict );
00124 }
00125
00126
00127 gROOT->AddClassGenerator( new TPyClassGenerator );
00128
00129
00130 isInitialized = kTRUE;
00131 return kTRUE;
00132 }
00133
00134
00135 void TPython::LoadMacro( const char* name )
00136 {
00137
00138
00139
00140
00141
00142 if ( ! Initialize() )
00143 return;
00144
00145
00146 PyObject* old = PyDict_Values( gMainDict );
00147
00148
00149 Exec( (std::string( "execfile(\"" ) + name + "\")").c_str() );
00150
00151
00152 PyObject* current = PyDict_Values( gMainDict );
00153
00154
00155 for ( int i = 0; i < PyList_GET_SIZE( current ); ++i ) {
00156 PyObject* value = PyList_GET_ITEM( current, i );
00157 Py_INCREF( value );
00158
00159 if ( ! PySequence_Contains( old, value ) ) {
00160
00161 if ( PyClass_Check( value ) || PyObject_HasAttr( value, PyROOT::PyStrings::gBases ) ) {
00162
00163 PyObject* pyModName = PyObject_GetAttr( value, PyROOT::PyStrings::gModule );
00164 PyObject* pyClName = PyObject_GetAttr( value, PyROOT::PyStrings::gName );
00165
00166 if ( PyErr_Occurred() )
00167 PyErr_Clear();
00168
00169
00170
00171 if ( (pyModName && pyClName) &&\
00172 ( (PyBytes_CheckExact( pyModName ) && PyBytes_CheckExact( pyClName )) ||\
00173 (PyBytes_Check( pyModName ) && PyBytes_Check( pyClName ))\
00174 ) ) {
00175
00176 std::string fullname = PyROOT_PyUnicode_AsString( pyModName );
00177 fullname += '.';
00178 fullname += PyROOT_PyUnicode_AsString( pyClName );
00179
00180
00181 TClass::GetClass( fullname.c_str(), kTRUE );
00182 }
00183
00184 Py_XDECREF( pyClName );
00185 Py_XDECREF( pyModName );
00186 }
00187 }
00188
00189 Py_DECREF( value );
00190 }
00191
00192 Py_DECREF( current );
00193 Py_DECREF( old );
00194 }
00195
00196
00197 void TPython::ExecScript( const char* name, int argc, const char** argv )
00198 {
00199
00200
00201
00202
00203
00204
00205
00206
00207 if ( ! Initialize() )
00208 return;
00209
00210
00211 if ( ! name ) {
00212 std::cerr << "Error: no file name specified." << std::endl;
00213 return;
00214 }
00215
00216 FILE* fp = fopen( name, "r" );
00217 if ( ! fp ) {
00218 std::cerr << "Error: could not open file \"" << name << "\"." << std::endl;
00219 return;
00220 }
00221
00222
00223 PyObject* oldargv = PySys_GetObject( const_cast< char* >( "argv" ) );
00224 if ( ! oldargv )
00225 PyErr_Clear();
00226 else {
00227 PyObject* l = PyList_New( PyList_GET_SIZE( oldargv ) );
00228 for ( int i = 0; i < PyList_GET_SIZE( oldargv ); ++i ) {
00229 PyObject* item = PyList_GET_ITEM( oldargv, i );
00230 Py_INCREF( item );
00231 PyList_SET_ITEM( l, i, item );
00232 }
00233 oldargv = l;
00234 }
00235
00236
00237 argc += 1;
00238 #if PY_VERSION_HEX < 0x03000000
00239 const char** argv2 = new const char*[ argc ];
00240 for ( int i = 1; i < argc; ++i ) argv2[ i ] = argv[ i-1 ];
00241 argv2[ 0 ] = Py_GetProgramName();
00242 PySys_SetArgv( argc, const_cast< char** >( argv2 ) );
00243 delete [] argv2;
00244 #else
00245
00246 argv = 0;
00247 #endif
00248
00249
00250 PyObject* gbl = PyDict_Copy( gMainDict );
00251 PyObject* result =
00252 PyRun_FileEx( fp, const_cast< char* >( name ), Py_file_input, gbl, gbl, 1 );
00253 if ( ! result )
00254 PyErr_Print();
00255 Py_XDECREF( result );
00256 Py_DECREF( gbl );
00257
00258
00259 if ( oldargv ) {
00260 PySys_SetObject( const_cast< char* >( "argv" ), oldargv );
00261 Py_DECREF( oldargv );
00262 }
00263 }
00264
00265
00266 Bool_t TPython::Exec( const char* cmd )
00267 {
00268
00269
00270
00271 if ( ! Initialize() )
00272 return kFALSE;
00273
00274
00275 PyObject* result =
00276 PyRun_String( const_cast< char* >( cmd ), Py_file_input, gMainDict, gMainDict );
00277
00278
00279 if ( result ) {
00280 Py_DECREF( result );
00281 return kTRUE;
00282 } else {
00283 PyErr_Print();
00284 return kFALSE;
00285 }
00286 }
00287
00288
00289
00290 const TPyReturn TPython::Eval( const char* expr )
00291 {
00292
00293
00294
00295
00296
00297
00298
00299 if ( ! Initialize() )
00300 return TPyReturn();
00301
00302
00303 PyObject* result =
00304 PyRun_String( const_cast< char* >( expr ), Py_eval_input, gMainDict, gMainDict );
00305
00306
00307 if ( ! result ) {
00308 PyErr_Print();
00309 return TPyReturn();
00310 }
00311
00312
00313 if ( result == Py_None || PyROOT::ObjectProxy_Check( result ) ||
00314 PyBytes_Check( result ) ||
00315 PyFloat_Check( result ) || PyLong_Check( result ) || PyInt_Check( result ) )
00316 return TPyReturn( result );
00317
00318
00319 PyObject* pyclass = PyObject_GetAttr( result, PyROOT::PyStrings::gClass );
00320 if ( pyclass != 0 ) {
00321
00322 PyObject* name = PyObject_GetAttr( pyclass, PyROOT::PyStrings::gName );
00323 PyObject* module = PyObject_GetAttr( pyclass, PyROOT::PyStrings::gModule );
00324
00325
00326 std::string qname =
00327 std::string( PyROOT_PyUnicode_AsString( module ) ) + '.' + PyROOT_PyUnicode_AsString( name );
00328 Py_DECREF( module );
00329 Py_DECREF( name );
00330 Py_DECREF( pyclass );
00331
00332
00333 TClass* klass = TClass::GetClass( qname.c_str() );
00334
00335
00336 if ( klass != 0 )
00337 return TPyReturn( result );
00338 } else
00339 PyErr_Clear();
00340
00341
00342 Py_DECREF( result );
00343 return TPyReturn();
00344 }
00345
00346
00347 Bool_t TPython::Bind( TObject* object, const char* label )
00348 {
00349
00350
00351
00352 if ( ! ( object && Initialize() ) )
00353 return kFALSE;
00354
00355
00356 TClass* klass = object->IsA();
00357 if ( klass != 0 ) {
00358 PyObject* bound = PyROOT::BindRootObject( (void*)object, klass );
00359
00360 if ( bound ) {
00361 Bool_t bOk = PyDict_SetItemString( gMainDict, const_cast< char* >( label ), bound ) == 0;
00362 Py_DECREF( bound );
00363
00364 return bOk;
00365 }
00366 }
00367
00368 return kFALSE;
00369 }
00370
00371
00372 void TPython::Prompt() {
00373
00374
00375
00376
00377 if ( ! Initialize() ) {
00378 return;
00379 }
00380
00381
00382 PyRun_InteractiveLoop( stdin, const_cast< char* >( "\0" ) );
00383 }
00384
00385
00386 Bool_t TPython::ObjectProxy_Check( PyObject* pyobject )
00387 {
00388
00389
00390
00391
00392 if ( ! Initialize() )
00393 return kFALSE;
00394
00395
00396 return PyROOT::ObjectProxy_Check( pyobject );
00397 }
00398
00399
00400 Bool_t TPython::ObjectProxy_CheckExact( PyObject* pyobject )
00401 {
00402
00403
00404
00405 if ( ! Initialize() )
00406 return kFALSE;
00407
00408
00409 return PyROOT::ObjectProxy_CheckExact( pyobject );
00410 }
00411
00412
00413 void* TPython::ObjectProxy_AsVoidPtr( PyObject* pyobject )
00414 {
00415
00416
00417
00418 if ( ! Initialize() )
00419 return 0;
00420
00421
00422 if ( ! PyROOT::ObjectProxy_Check( pyobject ) )
00423 return 0;
00424
00425
00426 return ((PyROOT::ObjectProxy*)pyobject)->GetObject();
00427 }
00428
00429
00430 PyObject* TPython::ObjectProxy_FromVoidPtr( void* addr, const char* classname )
00431 {
00432
00433
00434
00435 if ( ! Initialize() )
00436 return 0;
00437
00438
00439 return PyROOT::BindRootObjectNoCast( addr, TClass::GetClass( classname ), kFALSE );
00440 }