ROOT  6.06/09
Reference Guide
TPython.cxx
Go to the documentation of this file.
1 // @(#)root/pyroot:$Id$
2 // Author: Wim Lavrijsen, Apr 2004
3 
4 // Bindings
5 #include "PyROOT.h"
6 #include "PyStrings.h"
7 #include "TPython.h"
8 #include "ObjectProxy.h"
9 #include "MethodProxy.h"
10 #include "RootWrapper.h"
11 #include "TPyClassGenerator.h"
12 
13 // ROOT
14 #include "TROOT.h"
15 #include "TClassRef.h"
16 #include "TObject.h"
17 
18 // Standard
19 #include <stdio.h>
20 #include <Riostream.h>
21 #include <string>
22 
23 //______________________________________________________________________________
24 // Python interpreter access
25 // =========================
26 //
27 // The TPython class allows for access to python objects from Cling. The current
28 // functionality is only basic: ROOT objects and builtin types can freely cross
29 // the boundary between the two interpreters, python objects can be instantiated
30 // and their methods can be called. All other cross-coding is based on strings
31 // that are run on the python interpreter.
32 //
33 // Examples:
34 //
35 // $ cat MyPyClass.py
36 // print 'creating class MyPyClass ... '
37 //
38 // class MyPyClass:
39 // def __init__( self ):
40 // print 'in MyPyClass.__init__'
41 //
42 // def gime( self, what ):
43 // return what
44 //
45 // $ root -l
46 // // Execute a string of python code.
47 // root [0] TPython::Exec( "print \'Hello World!\'" );
48 // Hello World!
49 //
50 // // Create a TBrowser on the python side, and transfer it back and forth.
51 // // Note the required explicit (void*) cast!
52 // root [1] TBrowser* b = (void*)TPython::Eval( "ROOT.TBrowser()" );
53 // root [2] TPython::Bind( b, "b" );
54 // root [3] b == (void*) TPython::Eval( "b" )
55 // (int)1
56 //
57 // // Builtin variables can cross-over by using implicit casts.
58 // root [4] int i = TPython::Eval( "1 + 1" );
59 // root [5] i
60 // (int)2
61 //
62 // // Load a python module with a class definition, and use it. Casts are
63 // // necessary as the type information can not be otherwise derived.
64 // root [6] TPython::LoadMacro( "MyPyClass.py" );
65 // creating class MyPyClass ...
66 // root [7] MyPyClass m;
67 // in MyPyClass.__init__
68 // root [8] std::string s = (char*)m.gime( "aap" );
69 // root [9] s
70 // (class TString)"aap"
71 //
72 // It is possible to switch between interpreters by calling "TPython::Prompt()"
73 // on the Cling side, while returning with ^D (EOF). State is preserved between
74 // successive switches.
75 //
76 // The API part provides (direct) C++ access to the bindings functionality of
77 // PyROOT. It allows verifying that you deal with a PyROOT python object in the
78 // first place (ObjectProxy_Check for ObjectProxy and any derived types, as well
79 // as ObjectProxy_CheckExact for ObjectProxy's only); and it allows conversions
80 // of void* to an ObjectProxy and vice versa.
81 
82 
83 //- data ---------------------------------------------------------------------
85 static PyObject* gMainDict = 0;
86 
87 namespace PyROOT {
89 }
90 
91 
92 //- static public members ----------------------------------------------------
94 {
95 // Private initialization method: setup the python interpreter and load the
96 // ROOT module.
97 
98  static Bool_t isInitialized = kFALSE;
99  if ( isInitialized )
100  return kTRUE;
101 
102  if ( ! Py_IsInitialized() ) {
103  // this happens if Cling comes in first
104  PyEval_InitThreads();
105  Py_Initialize();
106 
107  // try again to see if the interpreter is initialized
108  if ( ! Py_IsInitialized() ) {
109  // give up ...
110  std::cerr << "Error: python has not been intialized; returning." << std::endl;
111  return kFALSE;
112  }
113 
114  // set the command line arguments on python's sys.argv
115 #if PY_VERSION_HEX < 0x03000000
116  char* argv[] = { const_cast< char* >( "root" ) };
117 #else
118  wchar_t* argv[] = { const_cast< wchar_t* >( L"root" ) };
119 #endif
120  PySys_SetArgv( sizeof(argv)/sizeof(argv[0]), argv );
121 
122  // force loading of the ROOT module
123  PyRun_SimpleString( const_cast< char* >( "import ROOT" ) );
124  }
125 
126  if ( ! gMainDict ) {
127  // retrieve the main dictionary
128  gMainDict = PyModule_GetDict(
129  PyImport_AddModule( const_cast< char* >( "__main__" ) ) );
130  Py_INCREF( gMainDict );
131  }
132 
133 // python side class construction, managed by ROOT
134  gROOT->AddClassGenerator( new TPyClassGenerator );
135 
136 // declare success ...
137  isInitialized = kTRUE;
138  return kTRUE;
139 }
140 
141 ////////////////////////////////////////////////////////////////////////////////
142 /// Import the named python module and create Cling equivalents for its classes
143 /// and methods.
144 
145 Bool_t TPython::Import( const char* mod_name )
146 {
147 // setup
148  if ( ! Initialize() )
149  return kFALSE;
150 
151  PyObject* mod = PyImport_ImportModule( mod_name );
152  if ( ! mod ) {
153  PyErr_Print();
154  return kFALSE;
155  }
156 
157 // allow finding to prevent creation of a python proxy for the C++ proxy
158  Py_INCREF( mod );
159  PyModule_AddObject( PyROOT::gRootModule, mod_name, mod );
160 
161 // force creation of the module as a namespace
162  TClass::GetClass( mod_name, kTRUE );
163 
164  PyObject* dct = PyModule_GetDict( mod );
165 
166 // create Cling classes for all new python classes
167  PyObject* values = PyDict_Values( dct );
168  for ( int i = 0; i < PyList_GET_SIZE( values ); ++i ) {
169  PyObject* value = PyList_GET_ITEM( values, i );
170  Py_INCREF( value );
171 
172  // collect classes
173  if ( PyClass_Check( value ) || PyObject_HasAttr( value, PyROOT::PyStrings::gBases ) ) {
174  // get full class name (including module)
175  PyObject* pyClName = PyObject_GetAttr( value, PyROOT::PyStrings::gName );
176 
177  if ( PyErr_Occurred() )
178  PyErr_Clear();
179 
180  // build full, qualified name
181  std::string fullname = mod_name;
182  fullname += ".";
183  fullname += PyROOT_PyUnicode_AsString( pyClName );
184 
185  // force class creation (this will eventually call TPyClassGenerator)
186  TClass::GetClass( fullname.c_str(), kTRUE );
187 
188  Py_XDECREF( pyClName );
189  }
190 
191  Py_DECREF( value );
192  }
193 
194  Py_DECREF( values );
195 
196 // TODO: mod "leaks" here
197  if ( PyErr_Occurred() )
198  return kFALSE;
199  return kTRUE;
200 }
201 
202 ////////////////////////////////////////////////////////////////////////////////
203 /// Execute the give python script as if it were a macro (effectively an
204 /// execfile in __main__), and create Cling equivalents for any newly available
205 /// python classes.
206 
207 void TPython::LoadMacro( const char* name )
208 {
209 // setup
210  if ( ! Initialize() )
211  return;
212 
213 // obtain a reference to look for new classes later
214  PyObject* old = PyDict_Values( gMainDict );
215 
216 // actual execution
217  Exec( (std::string( "execfile(\"" ) + name + "\")").c_str() );
218 
219 // obtain new __main__ contents
220  PyObject* current = PyDict_Values( gMainDict );
221 
222 // create Cling classes for all new python classes
223  for ( int i = 0; i < PyList_GET_SIZE( current ); ++i ) {
224  PyObject* value = PyList_GET_ITEM( current, i );
225  Py_INCREF( value );
226 
227  if ( ! PySequence_Contains( old, value ) ) {
228  // collect classes
229  if ( PyClass_Check( value ) || PyObject_HasAttr( value, PyROOT::PyStrings::gBases ) ) {
230  // get full class name (including module)
231  PyObject* pyModName = PyObject_GetAttr( value, PyROOT::PyStrings::gModule );
232  PyObject* pyClName = PyObject_GetAttr( value, PyROOT::PyStrings::gName );
233 
234  if ( PyErr_Occurred() )
235  PyErr_Clear();
236 
237  // need to check for both exact and derived (differences exist between older and newer
238  // versions of python ... bug?)
239  if ( (pyModName && pyClName) &&\
240  ( (PyBytes_CheckExact( pyModName ) && PyBytes_CheckExact( pyClName )) ||\
241  (PyBytes_Check( pyModName ) && PyBytes_Check( pyClName ))\
242  ) ) {
243  // build full, qualified name
244  std::string fullname = PyROOT_PyUnicode_AsString( pyModName );
245  fullname += '.';
246  fullname += PyROOT_PyUnicode_AsString( pyClName );
247 
248  // force class creation (this will eventually call TPyClassGenerator)
249  TClass::GetClass( fullname.c_str(), kTRUE );
250  }
251 
252  Py_XDECREF( pyClName );
253  Py_XDECREF( pyModName );
254  }
255  }
256 
257  Py_DECREF( value );
258  }
259 
260  Py_DECREF( current );
261  Py_DECREF( old );
262 }
263 
264 ////////////////////////////////////////////////////////////////////////////////
265 /// Execute a python stand-alone script, with argv CLI arguments.
266 ///
267 /// example of use:
268 /// const char* argv[] = { "1", "2", "3" };
269 /// TPython::ExecScript( "test.py", sizeof(argv)/sizeof(argv[0]), argv );
270 
271 void TPython::ExecScript( const char* name, int argc, const char**
272 #if PY_VERSION_HEX < 0x03000000
273  argv
274 #endif
275  )
276 {
277 
278 // setup
279  if ( ! Initialize() )
280  return;
281 
282 // verify arguments
283  if ( ! name ) {
284  std::cerr << "Error: no file name specified." << std::endl;
285  return;
286  }
287 
288  FILE* fp = fopen( name, "r" );
289  if ( ! fp ) {
290  std::cerr << "Error: could not open file \"" << name << "\"." << std::endl;
291  return;
292  }
293 
294 // store a copy of the old cli for restoration
295  PyObject* oldargv = PySys_GetObject( const_cast< char* >( "argv" ) ); // borrowed
296  if ( ! oldargv ) // e.g. apache
297  PyErr_Clear();
298  else {
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 );
302  Py_INCREF( item );
303  PyList_SET_ITEM( l, i, item ); // steals ref
304  }
305  oldargv = l;
306  }
307 
308 // create and set (add progam name) the new command line
309  argc += 1;
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 ) );
315  delete [] argv2;
316 #else
317 // TODO: fix this to work like above ...
318 #endif
319 
320 // actual script execution
321  PyObject* gbl = PyDict_Copy( gMainDict );
322  PyObject* result = // PyRun_FileEx closes fp (b/c of last argument "1")
323  PyRun_FileEx( fp, const_cast< char* >( name ), Py_file_input, gbl, gbl, 1 );
324  if ( ! result )
325  PyErr_Print();
326  Py_XDECREF( result );
327  Py_DECREF( gbl );
328 
329 // restore original command line
330  if ( oldargv ) {
331  PySys_SetObject( const_cast< char* >( "argv" ), oldargv );
332  Py_DECREF( oldargv );
333  }
334 }
335 
336 ////////////////////////////////////////////////////////////////////////////////
337 /// Execute a python statement (e.g. "import ROOT").
338 
339 Bool_t TPython::Exec( const char* cmd )
340 {
341 // setup
342  if ( ! Initialize() )
343  return kFALSE;
344 
345 // execute the command
346  PyObject* result =
347  PyRun_String( const_cast< char* >( cmd ), Py_file_input, gMainDict, gMainDict );
348 
349 // test for error
350  if ( result ) {
351  Py_DECREF( result );
352  return kTRUE;
353  } else {
354  PyErr_Print();
355  return kFALSE;
356  }
357 }
358 
359 
360 ////////////////////////////////////////////////////////////////////////////////
361 /// Evaluate a python expression (e.g. "ROOT.TBrowser()").
362 ///
363 /// Caution: do not hold on to the return value: either store it in a builtin
364 /// type (implicit casting will work), or in a pointer to a ROOT object (explicit
365 /// casting to a void* is required).
366 
367 const TPyReturn TPython::Eval( const char* expr )
368 {
369 // setup
370  if ( ! Initialize() )
371  return TPyReturn();
372 
373 // evaluate the expression
374  PyObject* result =
375  PyRun_String( const_cast< char* >( expr ), Py_eval_input, gMainDict, gMainDict );
376 
377 // report errors as appropriate; return void
378  if ( ! result ) {
379  PyErr_Print();
380  return TPyReturn();
381  }
382 
383 // results that require no convserion
384  if ( result == Py_None || PyROOT::ObjectProxy_Check( result ) ||
385  PyBytes_Check( result ) ||
386  PyFloat_Check( result ) || PyLong_Check( result ) || PyInt_Check( result ) )
387  return TPyReturn( result );
388 
389 // explicit conversion for python type required
390  PyObject* pyclass = PyObject_GetAttr( result, PyROOT::PyStrings::gClass );
391  if ( pyclass != 0 ) {
392  // retrieve class name and the module in which it resides
393  PyObject* name = PyObject_GetAttr( pyclass, PyROOT::PyStrings::gName );
394  PyObject* module = PyObject_GetAttr( pyclass, PyROOT::PyStrings::gModule );
395 
396  // concat name
397  std::string qname =
398  std::string( PyROOT_PyUnicode_AsString( module ) ) + '.' + PyROOT_PyUnicode_AsString( name );
399  Py_DECREF( module );
400  Py_DECREF( name );
401  Py_DECREF( pyclass );
402 
403  // locate ROOT style class with this name
404  TClass* klass = TClass::GetClass( qname.c_str() );
405 
406  // construct general ROOT python object that pretends to be of class 'klass'
407  if ( klass != 0 )
408  return TPyReturn( result );
409  } else
410  PyErr_Clear();
411 
412 // no conversion, return null pointer object
413  Py_DECREF( result );
414  return TPyReturn();
415 }
416 
417 ////////////////////////////////////////////////////////////////////////////////
418 /// Bind a ROOT object with, at the python side, the name "label".
419 
420 Bool_t TPython::Bind( TObject* object, const char* label )
421 {
422 // check given address and setup
423  if ( ! ( object && Initialize() ) )
424  return kFALSE;
425 
426 // bind object in the main namespace
427  TClass* klass = object->IsA();
428  if ( klass != 0 ) {
429  PyObject* bound = PyROOT::BindCppObject( (void*)object, klass->GetName() );
430 
431  if ( bound ) {
432  Bool_t bOk = PyDict_SetItemString( gMainDict, const_cast< char* >( label ), bound ) == 0;
433  Py_DECREF( bound );
434 
435  return bOk;
436  }
437  }
438 
439  return kFALSE;
440 }
441 
442 ////////////////////////////////////////////////////////////////////////////////
443 /// Enter an interactive python session (exit with ^D). State is preserved
444 /// between successive calls.
445 
447 // setup
448  if ( ! Initialize() ) {
449  return;
450  }
451 
452 // enter i/o interactive mode
453  PyRun_InteractiveLoop( stdin, const_cast< char* >( "\0" ) );
454 }
455 
456 ////////////////////////////////////////////////////////////////////////////////
457 /// Test whether the type of the given pyobject is of ObjectProxy type or any
458 /// derived type.
459 
461 {
462 // setup
463  if ( ! Initialize() )
464  return kFALSE;
465 
466 // detailed walk through inheritance hierarchy
467  return PyROOT::ObjectProxy_Check( pyobject );
468 }
469 
470 ////////////////////////////////////////////////////////////////////////////////
471 /// Test whether the type of the given pyobject is ObjectProxy type.
472 
474 {
475 // setup
476  if ( ! Initialize() )
477  return kFALSE;
478 
479 // direct pointer comparison of type member
480  return PyROOT::ObjectProxy_CheckExact( pyobject );
481 }
482 
483 ////////////////////////////////////////////////////////////////////////////////
484 /// Test whether the type of the given pyobject is of MethodProxy type or any
485 /// derived type.
486 
488 {
489 // setup
490  if ( ! Initialize() )
491  return kFALSE;
492 
493 // detailed walk through inheritance hierarchy
494  return PyROOT::MethodProxy_Check( pyobject );
495 }
496 
497 ////////////////////////////////////////////////////////////////////////////////
498 /// Test whether the type of the given pyobject is MethodProxy type.
499 
501 {
502 // setup
503  if ( ! Initialize() )
504  return kFALSE;
505 
506 // direct pointer comparison of type member
507  return PyROOT::MethodProxy_CheckExact( pyobject );
508 }
509 
510 ////////////////////////////////////////////////////////////////////////////////
511 /// Extract the object pointer held by the ObjectProxy pyobject.
512 
514 {
515 // setup
516  if ( ! Initialize() )
517  return 0;
518 
519 // check validity of cast
520  if ( ! PyROOT::ObjectProxy_Check( pyobject ) )
521  return 0;
522 
523 // get held object (may be null)
524  return ((PyROOT::ObjectProxy*)pyobject)->GetObject();
525 }
526 
527 ////////////////////////////////////////////////////////////////////////////////
528 /// Bind the addr to a python object of class defined by classname.
529 
531  void* addr, const char* classname, Bool_t python_owns )
532 {
533 // setup
534  if ( ! Initialize() )
535  return 0;
536 
537 // perform cast (the call will check TClass and addr, and set python errors)
538  PyObject* pyobject = PyROOT::BindCppObjectNoCast( addr, Cppyy::GetScope( classname ), kFALSE );
539 
540 // give ownership, for ref-counting, to the python side, if so requested
541  if ( python_owns && PyROOT::ObjectProxy_Check( pyobject ) )
542  ((PyROOT::ObjectProxy*)pyobject)->HoldOn();
543 
544  return pyobject;
545 }
#define PyBytes_CheckExact
Definition: PyROOT.h:53
static Bool_t Exec(const char *cmd)
Execute a python statement (e.g. "import ROOT").
Definition: TPython.cxx:339
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...
Definition: TPython.cxx:487
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.
Definition: TPython.cxx:207
#define gROOT
Definition: TROOT.h:340
struct staticInitHelper gbl
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
static Bool_t Initialize()
Definition: TPython.cxx:93
static Bool_t MethodProxy_CheckExact(PyObject *pyobject)
Test whether the type of the given pyobject is MethodProxy type.
Definition: TPython.cxx:500
static void Prompt()
Enter an interactive python session (exit with ^D).
Definition: TPython.cxx:446
R__EXTERN PyObject * gRootModule
Definition: ObjectProxy.cxx:39
static const TPyReturn Eval(const char *expr)
Evaluate a python expression (e.g.
Definition: TPython.cxx:367
R__EXTERN PyObject * gModule
Definition: PyStrings.h:31
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.
Definition: TPython.cxx:530
Bool_t ObjectProxy_CheckExact(T *object)
Definition: ObjectProxy.h:97
#define PyROOT_PyUnicode_AsString
Definition: PyROOT.h:66
#define PyBytes_Check
Definition: PyROOT.h:52
static Bool_t ObjectProxy_Check(PyObject *pyobject)
Test whether the type of the given pyobject is of ObjectProxy type or any derived type...
Definition: TPython.cxx:460
R__EXTERN PyObject * gBases
Definition: PyStrings.h:16
Bool_t ObjectProxy_Check(T *object)
Definition: ObjectProxy.h:91
R__EXTERN PyObject * gClass
Definition: PyStrings.h:18
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)
TLine * l
Definition: textangle.C:4
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:81
TCppScope_t GetScope(const std::string &scope_name)
Definition: Cppyy.cxx:150
R__EXTERN PyObject * gName
Definition: PyStrings.h:33
static Bool_t Import(const char *name)
Import the named python module and create Cling equivalents for its classes and methods.
Definition: TPython.cxx:145
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.
Definition: TClass.cxx:2881
#define name(a, b)
Definition: linkTestLib0.cpp:5
static Bool_t ObjectProxy_CheckExact(PyObject *pyobject)
Test whether the type of the given pyobject is ObjectProxy type.
Definition: TPython.cxx:473
Mother of all ROOT objects.
Definition: TObject.h:58
#define R__EXTERN
Definition: DllImport.h:27
static Bool_t Bind(TObject *object, const char *label)
Bind a ROOT object with, at the python side, the name "label".
Definition: TPython.cxx:420
Bool_t MethodProxy_Check(T *object)
Definition: MethodProxy.h:63
double result[121]
const Bool_t kTRUE
Definition: Rtypes.h:91
Bool_t MethodProxy_CheckExact(T *object)
Definition: MethodProxy.h:69
ClassImp(TPython) static PyObject *gMainDict=0
float value
Definition: math.cpp:443
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.
Definition: TPython.cxx:513
static void ExecScript(const char *name, int argc=0, const char **argv=0)
Execute a python stand-alone script, with argv CLI arguments.
Definition: TPython.cxx:271
_object PyObject
Definition: TPyArg.h:22