Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
Utility.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 "Utility.h"
8 #include "ObjectProxy.h"
9 #include "MethodProxy.h"
10 #include "TFunctionHolder.h"
11 #include "TCustomPyTypes.h"
12 #include "TemplateProxy.h"
13 #include "RootWrapper.h"
14 #include "PyCallable.h"
15 
16 // ROOT
17 #include "TApplication.h"
18 #include "TROOT.h"
19 #include "TSystem.h"
20 #include "TObject.h"
21 #include "TClassEdit.h"
22 #include "TClassRef.h"
23 #include "TCollection.h"
24 #include "TDataType.h"
25 #include "TFunction.h"
26 #include "TFunctionTemplate.h"
27 #include "TMethod.h"
28 #include "TMethodArg.h"
29 #include "TError.h"
30 #include "TInterpreter.h"
31 
32 // Standard
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <algorithm>
37 #include <list>
38 #include <mutex>
39 #include <sstream>
40 #include <utility>
41 
42 
43 //- data _____________________________________________________________________
44 dict_lookup_func PyROOT::gDictLookupOrg = 0;
45 Bool_t PyROOT::gDictLookupActive = kFALSE;
46 
47 typedef std::map< std::string, std::string > TC2POperatorMapping_t;
48 static TC2POperatorMapping_t gC2POperatorMapping;
49 
50 namespace {
51 
52  using namespace PyROOT::Utility;
53 
54  struct InitOperatorMapping_t {
55  public:
56  InitOperatorMapping_t() {
57  // Initialize the global map of operator names C++ -> python.
58 
59  // gC2POperatorMapping[ "[]" ] = "__setitem__"; // depends on return type
60  // gC2POperatorMapping[ "+" ] = "__add__"; // depends on # of args (see __pos__)
61  // gC2POperatorMapping[ "-" ] = "__sub__"; // id. (eq. __neg__)
62  // gC2POperatorMapping[ "*" ] = "__mul__"; // double meaning in C++
63 
64  gC2POperatorMapping[ "[]" ] = "__getitem__";
65  gC2POperatorMapping[ "()" ] = "__call__";
66  gC2POperatorMapping[ "/" ] = PYROOT__div__;
67  gC2POperatorMapping[ "%" ] = "__mod__";
68  gC2POperatorMapping[ "**" ] = "__pow__";
69  gC2POperatorMapping[ "<<" ] = "__lshift__";
70  gC2POperatorMapping[ ">>" ] = "__rshift__";
71  gC2POperatorMapping[ "&" ] = "__and__";
72  gC2POperatorMapping[ "|" ] = "__or__";
73  gC2POperatorMapping[ "^" ] = "__xor__";
74  gC2POperatorMapping[ "~" ] = "__inv__";
75  gC2POperatorMapping[ "+=" ] = "__iadd__";
76  gC2POperatorMapping[ "-=" ] = "__isub__";
77  gC2POperatorMapping[ "*=" ] = "__imul__";
78  gC2POperatorMapping[ "/=" ] = PYROOT__idiv__;
79  gC2POperatorMapping[ "%=" ] = "__imod__";
80  gC2POperatorMapping[ "**=" ] = "__ipow__";
81  gC2POperatorMapping[ "<<=" ] = "__ilshift__";
82  gC2POperatorMapping[ ">>=" ] = "__irshift__";
83  gC2POperatorMapping[ "&=" ] = "__iand__";
84  gC2POperatorMapping[ "|=" ] = "__ior__";
85  gC2POperatorMapping[ "^=" ] = "__ixor__";
86  gC2POperatorMapping[ "==" ] = "__eq__";
87  gC2POperatorMapping[ "!=" ] = "__ne__";
88  gC2POperatorMapping[ ">" ] = "__gt__";
89  gC2POperatorMapping[ "<" ] = "__lt__";
90  gC2POperatorMapping[ ">=" ] = "__ge__";
91  gC2POperatorMapping[ "<=" ] = "__le__";
92 
93  // the following type mappings are "exact"
94  gC2POperatorMapping[ "const char*" ] = "__str__";
95  gC2POperatorMapping[ "char*" ] = "__str__";
96  gC2POperatorMapping[ "const char *" ] = gC2POperatorMapping[ "const char*" ];
97  gC2POperatorMapping[ "char *" ] = gC2POperatorMapping[ "char*" ];
98  gC2POperatorMapping[ "int" ] = "__int__";
99  gC2POperatorMapping[ "long" ] = PYROOT__long__;
100  gC2POperatorMapping[ "double" ] = "__float__";
101 
102  // the following type mappings are "okay"; the assumption is that they
103  // are not mixed up with the ones above or between themselves (and if
104  // they are, that it is done consistently)
105  gC2POperatorMapping[ "short" ] = "__int__";
106  gC2POperatorMapping[ "unsigned short" ] = "__int__";
107  gC2POperatorMapping[ "unsigned int" ] = PYROOT__long__;
108  gC2POperatorMapping[ "unsigned long" ] = PYROOT__long__;
109  gC2POperatorMapping[ "long long" ] = PYROOT__long__;
110  gC2POperatorMapping[ "unsigned long long" ] = PYROOT__long__;
111  gC2POperatorMapping[ "float" ] = "__float__";
112 
113  gC2POperatorMapping[ "->" ] = "__follow__"; // not an actual python operator
114  gC2POperatorMapping[ "=" ] = "__assign__"; // id.
115 
116 #if PY_VERSION_HEX < 0x03000000
117  gC2POperatorMapping[ "bool" ] = "__nonzero__";
118 #else
119  gC2POperatorMapping[ "bool" ] = "__bool__";
120 #endif
121  }
122  } initOperatorMapping_;
123 
124  std::once_flag sOperatorTemplateFlag;
125  void InitOperatorTemplate() {
126  gROOT->ProcessLine(
127  "namespace _pyroot_internal { template<class C1, class C2>"
128  " bool is_equal(const C1& c1, const C2& c2){ return (bool)(c1 == c2); } }" );
129  gROOT->ProcessLine(
130  "namespace _pyroot_internal { template<class C1, class C2>"
131  " bool is_not_equal(const C1& c1, const C2& c2){ return (bool)(c1 != c2); } }" );
132  }
133 
134  inline void RemoveConst( std::string& cleanName ) {
135  std::string::size_type spos = std::string::npos;
136  while ( ( spos = cleanName.find( "const" ) ) != std::string::npos ) {
137  cleanName.swap( cleanName.erase( spos, 5 ) );
138  }
139  }
140 
141 } // unnamed namespace
142 
143 
144 //- public functions ---------------------------------------------------------
145 ULong_t PyROOT::PyLongOrInt_AsULong( PyObject* pyobject )
146 {
147 // Convert <pybject> to C++ unsigned long, with bounds checking, allow int -> ulong.
148  ULong_t ul = PyLong_AsUnsignedLong( pyobject );
149  if ( PyErr_Occurred() && PyInt_Check( pyobject ) ) {
150  PyErr_Clear();
151  Long_t i = PyInt_AS_LONG( pyobject );
152  if ( 0 <= i ) {
153  ul = (ULong_t)i;
154  } else {
155  PyErr_SetString( PyExc_ValueError,
156  "can\'t convert negative value to unsigned long" );
157  }
158  }
159 
160  return ul;
161 }
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// Convert <pyobject> to C++ unsigned long long, with bounds checking.
165 
166 ULong64_t PyROOT::PyLongOrInt_AsULong64( PyObject* pyobject )
167 {
168  ULong64_t ull = PyLong_AsUnsignedLongLong( pyobject );
169  if ( PyErr_Occurred() && PyInt_Check( pyobject ) ) {
170  PyErr_Clear();
171  Long_t i = PyInt_AS_LONG( pyobject );
172  if ( 0 <= i ) {
173  ull = (ULong64_t)i;
174  } else {
175  PyErr_SetString( PyExc_ValueError,
176  "can\'t convert negative value to unsigned long long" );
177  }
178  }
179 
180  return ull;
181 }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// Add the given function to the class under name 'label'.
185 
186 Bool_t PyROOT::Utility::AddToClass(
187  PyObject* pyclass, const char* label, PyCFunction cfunc, int flags )
188 {
189 // use list for clean-up (.so's are unloaded only at interpreter shutdown)
190  static std::list< PyMethodDef > s_pymeths;
191 
192  s_pymeths.push_back( PyMethodDef() );
193  PyMethodDef* pdef = &s_pymeths.back();
194  pdef->ml_name = const_cast< char* >( label );
195  pdef->ml_meth = cfunc;
196  pdef->ml_flags = flags;
197  pdef->ml_doc = NULL;
198 
199  PyObject* func = PyCFunction_New( pdef, NULL );
200  PyObject* method = TCustomInstanceMethod_New( func, NULL, pyclass );
201  Bool_t isOk = PyObject_SetAttrString( pyclass, pdef->ml_name, method ) == 0;
202  Py_DECREF( method );
203  Py_DECREF( func );
204 
205  if ( PyErr_Occurred() )
206  return kFALSE;
207 
208  if ( ! isOk ) {
209  PyErr_Format( PyExc_TypeError, "could not add method %s", label );
210  return kFALSE;
211  }
212 
213  return kTRUE;
214 }
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 /// Add the given function to the class under name 'label'.
218 
219 Bool_t PyROOT::Utility::AddToClass( PyObject* pyclass, const char* label, const char* func )
220 {
221  PyObject* pyfunc = PyObject_GetAttrString( pyclass, const_cast< char* >( func ) );
222  if ( ! pyfunc )
223  return kFALSE;
224 
225  Bool_t isOk = PyObject_SetAttrString( pyclass, const_cast< char* >( label ), pyfunc ) == 0;
226 
227  Py_DECREF( pyfunc );
228  return isOk;
229 }
230 
231 ////////////////////////////////////////////////////////////////////////////////
232 /// Add the given function to the class under name 'label'.
233 
234 Bool_t PyROOT::Utility::AddToClass( PyObject* pyclass, const char* label, PyCallable* pyfunc )
235 {
236  MethodProxy* method =
237  (MethodProxy*)PyObject_GetAttrString( pyclass, const_cast< char* >( label ) );
238 
239  if ( ! method || ! MethodProxy_Check( method ) ) {
240  // not adding to existing MethodProxy; add callable directly to the class
241  if ( PyErr_Occurred() )
242  PyErr_Clear();
243  Py_XDECREF( (PyObject*)method );
244  method = MethodProxy_New( label, pyfunc );
245  Bool_t isOk = PyObject_SetAttrString(
246  pyclass, const_cast< char* >( label ), (PyObject*)method ) == 0;
247  Py_DECREF( method );
248  return isOk;
249  }
250 
251  method->AddMethod( pyfunc );
252 
253  Py_DECREF( method );
254  return kTRUE;
255 }
256 
257 ////////////////////////////////////////////////////////////////////////////////
258 /// Helper to add base class methods to the derived class one (this covers the
259 /// 'using' cases, which the dictionary does not provide).
260 
261 Bool_t PyROOT::Utility::AddUsingToClass( PyObject* pyclass, const char* method )
262 {
263  MethodProxy* derivedMethod =
264  (MethodProxy*)PyObject_GetAttrString( pyclass, const_cast< char* >( method ) );
265  if ( ! MethodProxy_Check( derivedMethod ) ) {
266  Py_XDECREF( derivedMethod );
267  return kFALSE;
268  }
269 
270  PyObject* mro = PyObject_GetAttr( pyclass, PyStrings::gMRO );
271  if ( ! mro || ! PyTuple_Check( mro ) ) {
272  Py_XDECREF( mro );
273  Py_DECREF( derivedMethod );
274  return kFALSE;
275  }
276 
277  MethodProxy* baseMethod = 0;
278  for ( int i = 1; i < PyTuple_GET_SIZE( mro ); ++i ) {
279  baseMethod = (MethodProxy*)PyObject_GetAttrString(
280  PyTuple_GET_ITEM( mro, i ), const_cast< char* >( method ) );
281 
282  if ( ! baseMethod ) {
283  PyErr_Clear();
284  continue;
285  }
286 
287  if ( MethodProxy_Check( baseMethod ) )
288  break;
289 
290  Py_DECREF( baseMethod );
291  baseMethod = 0;
292  }
293 
294  Py_DECREF( mro );
295 
296  if ( ! MethodProxy_Check( baseMethod ) ) {
297  Py_XDECREF( baseMethod );
298  Py_DECREF( derivedMethod );
299  return kFALSE;
300  }
301 
302  derivedMethod->AddMethod( baseMethod );
303 
304  Py_DECREF( baseMethod );
305  Py_DECREF( derivedMethod );
306 
307  return kTRUE;
308 }
309 
310 ////////////////////////////////////////////////////////////////////////////////
311 /// Install the named operator (op) into the left object's class if such a function
312 /// exists as a global overload; a label must be given if the operator is not in
313 /// gC2POperatorMapping (i.e. if it is ambiguous at the member level).
314 
315 Bool_t PyROOT::Utility::AddBinaryOperator(
316  PyObject* left, PyObject* right, const char* op, const char* label, const char* alt, bool lazy )
317 {
318 // this should be a given, nevertheless ...
319  if ( ! ObjectProxy_Check( left ) )
320  return kFALSE;
321 
322 // retrieve the class names to match the signature of any found global functions
323  std::string rcname = ClassName( right );
324  std::string lcname = ClassName( left );
325  PyObject* pyclass = PyObject_GetAttr( left, PyStrings::gClass );
326 
327  Bool_t result = AddBinaryOperator( pyclass, lcname, rcname, op, label, alt, lazy );
328 
329  Py_DECREF( pyclass );
330  return result;
331 }
332 
333 ////////////////////////////////////////////////////////////////////////////////
334 /// Install binary operator op in pyclass, working on two instances of pyclass.
335 
336 Bool_t PyROOT::Utility::AddBinaryOperator(
337  PyObject* pyclass, const char* op, const char* label, const char* alt, bool lazy )
338 {
339  PyObject* pyname = PyObject_GetAttr( pyclass, PyStrings::gCppName );
340  if ( ! pyname ) pyname = PyObject_GetAttr( pyclass, PyStrings::gName );
341  std::string cname = Cppyy::ResolveName( PyROOT_PyUnicode_AsString( pyname ) );
342  Py_DECREF( pyname ); pyname = 0;
343 
344  return AddBinaryOperator( pyclass, cname, cname, op, label, alt, lazy );
345 }
346 
347 ////////////////////////////////////////////////////////////////////////////////
348 /// Helper to find a function with matching signature in 'funcs'.
349 
350 static inline Cppyy::TCppMethod_t FindAndAddOperator( const std::string& lcname, const std::string& rcname,
351  const char* op, TClass* klass = 0 ) {
352  std::string opname = "operator";
353  opname += op;
354  std::string proto = lcname + ", " + rcname;
355 
356 // case of global namespace
357  if ( ! klass )
358  return (Cppyy::TCppMethod_t)gROOT->GetGlobalFunctionWithPrototype( opname.c_str(), proto.c_str() );
359 
360 // case of specific namespace
361  return (Cppyy::TCppMethod_t)klass->GetMethodWithPrototype( opname.c_str(), proto.c_str() );
362 }
363 
364 Bool_t PyROOT::Utility::AddBinaryOperator( PyObject* pyclass, const std::string& lcname,
365  const std::string& rcname, const char* op, const char* label, const char* alt, bool lazy )
366 {
367 // Find a global function with a matching signature and install the result on pyclass;
368 // in addition, __gnu_cxx, std::__1, and _pyroot_internal are searched pro-actively (as
369 // there's AFAICS no way to unearth using information).
370 
371 // This function can be called too early when setting up some of the ROOT core classes,
372 // which in turn can trigger the creation of a (default) TApplication. Wait with looking
373 // for binary operators '!=' and '==' (which are set early in Pythonize.cxx) until fully
374 // initialized. Other operators are expected to have entered from user code.
375  if ( !lazy && !gApplication && (strcmp( op, "==" ) == 0 || strcmp( op, "!=" ) == 0) )
376  return kFALSE;
377 
378 // For GNU on clang, search the internal __gnu_cxx namespace for binary operators (is
379 // typically the case for STL iterators operator==/!=.
380  static TClassRef gnucxx( "__gnu_cxx" );
381  static bool gnucxx_exists = (bool)gnucxx.GetClass();
382 
383 // Same for clang on Mac. TODO: find proper pre-processor magic to only use those specific
384 // namespaces that are actually around; although to be sure, this isn't expensive.
385  static TClassRef std__1( "std::__1" );
386  static bool std__1_exists = (bool)std__1.GetClass();
387 
388 // One more, mostly for Mac, but again not sure whether this is not a general issue. Some
389 // operators are declared as friends only in classes, so then they're not found in the
390 // global namespace. That's why there's this little helper.
391  std::call_once( sOperatorTemplateFlag, InitOperatorTemplate );
392  static TClassRef _pr_int( "_pyroot_internal" );
393 
394  PyCallable* pyfunc = 0;
395  if ( gnucxx_exists ) {
396  Cppyy::TCppMethod_t func = FindAndAddOperator( lcname, rcname, op, gnucxx.GetClass() );
397  if ( func ) pyfunc = new TFunctionHolder( Cppyy::GetScope( "__gnu_cxx" ), func );
398  }
399 
400  if ( ! pyfunc && std__1_exists ) {
401  Cppyy::TCppMethod_t func = FindAndAddOperator( lcname, rcname, op, std__1.GetClass() );
402  if ( func ) pyfunc = new TFunctionHolder( Cppyy::GetScope( "std::__1" ), func );
403  }
404 
405  if ( ! pyfunc ) {
406  std::string::size_type pos = lcname.substr(0, lcname.find('<')).rfind( "::" );
407  if ( pos != std::string::npos ) {
408  TClass* lcscope = TClass::GetClass( lcname.substr( 0, pos ).c_str() );
409  if ( lcscope ) {
410  Cppyy::TCppMethod_t func = FindAndAddOperator( lcname, rcname, op, lcscope );
411  if ( func ) pyfunc = new TFunctionHolder( Cppyy::GetScope( lcname.substr( 0, pos ) ), func );
412  }
413  }
414  }
415 
416  if ( ! pyfunc ) {
417  Cppyy::TCppMethod_t func = FindAndAddOperator( lcname, rcname, op );
418  if ( func ) pyfunc = new TFunctionHolder( Cppyy::gGlobalScope, func );
419  }
420 
421  if ( ! pyfunc && _pr_int.GetClass() &&
422  lcname.find( "iterator" ) != std::string::npos &&
423  rcname.find( "iterator" ) != std::string::npos ) {
424  // TODO: gets called too often; make sure it's purely lazy calls only; also try to
425  // find a better notion for which classes (other than iterators) this is supposed to
426  // work; right now it fails for cases where None is passed
427  std::stringstream fname;
428  if ( strncmp( op, "==", 2 ) == 0 ) { fname << "is_equal<"; }
429  else if ( strncmp( op, "!=", 2 ) == 0 ) { fname << "is_not_equal<"; }
430  else { fname << "not_implemented<"; }
431  fname << lcname << ", " << rcname << ">";
432  Cppyy::TCppMethod_t func = (Cppyy::TCppMethod_t)_pr_int->GetMethodAny( fname.str().c_str() );
433  if ( func ) pyfunc = new TFunctionHolder( Cppyy::GetScope( "_pyroot_internal" ), func );
434  }
435 
436 // last chance: there could be a non-instantiated templated method
437  TClass* lc = TClass::GetClass( lcname.c_str() );
438  if ( lc && strcmp(op, "==") != 0 && strcmp(op, "!=") != 0 ) {
439  std::string opname = "operator"; opname += op;
440  gInterpreter->LoadFunctionTemplates(lc);
441  gInterpreter->GetFunctionTemplate(lc->GetClassInfo(), opname.c_str());
442  TFunctionTemplate*f = lc->GetFunctionTemplate(opname.c_str());
443  Cppyy::TCppMethod_t func =
444  (Cppyy::TCppMethod_t)lc->GetMethodWithPrototype( opname.c_str(), rcname.c_str() );
445  if ( func && f ) pyfunc = new TMethodHolder( Cppyy::GetScope( lcname ), func );
446  }
447 
448  if ( pyfunc ) { // found a matching overload; add to class
449  Bool_t ok = AddToClass( pyclass, label, pyfunc );
450  if ( ok && alt )
451  return AddToClass( pyclass, alt, label );
452  return ok;
453  }
454 
455  return kFALSE;
456 }
457 
458 ////////////////////////////////////////////////////////////////////////////////
459 /// Helper to construct the "< type, type, ... >" part of a templated name (either
460 /// for a class as in MakeRootTemplateClass in RootModule.cxx) or for method lookup
461 /// (as in TemplatedMemberHook, below).
462 
463 PyObject* PyROOT::Utility::BuildTemplateName( PyObject* pyname, PyObject* tpArgs, int argoff,
464  PyObject* args, ArgPreference pref, int* pcnt, bool inferredTypes )
465 {
466  if ( pyname )
467  pyname = PyROOT_PyUnicode_FromString( PyROOT_PyUnicode_AsString( pyname ) );
468  else
469  pyname = PyROOT_PyUnicode_FromString( "" );
470  PyROOT_PyUnicode_AppendAndDel( &pyname, PyROOT_PyUnicode_FromString( "<" ) );
471 
472  Py_ssize_t nArgs = PyTuple_GET_SIZE( tpArgs );
473  for ( int i = argoff; i < nArgs; ++i ) {
474  // add type as string to name
475  PyObject* tn = PyTuple_GET_ITEM( tpArgs, i );
476  if ( PyROOT_PyUnicode_Check( tn ) ) {
477  PyROOT_PyUnicode_Append( &pyname, tn );
478  } else if (PyObject_HasAttr( tn, PyStrings::gName ) ) {
479  // __cppname__ provides a better name for C++ classes (namespaces)
480  PyObject* tpName;
481  if ( PyObject_HasAttr( tn, PyStrings::gCppName ) ) {
482  tpName = PyObject_GetAttr( tn, PyStrings::gCppName );
483  } else {
484  tpName = PyObject_GetAttr( tn, PyStrings::gName );
485  }
486 
487  // Check if parameter is reference, pointer or value
488  if (args) {
489  auto arg = PyTuple_GET_ITEM(args, i);
490  ObjectProxy* pyobj = (ObjectProxy*)arg;
491  if (ObjectProxy_Check(pyobj)) {
492  if (pcnt) *pcnt += 1;
493  if ((pyobj->fFlags & ObjectProxy::kIsReference) || pref == kPointer) {
494  PyROOT_PyUnicode_AppendAndDel(&tpName, PyROOT_PyUnicode_FromString("*"));
495  } else if (pref != kValue) {
496  PyROOT_PyUnicode_AppendAndDel(&tpName, PyROOT_PyUnicode_FromString("&"));
497  }
498  }
499  }
500 
501  // special case for strings
502  auto tpNameStr = PyROOT_PyUnicode_AsString(tpName);
503  if ( strcmp( tpNameStr, "str" ) == 0 ) {
504  Py_DECREF( tpName );
505  tpName = PyROOT_PyUnicode_FromString( "std::string" );
506  }
507  // and Python float (should be double in C++) if types have been inferred
508  else if (inferredTypes && strcmp(tpNameStr, "float") == 0) {
509  Py_DECREF(tpName);
510  tpName = PyROOT_PyUnicode_FromString("double");
511  }
512  PyROOT_PyUnicode_AppendAndDel( &pyname, tpName );
513  } else if ( PyInt_Check( tn ) || PyLong_Check( tn ) || PyFloat_Check( tn ) ) {
514  // last ditch attempt, works for things like int values; since this is a
515  // source of errors otherwise, it is limited to specific types and not
516  // generally used (str(obj) can print anything ...)
517  PyObject* pystr = PyObject_Str( tn );
518  PyROOT_PyUnicode_AppendAndDel( &pyname, pystr );
519  } else {
520  Py_DECREF( pyname );
521  PyErr_SetString( PyExc_SyntaxError, "could not get __cppname__ from provided template argument. Is it a str, class, type or int?" );
522  return 0;
523  }
524 
525  // add a comma, as needed
526  if ( i != nArgs - 1 )
527  PyROOT_PyUnicode_AppendAndDel( &pyname, PyROOT_PyUnicode_FromString( ", " ) );
528  }
529 
530 // close template name; prevent '>>', which should be '> >'
531  if ( PyROOT_PyUnicode_AsString( pyname )[ PyROOT_PyUnicode_GetSize( pyname ) - 1 ] == '>' )
532  PyROOT_PyUnicode_AppendAndDel( &pyname, PyROOT_PyUnicode_FromString( " >" ) );
533  else
534  PyROOT_PyUnicode_AppendAndDel( &pyname, PyROOT_PyUnicode_FromString( ">" ) );
535 
536  return pyname;
537 }
538 
539 ////////////////////////////////////////////////////////////////////////////////
540 /// Initialize a proxy class for use by python, and add it to the ROOT module.
541 
542 Bool_t PyROOT::Utility::InitProxy( PyObject* module, PyTypeObject* pytype, const char* name )
543 {
544 // finalize proxy type
545  if ( PyType_Ready( pytype ) < 0 )
546  return kFALSE;
547 
548 // add proxy type to the given (ROOT) module
549  Py_INCREF( pytype ); // PyModule_AddObject steals reference
550  if ( PyModule_AddObject( module, (char*)name, (PyObject*)pytype ) < 0 ) {
551  Py_DECREF( pytype );
552  return kFALSE;
553  }
554 
555 // declare success
556  return kTRUE;
557 }
558 
559 ////////////////////////////////////////////////////////////////////////////////
560 /// Retrieve a linear buffer pointer from the given pyobject.
561 
562 int PyROOT::Utility::GetBuffer( PyObject* pyobject, char tc, int size, void*& buf, Bool_t check )
563 {
564 // special case: don't handle character strings here (yes, they're buffers, but not quite)
565  if ( PyBytes_Check( pyobject ) )
566  return 0;
567 
568 // attempt to retrieve pointer to buffer interface
569  PyBufferProcs* bufprocs = Py_TYPE(pyobject)->tp_as_buffer;
570 
571  PySequenceMethods* seqmeths = Py_TYPE(pyobject)->tp_as_sequence;
572  if ( seqmeths != 0 && bufprocs != 0
573 #if PY_VERSION_HEX < 0x03000000
574  && bufprocs->bf_getwritebuffer != 0
575  && (*(bufprocs->bf_getsegcount))( pyobject, 0 ) == 1
576 #else
577  && bufprocs->bf_getbuffer != 0
578 #endif
579  ) {
580 
581  // get the buffer
582 #if PY_VERSION_HEX < 0x03000000
583  Py_ssize_t buflen = (*(bufprocs->bf_getwritebuffer))( pyobject, 0, &buf );
584 #else
585  Py_buffer bufinfo;
586  (*(bufprocs->bf_getbuffer))( pyobject, &bufinfo, PyBUF_WRITABLE );
587  buf = (char*)bufinfo.buf;
588  Py_ssize_t buflen = bufinfo.len;
589 #if PY_VERSION_HEX < 0x03010000
590  PyBuffer_Release( pyobject, &bufinfo );
591 #else
592  PyBuffer_Release( &bufinfo );
593 #endif
594 #endif
595 
596  if ( buf && check == kTRUE ) {
597  // determine buffer compatibility (use "buf" as a status flag)
598  PyObject* pytc = PyObject_GetAttr( pyobject, PyStrings::gTypeCode );
599  if ( pytc != 0 ) { // for array objects
600  if ( PyROOT_PyUnicode_AsString( pytc )[0] != tc )
601  buf = 0; // no match
602  Py_DECREF( pytc );
603  } else if ( seqmeths->sq_length &&
604  (int)(buflen / (*(seqmeths->sq_length))( pyobject )) == size ) {
605  // this is a gamble ... may or may not be ok, but that's for the user
606  PyErr_Clear();
607  } else if ( buflen == size ) {
608  // also a gamble, but at least 1 item will fit into the buffer, so very likely ok ...
609  PyErr_Clear();
610  } else {
611  buf = 0; // not compatible
612 
613  // clarify error message
614  PyObject* pytype = 0, *pyvalue = 0, *pytrace = 0;
615  PyErr_Fetch( &pytype, &pyvalue, &pytrace );
616  PyObject* pyvalue2 = PyROOT_PyUnicode_FromFormat(
617  (char*)"%s and given element size (%ld) do not match needed (%d)",
618  PyROOT_PyUnicode_AsString( pyvalue ),
619  seqmeths->sq_length ? (Long_t)(buflen / (*(seqmeths->sq_length))( pyobject )) : (Long_t)buflen,
620  size );
621  Py_DECREF( pyvalue );
622  PyErr_Restore( pytype, pyvalue2, pytrace );
623  }
624  }
625 
626  return buflen;
627  }
628 
629  return 0;
630 }
631 
632 ////////////////////////////////////////////////////////////////////////////////
633 /// Map the given C++ operator name on the python equivalent.
634 
635 std::string PyROOT::Utility::MapOperatorName( const std::string& name, Bool_t bTakesParams )
636 {
637  if ( 8 < name.size() && name.substr( 0, 8 ) == "operator" ) {
638  std::string op = name.substr( 8, std::string::npos );
639 
640  // stripping ...
641  std::string::size_type start = 0, end = op.size();
642  while ( start < end && isspace( op[ start ] ) ) ++start;
643  while ( start < end && isspace( op[ end-1 ] ) ) --end;
644  op = TClassEdit::ResolveTypedef( op.substr( start, end - start ).c_str(), true );
645 
646  // map C++ operator to python equivalent, or made up name if no equivalent exists
647  TC2POperatorMapping_t::iterator pop = gC2POperatorMapping.find( op );
648  if ( pop != gC2POperatorMapping.end() ) {
649  return pop->second;
650 
651  } else if ( op == "*" ) {
652  // dereference v.s. multiplication of two instances
653  return bTakesParams ? "__mul__" : "__deref__";
654 
655  } else if ( op == "+" ) {
656  // unary positive v.s. addition of two instances
657  return bTakesParams ? "__add__" : "__pos__";
658 
659  } else if ( op == "-" ) {
660  // unary negative v.s. subtraction of two instances
661  return bTakesParams ? "__sub__" : "__neg__";
662 
663  } else if ( op == "++" ) {
664  // prefix v.s. postfix increment
665  return bTakesParams ? "__postinc__" : "__preinc__";
666 
667  } else if ( op == "--" ) {
668  // prefix v.s. postfix decrement
669  return bTakesParams ? "__postdec__" : "__predec__";
670  }
671 
672  }
673 
674 // might get here, as not all operator methods are handled (new, delete, etc.)
675  return name;
676 }
677 
678 ////////////////////////////////////////////////////////////////////////////////
679 /// Break down the compound of a fully qualified type name.
680 
681 const std::string PyROOT::Utility::Compound( const std::string& name )
682 {
683  std::string cleanName = name;
684  RemoveConst( cleanName );
685 
686  std::string compound = "";
687  for ( int ipos = (int)cleanName.size()-1; 0 <= ipos; --ipos ) {
688  char c = cleanName[ipos];
689  if ( isspace( c ) ) continue;
690  if ( isalnum( c ) || c == '_' || c == '>' ) break;
691 
692  compound = c + compound;
693  }
694 
695 // for arrays (TODO: deal with the actual size)
696  if ( compound == "]" )
697  return "[]";
698 
699  return compound;
700 }
701 
702 ////////////////////////////////////////////////////////////////////////////////
703 /// Extract size from an array type, if available.
704 
705 Py_ssize_t PyROOT::Utility::ArraySize( const std::string& name )
706 {
707  std::string cleanName = name;
708  RemoveConst( cleanName );
709 
710  if ( cleanName[cleanName.size()-1] == ']' ) {
711  std::string::size_type idx = cleanName.rfind( '[' );
712  if ( idx != std::string::npos ) {
713  const std::string asize = cleanName.substr( idx+1, cleanName.size()-2 );
714  return strtoul( asize.c_str(), NULL, 0 );
715  }
716  }
717 
718  return -1;
719 }
720 
721 ////////////////////////////////////////////////////////////////////////////////
722 /// Retrieve the class name from the given python object (which may be just an
723 /// instance of the class).
724 
725 const std::string PyROOT::Utility::ClassName( PyObject* pyobj )
726 {
727  std::string clname = "<unknown>";
728  PyObject* pyclass = PyObject_GetAttr( pyobj, PyStrings::gClass );
729  if ( pyclass != 0 ) {
730  PyObject* pyname = PyObject_GetAttr( pyclass, PyStrings::gCppName );
731 
732  if ( pyname != 0 ) {
733  clname = PyROOT_PyUnicode_AsString( pyname );
734  Py_DECREF( pyname );
735  } else {
736  PyErr_Clear();
737  pyname = PyObject_GetAttr( pyclass, PyStrings::gName );
738  if ( pyname != 0 ) {
739  clname = PyROOT_PyUnicode_AsString( pyname );
740  Py_DECREF( pyname );
741  } else {
742  PyErr_Clear();
743  }
744  }
745  Py_DECREF( pyclass );
746  } else {
747  PyErr_Clear();
748  }
749 
750  return clname;
751 }
752 
753 ////////////////////////////////////////////////////////////////////////////////
754 /// Translate CINT error/warning into python equivalent.
755 
756 void PyROOT::Utility::ErrMsgCallback( char* /* msg */ )
757 {
758 // TODO (Cling): this function is probably not going to be used anymore and
759 // may need removing at some point for cleanup
760 
761 /* Commented out for Cling ---
762 
763 // ignore the "*** Interpreter error recovered ***" message
764  if ( strstr( msg, "error recovered" ) )
765  return;
766 
767 // ignore CINT-style FILE/LINE messages
768  if ( strstr( msg, "FILE:" ) )
769  return;
770 
771 // get file name and line number
772  char* errFile = (char*)G__stripfilename( G__get_ifile()->name );
773  int errLine = G__get_ifile()->line_number;
774 
775 // ignore ROOT-style FILE/LINE messages
776  char buf[256];
777  snprintf( buf, 256, "%s:%d:", errFile, errLine );
778  if ( strstr( msg, buf ) )
779  return;
780 
781 // strip newline, if any
782  int len = strlen( msg );
783  if ( msg[ len-1 ] == '\n' )
784  msg[ len-1 ] = '\0';
785 
786 // concatenate message if already in error processing mode (e.g. if multiple CINT errors)
787  if ( PyErr_Occurred() ) {
788  PyObject *etype, *value, *trace;
789  PyErr_Fetch( &etype, &value, &trace ); // clears current exception
790 
791  // need to be sure that error can be added; otherwise leave earlier error in place
792  if ( PyROOT_PyUnicode_Check( value ) ) {
793  if ( ! PyErr_GivenExceptionMatches( etype, PyExc_IndexError ) )
794  PyROOT_PyUnicode_AppendAndDel( &value, PyROOT_PyUnicode_FromString( (char*)"\n " ) );
795  PyROOT_PyUnicode_AppendAndDel( &value, PyROOT_PyUnicode_FromString( msg ) );
796  }
797 
798  PyErr_Restore( etype, value, trace );
799  return;
800  }
801 
802 // else, translate known errors and warnings, or simply accept the default
803  char* format = (char*)"(file \"%s\", line %d) %s";
804  char* p = 0;
805  if ( ( p = strstr( msg, "Syntax Error:" ) ) )
806  PyErr_Format( PyExc_SyntaxError, format, errFile, errLine, p+14 );
807  else if ( ( p = strstr( msg, "Error: Array" ) ) )
808  PyErr_Format( PyExc_IndexError, format, errFile, errLine, p+12 );
809  else if ( ( p = strstr( msg, "Error:" ) ) )
810  PyErr_Format( PyExc_RuntimeError, format, errFile, errLine, p+7 );
811  else if ( ( p = strstr( msg, "Exception:" ) ) )
812  PyErr_Format( PyExc_RuntimeError, format, errFile, errLine, p+11 );
813  else if ( ( p = strstr( msg, "Limitation:" ) ) )
814  PyErr_Format( PyExc_NotImplementedError, format, errFile, errLine, p+12 );
815  else if ( ( p = strstr( msg, "Internal Error: malloc" ) ) )
816  PyErr_Format( PyExc_MemoryError, format, errFile, errLine, p+23 );
817  else if ( ( p = strstr( msg, "Internal Error:" ) ) )
818  PyErr_Format( PyExc_SystemError, format, errFile, errLine, p+16 );
819  else if ( ( p = strstr( msg, "Warning:" ) ) )
820 // either printout or raise exception, depending on user settings
821  PyErr_WarnExplicit( NULL, p+9, errFile, errLine, (char*)"CINT", NULL );
822  else if ( ( p = strstr( msg, "Note:" ) ) )
823  fprintf( stdout, "Note: (file \"%s\", line %d) %s\n", errFile, errLine, p+6 );
824  else // unknown: printing it to screen is the safest action
825  fprintf( stdout, "Message: (file \"%s\", line %d) %s\n", errFile, errLine, msg );
826 
827  --- Commented out for Cling */
828 }
829 
830 ////////////////////////////////////////////////////////////////////////////////
831 /// Translate ROOT error/warning to python.
832 
833 void PyROOT::Utility::ErrMsgHandler( int level, Bool_t abort, const char* location, const char* msg )
834 {
835 // initialization from gEnv (the default handler will return w/o msg b/c level too low)
836  if ( gErrorIgnoreLevel == kUnset )
837  ::DefaultErrorHandler( kUnset - 1, kFALSE, "", "" );
838 
839  if ( level < gErrorIgnoreLevel )
840  return;
841 
842 // turn warnings into python warnings
843  if (level >= kError)
844  ::DefaultErrorHandler( level, abort, location, msg );
845  else if ( level >= kWarning ) {
846  static const char* emptyString = "";
847  if (!location) location = emptyString;
848  // This warning might be triggered while holding the ROOT lock, while
849  // some othe rtherad is holding the GIL and waiting for the ROOT lock.
850  // That will trigger a deadlock.
851  // So if ROOT is in MT mode, use ROOT's error handler that doesn't take
852  // the GIL.
853  if (!gGlobalMutex) {
854  // either printout or raise exception, depending on user settings
855  PyErr_WarnExplicit( NULL, (char*)msg, (char*)location, 0, (char*)"ROOT", NULL );
856  } else {
857  ::DefaultErrorHandler( level, abort, location, msg );
858  }
859  }
860  else
861  ::DefaultErrorHandler( level, abort, location, msg );
862 }
863 
864 
865 ////////////////////////////////////////////////////////////////////////////////
866 /// Compile a function on the fly and return a function pointer for use on C-APIs.
867 /// The callback should take a (void*)pyfunc and the (Long_t)user as well as the
868 /// rest of the declare signature. It should also live in namespace PyROOT
869 
870 void* PyROOT::Utility::CreateWrapperMethod( PyObject* pyfunc, Long_t user,
871  const char* retType, const std::vector<std::string>& signature, const char* callback )
872 {
873  static Long_t s_fid = 0;
874 
875  if ( ! PyCallable_Check( pyfunc ) )
876  return 0;
877 
878 // keep alive (TODO: manage this intelligently)
879  Py_INCREF( pyfunc );
880 
881  Long_t fid = s_fid++;
882 
883 // basic function name part
884  std::ostringstream funcName;
885  funcName << "pyrootGenFun" << fid;
886 
887 // build-up a signature
888  std::ostringstream sigDecl, argsig;
889  std::vector<std::string>::size_type nargs = signature.size();
890  for ( std::vector<std::string>::size_type i = 0; i < nargs; ++i ) {
891  sigDecl << signature[i] << " a" << i;
892  argsig << ", a" << i;
893  if ( i != nargs-1 ) sigDecl << ", ";
894  }
895 
896 // create full definition
897  std::ostringstream declCode;
898  declCode << "namespace PyROOT { "
899  << retType << " " << callback << "(void*, Long_t, " << sigDecl.str() << "); }\n"
900  << retType << " " << funcName.str() << "(" << sigDecl.str()
901  << ") { void* v0 = (void*)" << (void*)pyfunc << "; "
902  << "return PyROOT::" << callback << "(v0, " << user << argsig.str() << "); }";
903 
904 // body compilation
905  gInterpreter->LoadText( declCode.str().c_str() );
906 
907 // func-ptr retrieval code
908  std::ostringstream fptrCode;
909  fptrCode << "void* pyrootPtrVar" << fid << " = (void*)" << funcName.str()
910  << "; pyrootPtrVar" << fid << ";";
911 
912 // retrieve function pointer
913  void* fptr = (void*)gInterpreter->ProcessLineSynch( fptrCode.str().c_str() );
914  if ( fptr == 0 )
915  PyErr_SetString( PyExc_SyntaxError, "could not generate C++ callback wrapper" );
916 
917  return fptr;
918 }
919 
920 ////////////////////////////////////////////////////////////////////////////////
921 /// Re-acquire the GIL before calling PyErr_Occurred() in case it has been
922 /// released; note that the p2.2 code assumes that there are no callbacks in
923 /// C++ to python (or at least none returning errors).
924 
925 PyObject* PyROOT::Utility::PyErr_Occurred_WithGIL()
926 {
927 #if PY_VERSION_HEX >= 0x02030000
928  PyGILState_STATE gstate = PyGILState_Ensure();
929  PyObject* e = PyErr_Occurred();
930  PyGILState_Release( gstate );
931 #else
932  if ( PyThreadState_GET() )
933  return PyErr_Occurred();
934  PyObject* e = 0;
935 #endif
936 
937  return e;
938 }
939 
940 ////////////////////////////////////////////////////////////////////////////////
941 
942 namespace {
943  static int (*sOldInputHook)() = NULL;
944  static PyThreadState* sInputHookEventThreadState = NULL;
945 
946  static int EventInputHook()
947  {
948  // This method is supposed to be called from CPython's command line and
949  // drives the GUI.
950  PyEval_RestoreThread( sInputHookEventThreadState );
951  gSystem->ProcessEvents();
952  PyEval_SaveThread();
953 
954  if ( sOldInputHook ) return sOldInputHook();
955  return 0;
956  }
957 
958 } // unnamed namespace
959 
960 PyObject* PyROOT::Utility::InstallGUIEventInputHook()
961 {
962 // Install the method hook for sending events to the GUI
963  if ( PyOS_InputHook && PyOS_InputHook != &EventInputHook )
964  sOldInputHook = PyOS_InputHook;
965 
966  sInputHookEventThreadState = PyThreadState_Get();
967 
968  PyOS_InputHook = (int (*)())&EventInputHook;
969  Py_INCREF( Py_None );
970  return Py_None;
971 }
972 
973 PyObject* PyROOT::Utility::RemoveGUIEventInputHook()
974 {
975 // Remove event hook, if it was installed
976  PyOS_InputHook = sOldInputHook;
977  sInputHookEventThreadState = NULL;
978 
979  Py_INCREF( Py_None );
980  return Py_None;
981 }