Logo ROOT   6.07/09
Reference Guide
MethodProxy.cxx
Go to the documentation of this file.
1 // @(#)root/pyroot:$Id$
2 // Author: Wim Lavrijsen, Jan 2005
3 
4 // Bindings
5 #include "PyROOT.h"
6 #include "structmember.h" // from Python
7 #if PY_VERSION_HEX >= 0x02050000
8 #include "code.h" // from Python
9 #else
10 #include "compile.h" // from Python
11 #endif
12 #ifndef CO_NOFREE
13 // python2.2 does not have CO_NOFREE defined
14 #define CO_NOFREE 0x0040
15 #endif
16 #include "MethodProxy.h"
17 #include "ObjectProxy.h"
18 #include "TCallContext.h"
19 #include "TPyException.h"
20 #include "PyStrings.h"
21 
22 // Standard
23 #include <algorithm>
24 #include <vector>
25 
26 
27 namespace PyROOT {
28 
29 // TODO: only used here, but may be better off integrated with Pythonize.cxx callbacks
30  class TPythonCallback : public PyCallable {
31  public:
32  PyObject* fCallable;
33 
34  TPythonCallback( PyObject* callable ):
35  fCallable(nullptr)
36  {
37  if ( !PyCallable_Check( callable ) ) {
38  PyErr_SetString(PyExc_TypeError, "parameter must be callable");
39  return;
40  }
41  fCallable = callable;
42  Py_INCREF( fCallable );
43  }
44 
45  virtual ~TPythonCallback() {
46  Py_DECREF( fCallable );
47  fCallable = 0;
48  }
49 
50  virtual PyObject* GetSignature() { return PyROOT_PyUnicode_FromString( "*args, **kwargs" ); } ;
51  virtual PyObject* GetPrototype() { return PyROOT_PyUnicode_FromString( "<callback>" ); } ;
52  virtual PyObject* GetDocString() {
53  if ( PyObject_HasAttrString( fCallable, "__doc__" )) {
54  return PyObject_GetAttrString( fCallable, "__doc__" );
55  } else {
56  return GetPrototype();
57  }
58  }
59 
60  virtual Int_t GetPriority() { return 100; };
61 
62  virtual Int_t GetMaxArgs() { return 100; };
63  virtual PyObject* GetCoVarNames() { // TODO: pick these up from the callable
64  Py_INCREF( Py_None );
65  return Py_None;
66  }
67  virtual PyObject* GetArgDefault( Int_t /* iarg */ ) { // TODO: pick these up from the callable
68  Py_INCREF( Py_None );
69  return Py_None;
70  }
71 
72  virtual PyObject* GetScopeProxy() { // should this be the module ??
73  Py_INCREF( Py_None );
74  return Py_None;
75  }
76 
77  virtual PyCallable* Clone() { return new TPythonCallback( *this ); }
78 
79  virtual PyObject* Call(
80  ObjectProxy*& self, PyObject* args, PyObject* kwds, TCallContext* /* ctxt = 0 */ ) {
81 
82  PyObject* newArgs = nullptr;
83  if ( self ) {
84  Py_ssize_t nargs = PyTuple_Size( args );
85  newArgs = PyTuple_New( nargs+1 );
86  Py_INCREF( self );
87  PyTuple_SET_ITEM( newArgs, 0, (PyObject*)self );
88  for ( Py_ssize_t iarg = 0; iarg < nargs; ++iarg ) {
89  PyObject* pyarg = PyTuple_GET_ITEM( args, iarg );
90  Py_INCREF( pyarg );
91  PyTuple_SET_ITEM( newArgs, iarg+1, pyarg );
92  }
93  } else {
94  Py_INCREF( args );
95  newArgs = args;
96  }
97  return PyObject_Call( fCallable, newArgs, kwds );
98  }
99  };
100 
101 namespace {
102 
103 // helper to test whether a method is used in a pseudo-function modus
104  Bool_t inline IsPseudoFunc( MethodProxy* pymeth )
105  {
106  return (void*)pymeth == (void*)pymeth->fSelf;
107  }
108 
109 // helper for collecting/maintaining exception data in overload dispatch
110  struct PyError_t {
111  PyError_t() { fType = fValue = fTrace = 0; }
112 
113  static void Clear( PyError_t& e )
114  {
115  // Remove exception information.
116  Py_XDECREF( e.fType ); Py_XDECREF( e.fValue ); Py_XDECREF( e.fTrace );
117  e.fType = e.fValue = e.fTrace = 0;
118  }
119 
121  };
122 
123 // helper to hash tuple (using tuple hash would cause self-tailing loops)
124  inline Long_t HashSignature( PyObject* args )
125  {
126  // Build a hash from the types of the given python function arguments.
127  ULong_t hash = 0;
128 
129  Int_t nargs = PyTuple_GET_SIZE( args );
130  for ( Int_t i = 0; i < nargs; ++i ) {
131  hash += (ULong_t) Py_TYPE( PyTuple_GET_ITEM( args, i ) );
132  hash += (hash << 10); hash ^= (hash >> 6);
133  }
134 
135  hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15);
136 
137  return hash;
138  }
139 
140 // helper to sort on method priority
141  int PriorityCmp( PyCallable* left, PyCallable* right )
142  {
143  return left->GetPriority() > right->GetPriority();
144  }
145 
146 // return helper
147  inline void ResetCallState( ObjectProxy*& selfnew, ObjectProxy* selfold, Bool_t clear ) {
148  if ( selfnew != selfold ) {
149  Py_XDECREF( selfnew );
150  selfnew = selfold;
151  }
152 
153  if ( clear )
154  PyErr_Clear();
155  }
156 
157 // helper to factor out return logic of mp_call
158  inline PyObject* HandleReturn( MethodProxy* pymeth, ObjectProxy* oldSelf, PyObject* result ) {
159 
160  // special case for python exceptions, propagated through C++ layer
161  if ( result ) {
162 
163  // if this method creates new objects, always take ownership
164  if ( IsCreator( pymeth->fMethodInfo->fFlags ) ) {
165 
166  // either be a constructor with a fresh object proxy self ...
167  if ( IsConstructor( pymeth->fMethodInfo->fFlags ) ) {
168  if ( pymeth->fSelf )
169  pymeth->fSelf->HoldOn();
170  }
171 
172  // ... or be a method with an object proxy return value
173  else if ( ObjectProxy_Check( result ) )
174  ((ObjectProxy*)result)->HoldOn();
175  }
176 
177  // if this new object falls inside self, make sure its lifetime is proper
178  if ( ObjectProxy_Check( pymeth->fSelf ) && ObjectProxy_Check( result ) ) {
179  Long_t ptrdiff = (Long_t)((ObjectProxy*)result)->GetObject() - (Long_t)pymeth->fSelf->GetObject();
180  if ( 0 <= ptrdiff && ptrdiff < (Long_t)Cppyy::SizeOf( pymeth->fSelf->ObjectIsA() ) ) {
181  if ( PyObject_SetAttr( result, PyStrings::gLifeLine, (PyObject*)pymeth->fSelf ) == -1 )
182  PyErr_Clear(); // ignored
183  }
184  }
185  }
186 
187  // reset self as necessary to allow re-use of the MethodProxy
188  ResetCallState( pymeth->fSelf, oldSelf, kFALSE );
189 
190  return result;
191  }
192 
193 
194 //= PyROOT method proxy object behaviour =====================================
195  PyObject* mp_name( MethodProxy* pymeth, void* )
196  {
197  return PyROOT_PyUnicode_FromString( pymeth->GetName().c_str() );
198  }
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 
202  PyObject* mp_module( MethodProxy* /* pymeth */, void* )
203  {
204  Py_INCREF( PyStrings::gROOTns );
205  return PyStrings::gROOTns;
206  }
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Build python document string ('__doc__') from all C++-side overloads.
210 
211  PyObject* mp_doc( MethodProxy* pymeth, void* )
212  {
213  MethodProxy::Methods_t& methods = pymeth->fMethodInfo->fMethods;
214 
215  // collect doc strings
216  Int_t nMethods = methods.size();
217  PyObject* doc = methods[0]->GetDocString();
218 
219  // simple case
220  if ( nMethods == 1 )
221  return doc;
222 
223  // overloaded method
225  for ( Int_t i = 1; i < nMethods; ++i ) {
226  PyROOT_PyUnicode_Append( &doc, separator );
227  PyROOT_PyUnicode_AppendAndDel( &doc, methods[i]->GetDocString() );
228  }
229  Py_DECREF( separator );
230 
231  return doc;
232  }
233 
234 ////////////////////////////////////////////////////////////////////////////////
235 /// Create a new method proxy to be returned.
236 
237  PyObject* mp_meth_func( MethodProxy* pymeth, void* )
238  {
239  MethodProxy* newPyMeth = (MethodProxy*)MethodProxy_Type.tp_alloc( &MethodProxy_Type, 0 );
240 
241  // method info is shared, as it contains the collected overload knowledge
242  *pymeth->fMethodInfo->fRefCount += 1;
243  newPyMeth->fMethodInfo = pymeth->fMethodInfo;
244 
245  // new method is unbound, use of 'meth' is for keeping track whether this
246  // proxy is used in the capacity of a method or a function
247  newPyMeth->fSelf = (ObjectProxy*)newPyMeth;
248 
249  return (PyObject*)newPyMeth;
250  }
251 
252 ////////////////////////////////////////////////////////////////////////////////
253 /// Return the bound self, if any; in case of pseudo-function role, pretend
254 /// that the data member im_self does not exist.
255 
256  PyObject* mp_meth_self( MethodProxy* pymeth, void* )
257  {
258  if ( IsPseudoFunc( pymeth ) ) {
259  PyErr_Format( PyExc_AttributeError,
260  "function %s has no attribute \'im_self\'", pymeth->fMethodInfo->fName.c_str() );
261  return 0;
262  } else if ( pymeth->fSelf != 0 ) {
263  Py_INCREF( (PyObject*)pymeth->fSelf );
264  return (PyObject*)pymeth->fSelf;
265  }
266 
267  Py_INCREF( Py_None );
268  return Py_None;
269  }
270 
271 ////////////////////////////////////////////////////////////////////////////////
272 /// Return scoping class; in case of pseudo-function role, pretend that there
273 /// is no encompassing class (i.e. global scope).
274 
275  PyObject* mp_meth_class( MethodProxy* pymeth, void* )
276  {
277  if ( ! IsPseudoFunc( pymeth ) ) {
278  PyObject* pyclass = pymeth->fMethodInfo->fMethods[0]->GetScopeProxy();
279  if ( ! pyclass )
280  PyErr_Format( PyExc_AttributeError,
281  "function %s has no attribute \'im_class\'", pymeth->fMethodInfo->fName.c_str() );
282  return pyclass;
283  }
284 
285  Py_INCREF( Py_None );
286  return Py_None;
287  }
288 
289 ////////////////////////////////////////////////////////////////////////////////
290 /// Stub only, to fill out the python function interface.
291 
292  PyObject* mp_func_closure( MethodProxy* /* pymeth */, void* )
293  {
294  Py_INCREF( Py_None );
295  return Py_None;
296  }
297 
298 ////////////////////////////////////////////////////////////////////////////////
299 /// Code details are used in module inspect to fill out interactive help()
300 
301  PyObject* mp_func_code( MethodProxy* pymeth, void* )
302  {
303 #if PY_VERSION_HEX < 0x03000000
304  MethodProxy::Methods_t& methods = pymeth->fMethodInfo->fMethods;
305 
306  // collect arguments only if there is just 1 overload, otherwise put in a
307  // fake *args (see below for co_varnames)
308  PyObject* co_varnames = methods.size() == 1 ? methods[0]->GetCoVarNames() : NULL;
309  if ( !co_varnames ) {
310  // TODO: static methods need no 'self' (but is harmless otherwise)
311  co_varnames = PyTuple_New( 1 /* self */ + 1 /* fake */ );
312  PyTuple_SET_ITEM( co_varnames, 0, PyROOT_PyUnicode_FromString( "self" ) );
313  PyTuple_SET_ITEM( co_varnames, 1, PyROOT_PyUnicode_FromString( "*args" ) );
314  }
315 
316  int co_argcount = PyTuple_Size( co_varnames );
317 
318  // for now, code object representing the statement 'pass'
319  PyObject* co_code = PyString_FromStringAndSize( "d\x00\x00S", 4 );
320 
321  // tuples with all the const literals used in the function
322  PyObject* co_consts = PyTuple_New( 0 );
323  PyObject* co_names = PyTuple_New( 0 );
324 
325  // names, freevars, and cellvars go unused
326  PyObject* co_unused = PyTuple_New( 0 );
327 
328  // filename is made-up
329  PyObject* co_filename = PyString_FromString( "ROOT.py" );
330 
331  // name is the function name, also through __name__ on the function itself
332  PyObject* co_name = PyString_FromString( pymeth->GetName().c_str() );
333 
334  // firstlineno is the line number of first function code in the containing scope
335 
336  // lnotab is a packed table that maps instruction count and line number
337  PyObject* co_lnotab = PyString_FromString( "\x00\x01\x0c\x01" );
338 
339  PyObject* code = (PyObject*)PyCode_New(
340  co_argcount, // argcount
341  co_argcount + 1, // nlocals
342  2, // stacksize
343  CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE, // flags
344  co_code, // code
345  co_consts, // consts
346  co_names, // names
347  co_varnames, // varnames
348  co_unused, // freevars
349  co_unused, // cellvars
350  co_filename, // filename
351  co_name, // name
352  1, // firstlineno
353  co_lnotab ); // lnotab
354 
355  Py_DECREF( co_lnotab );
356  Py_DECREF( co_name );
357  Py_DECREF( co_unused );
358  Py_DECREF( co_filename );
359  Py_DECREF( co_varnames );
360  Py_DECREF( co_names );
361  Py_DECREF( co_consts );
362  Py_DECREF( co_code );
363 
364  return code;
365 #else
366 // not important for functioning of most code, so not implemented for p3 for now (TODO)
367  pymeth = 0;
368  if ( pymeth || !pymeth) Py_INCREF( Py_None );
369  return Py_None;
370 #endif
371  }
372 
373 ////////////////////////////////////////////////////////////////////////////////
374 /// Create a tuple of default values, if there is only one method (otherwise
375 /// leave undefined: this is only used by inspect for interactive help())
376 
377  PyObject* mp_func_defaults( MethodProxy* pymeth, void* )
378  {
379  MethodProxy::Methods_t& methods = pymeth->fMethodInfo->fMethods;
380 
381  if ( methods.size() != 1 )
382  return PyTuple_New( 0 );
383 
384  int maxarg = methods[0]->GetMaxArgs();
385 
386  PyObject* defaults = PyTuple_New( maxarg );
387 
388  int itup = 0;
389  for ( int iarg = 0; iarg < maxarg; ++iarg ) {
390  PyObject* defvalue = methods[0]->GetArgDefault( iarg );
391  if ( defvalue )
392  PyTuple_SET_ITEM( defaults, itup++, defvalue );
393  }
394  _PyTuple_Resize( &defaults, itup );
395 
396  return defaults;
397  }
398 
399 ////////////////////////////////////////////////////////////////////////////////
400 /// Return this function's global dict (hard-wired to be the ROOT module); used
401 /// for lookup of names from co_code indexing into co_names.
402 
403  PyObject* mp_func_globals( MethodProxy* /* pymeth */, void* )
404  {
405  PyObject* pyglobal = PyModule_GetDict( PyImport_AddModule( (char*)"ROOT" ) );
406  Py_XINCREF( pyglobal );
407  return pyglobal;
408  }
409 
410 ////////////////////////////////////////////////////////////////////////////////
411 /// Get '_creates' boolean, which determines ownership of return values.
412 
413  PyObject* mp_getcreates( MethodProxy* pymeth, void* )
414  {
415  return PyInt_FromLong( (Bool_t)IsCreator( pymeth->fMethodInfo->fFlags ) );
416  }
417 
418 ////////////////////////////////////////////////////////////////////////////////
419 /// Set '_creates' boolean, which determines ownership of return values.
420 
421  int mp_setcreates( MethodProxy* pymeth, PyObject* value, void* )
422  {
423  if ( ! value ) { // means that _creates is being deleted
424  pymeth->fMethodInfo->fFlags &= ~TCallContext::kIsCreator;
425  return 0;
426  }
427 
428  Long_t iscreator = PyLong_AsLong( value );
429  if ( iscreator == -1 && PyErr_Occurred() ) {
430  PyErr_SetString( PyExc_ValueError, "a boolean 1 or 0 is required for _creates" );
431  return -1;
432  }
433 
434  if ( iscreator )
435  pymeth->fMethodInfo->fFlags |= TCallContext::kIsCreator;
436  else
437  pymeth->fMethodInfo->fFlags &= ~TCallContext::kIsCreator;
438 
439  return 0;
440  }
441 
442 ////////////////////////////////////////////////////////////////////////////////
443 /// Get '_mempolicy' enum, which determines ownership of call arguments.
444 
445  PyObject* mp_getmempolicy( MethodProxy* pymeth, void* )
446  {
447  if ( (Bool_t)(pymeth->fMethodInfo->fFlags & TCallContext::kUseHeuristics ) )
448  return PyInt_FromLong( TCallContext::kUseHeuristics );
449 
450  if ( (Bool_t)(pymeth->fMethodInfo->fFlags & TCallContext::kUseStrict ) )
451  return PyInt_FromLong( TCallContext::kUseStrict );
452 
453  return PyInt_FromLong( -1 );
454  }
455 
456 ////////////////////////////////////////////////////////////////////////////////
457 /// Set '_mempolicy' enum, which determines ownership of call arguments.
458 
459  int mp_setmempolicy( MethodProxy* pymeth, PyObject* value, void* )
460  {
461  Long_t mempolicy = PyLong_AsLong( value );
462  if ( mempolicy == TCallContext::kUseHeuristics ) {
463  pymeth->fMethodInfo->fFlags |= TCallContext::kUseHeuristics;
464  pymeth->fMethodInfo->fFlags &= ~TCallContext::kUseStrict;
465  } else if ( mempolicy == TCallContext::kUseStrict ) {
466  pymeth->fMethodInfo->fFlags |= TCallContext::kUseStrict;
467  pymeth->fMethodInfo->fFlags &= ~TCallContext::kUseHeuristics;
468  } else {
469  PyErr_SetString( PyExc_ValueError,
470  "expected kMemoryStrict or kMemoryHeuristics as value for _mempolicy" );
471  return -1;
472  }
473 
474  return 0;
475  }
476 
477 ////////////////////////////////////////////////////////////////////////////////
478 /// Get '_manage_smart_ptr' boolean, which determines whether or not to
479 /// manage returned smart pointers intelligently.
480 
481  PyObject* mp_get_manage_smart_ptr( MethodProxy* pymeth, void* )
482  {
483  return PyInt_FromLong(
484  (Bool_t)(pymeth->fMethodInfo->fFlags & TCallContext::kManageSmartPtr) );
485  }
486 
487 ////////////////////////////////////////////////////////////////////////////////
488 /// Set '_manage_smart_ptr' boolean, which determines whether or not to
489 /// manage returned smart pointers intelligently.
490 
491  int mp_set_manage_smart_ptr( MethodProxy* pymeth, PyObject* value, void* )
492  {
493  Long_t policy = PyLong_AsLong( value );
494  if ( policy == -1 && PyErr_Occurred() ) {
495  PyErr_SetString( PyExc_ValueError, "a boolean 1 or 0 is required for _manage_smart_ptr" );
496  return -1;
497  }
498 
499  pymeth->fMethodInfo->fFlags |= TCallContext::kManageSmartPtr;
500 
501  return 0;
502  }
503 
504 ////////////////////////////////////////////////////////////////////////////////
505 /// Get '_threaded' boolean, which determines whether the GIL will be released.
506 
507  PyObject* mp_getthreaded( MethodProxy* pymeth, void* )
508  {
509  return PyInt_FromLong(
510  (Bool_t)(pymeth->fMethodInfo->fFlags & TCallContext::kReleaseGIL) );
511  }
512 
513 ////////////////////////////////////////////////////////////////////////////////
514 /// Set '_threaded' boolean, which determines whether the GIL will be released.
515 
516  int mp_setthreaded( MethodProxy* pymeth, PyObject* value, void* )
517  {
518  Long_t isthreaded = PyLong_AsLong( value );
519  if ( isthreaded == -1 && PyErr_Occurred() ) {
520  PyErr_SetString( PyExc_ValueError, "a boolean 1 or 0 is required for _creates" );
521  return -1;
522  }
523 
524  if ( isthreaded )
525  pymeth->fMethodInfo->fFlags |= TCallContext::kReleaseGIL;
526  else
527  pymeth->fMethodInfo->fFlags &= ~TCallContext::kReleaseGIL;
528 
529  return 0;
530  }
531 
532 ////////////////////////////////////////////////////////////////////////////////
533 
534  PyGetSetDef mp_getset[] = {
535  { (char*)"__name__", (getter)mp_name, NULL, NULL, NULL },
536  { (char*)"__module__", (getter)mp_module, NULL, NULL, NULL },
537  { (char*)"__doc__", (getter)mp_doc, NULL, NULL, NULL },
538 
539  // to be more python-like, where these are duplicated as well; to actually
540  // derive from the python method or function type is too memory-expensive,
541  // given that most of the members of those types would not be used
542  { (char*)"im_func", (getter)mp_meth_func, NULL, NULL, NULL },
543  { (char*)"im_self", (getter)mp_meth_self, NULL, NULL, NULL },
544  { (char*)"im_class", (getter)mp_meth_class, NULL, NULL, NULL },
545 
546  { (char*)"func_closure", (getter)mp_func_closure, NULL, NULL, NULL },
547  { (char*)"func_code", (getter)mp_func_code, NULL, NULL, NULL },
548  { (char*)"func_defaults", (getter)mp_func_defaults, NULL, NULL, NULL },
549  { (char*)"func_globals", (getter)mp_func_globals, NULL, NULL, NULL },
550  { (char*)"func_doc", (getter)mp_doc, NULL, NULL, NULL },
551  { (char*)"func_name", (getter)mp_name, NULL, NULL, NULL },
552 
553  { (char*)"_creates", (getter)mp_getcreates, (setter)mp_setcreates,
554  (char*)"For ownership rules of result: if true, objects are python-owned", NULL },
555  { (char*)"_mempolicy", (getter)mp_getmempolicy, (setter)mp_setmempolicy,
556  (char*)"For argument ownership rules: like global, either heuristic or strict", NULL },
557  { (char*)"_manage_smart_ptr", (getter)mp_get_manage_smart_ptr, (setter)mp_set_manage_smart_ptr,
558  (char*)"If a smart pointer is returned, determines management policy.", NULL },
559  { (char*)"_threaded", (getter)mp_getthreaded, (setter)mp_setthreaded,
560  (char*)"If true, releases GIL on call into C++", NULL },
561  { (char*)NULL, NULL, NULL, NULL, NULL }
562  };
563 
564 //= PyROOT method proxy function behavior ====================================
565  PyObject* mp_call( MethodProxy* pymeth, PyObject* args, PyObject* kwds )
566  {
567  // Call the appropriate overload of this method.
568 
569  // if called through im_func pseudo-representation (this can be gamed if the
570  // user really wants to ... )
571  if ( IsPseudoFunc( pymeth ) )
572  pymeth->fSelf = NULL;
573 
574  ObjectProxy* oldSelf = pymeth->fSelf;
575 
576  // get local handles to proxy internals
577  auto& methods = pymeth->fMethodInfo->fMethods;
578  auto& dispatchMap = pymeth->fMethodInfo->fDispatchMap;
579  auto& mflags = pymeth->fMethodInfo->fFlags;
580 
581  Int_t nMethods = methods.size();
582 
583  TCallContext ctxt = { 0 };
584  ctxt.fFlags |= (mflags & TCallContext::kUseHeuristics);
585  ctxt.fFlags |= (mflags & TCallContext::kUseStrict);
586  ctxt.fFlags |= (mflags & TCallContext::kManageSmartPtr);
587  if ( ! ctxt.fFlags ) ctxt.fFlags |= TCallContext::sMemoryPolicy;
588  ctxt.fFlags |= (mflags & TCallContext::kReleaseGIL);
589 
590  // simple case
591  if ( nMethods == 1 ) {
592  PyObject* result = methods[0]->Call( pymeth->fSelf, args, kwds, &ctxt );
593  return HandleReturn( pymeth, oldSelf, result );
594  }
595 
596  // otherwise, handle overloading
597  Long_t sighash = HashSignature( args );
598 
599  // look for known signatures ...
600  MethodProxy::DispatchMap_t::iterator m = dispatchMap.find( sighash );
601  if ( m != dispatchMap.end() ) {
602  Int_t index = m->second;
603  PyObject* result = methods[ index ]->Call( pymeth->fSelf, args, kwds, &ctxt );
604  result = HandleReturn( pymeth, oldSelf, result );
605 
606  if ( result != 0 )
607  return result;
608 
609  // fall through: python is dynamic, and so, the hashing isn't infallible
610  ResetCallState( pymeth->fSelf, oldSelf, kTRUE );
611  }
612 
613  // ... otherwise loop over all methods and find the one that does not fail
614  if ( ! IsSorted( mflags ) ) {
615  std::stable_sort( methods.begin(), methods.end(), PriorityCmp );
616  mflags |= TCallContext::kIsSorted;
617  }
618 
619  std::vector< PyError_t > errors;
620  for ( Int_t i = 0; i < nMethods; ++i ) {
621  PyObject* result = methods[i]->Call( pymeth->fSelf, args, kwds, &ctxt );
622 
623  if ( result != 0 ) {
624  // success: update the dispatch map for subsequent calls
625  dispatchMap[ sighash ] = i;
626  std::for_each( errors.begin(), errors.end(), PyError_t::Clear );
627  return HandleReturn( pymeth, oldSelf, result );
628  }
629 
630  // failure: collect error message/trace (automatically clears exception, too)
631  if ( ! PyErr_Occurred() ) {
632  // this should not happen; set an error to prevent core dump and report
633  PyObject* sig = methods[i]->GetPrototype();
634  PyErr_Format( PyExc_SystemError, "%s =>\n %s",
635  PyROOT_PyUnicode_AsString( sig ), (char*)"NULL result without error in mp_call" );
636  Py_DECREF( sig );
637  }
638  PyError_t e;
639  PyErr_Fetch( &e.fType, &e.fValue, &e.fTrace );
640  errors.push_back( e );
641  ResetCallState( pymeth->fSelf, oldSelf, kFALSE );
642  }
643 
644  // first summarize, then add details
646  "none of the %d overloaded methods succeeded. Full details:", nMethods );
648 
649  // if this point is reached, none of the overloads succeeded: notify user
650  PyObject* exc_type = NULL;
651  for ( std::vector< PyError_t >::iterator e = errors.begin(); e != errors.end(); ++e ) {
652  if ( e->fType != PyExc_NotImplementedError ) {
653  if ( ! exc_type ) exc_type = e->fType;
654  else if ( exc_type != e->fType ) exc_type = PyExc_TypeError;
655  }
656  PyROOT_PyUnicode_Append( &value, separator );
657  PyROOT_PyUnicode_Append( &value, e->fValue );
658  }
659 
660  Py_DECREF( separator );
661  std::for_each( errors.begin(), errors.end(), PyError_t::Clear );
662 
663  // report failure
664  PyErr_SetObject( exc_type ? exc_type : PyExc_TypeError, value );
665  Py_DECREF( value );
666  return 0;
667  }
668 
669 ////////////////////////////////////////////////////////////////////////////////
670 /// Descriptor; create and return a new bound method proxy (language requirement).
671 
672  MethodProxy* mp_descrget( MethodProxy* pymeth, ObjectProxy* pyobj, PyObject* )
673  {
674  MethodProxy* newPyMeth = (MethodProxy*)MethodProxy_Type.tp_alloc( &MethodProxy_Type, 0 );
675 
676  // method info is shared, as it contains the collected overload knowledge
677  *pymeth->fMethodInfo->fRefCount += 1;
678  newPyMeth->fMethodInfo = pymeth->fMethodInfo;
679 
680  // new method is to be bound to current object (may be NULL)
681  Py_XINCREF( (PyObject*)pyobj );
682  newPyMeth->fSelf = pyobj;
683 
684  return newPyMeth;
685  }
686 
687 
688 //= PyROOT method proxy construction/destruction =================================
689  MethodProxy* mp_new( PyTypeObject*, PyObject*, PyObject* )
690  {
691  // Create a new method proxy object.
692  MethodProxy* pymeth = PyObject_GC_New( MethodProxy, &MethodProxy_Type );
693  pymeth->fSelf = NULL;
694  pymeth->fMethodInfo = new MethodProxy::MethodInfo_t;
695 
696  PyObject_GC_Track( pymeth );
697  return pymeth;
698  }
699 
700 ////////////////////////////////////////////////////////////////////////////////
701 /// Deallocate memory held by method proxy object.
702 
703  void mp_dealloc( MethodProxy* pymeth )
704  {
705  PyObject_GC_UnTrack( pymeth );
706 
707  if ( ! IsPseudoFunc( pymeth ) )
708  Py_CLEAR( pymeth->fSelf );
709  pymeth->fSelf = NULL;
710 
711  if ( --(*pymeth->fMethodInfo->fRefCount) <= 0 ) {
712  delete pymeth->fMethodInfo;
713  }
714 
715  PyObject_GC_Del( pymeth );
716  }
717 
718 
719 ////////////////////////////////////////////////////////////////////////////////
720 /// Hash of method proxy object for insertion into dictionaries; with actual
721 /// method (fMethodInfo) shared, its address is best suited.
722 
723  Long_t mp_hash( MethodProxy* pymeth )
724  {
725  return _Py_HashPointer( pymeth->fMethodInfo );
726  }
727 
728 ////////////////////////////////////////////////////////////////////////////////
729 /// Garbage collector traverse of held python member objects.
730 
731  int mp_traverse( MethodProxy* pymeth, visitproc visit, void* args )
732  {
733  if ( pymeth->fSelf && ! IsPseudoFunc( pymeth ) )
734  return visit( (PyObject*)pymeth->fSelf, args );
735 
736  return 0;
737  }
738 
739 ////////////////////////////////////////////////////////////////////////////////
740 /// Garbage collector clear of held python member objects.
741 
742  int mp_clear( MethodProxy* pymeth )
743  {
744  if ( ! IsPseudoFunc( pymeth ) )
745  Py_CLEAR( pymeth->fSelf );
746  pymeth->fSelf = NULL;
747 
748  return 0;
749  }
750 
751 ////////////////////////////////////////////////////////////////////////////////
752 /// Rich set of comparison objects; only equals is defined.
753 
754  PyObject* mp_richcompare( MethodProxy* self, MethodProxy* other, int op )
755  {
756  if ( op != Py_EQ )
757  return PyType_Type.tp_richcompare( (PyObject*)self, (PyObject*)other, op );
758 
759  // defined by type + (shared) MethodInfo + bound self, with special case for fSelf (i.e. pseudo-function)
760  if ( ( Py_TYPE(self) == Py_TYPE(other) && self->fMethodInfo == other->fMethodInfo ) && \
761  ( ( IsPseudoFunc( self ) && IsPseudoFunc( other ) ) || self->fSelf == other->fSelf ) ) {
762  Py_INCREF( Py_True );
763  return Py_True;
764  }
765  Py_INCREF( Py_False );
766  return Py_False;
767  }
768 
769 
770 //= PyROOT method proxy access to internals =================================
771  PyObject* mp_disp( MethodProxy* pymeth, PyObject* sigarg )
772  {
773  // Select and call a specific C++ overload, based on its signature.
774  if ( ! PyROOT_PyUnicode_Check( sigarg ) ) {
775  PyErr_Format( PyExc_TypeError, "disp() argument 1 must be string, not %.50s",
776  sigarg == Py_None ? "None" : Py_TYPE(sigarg)->tp_name );
777  return 0;
778  }
779 
781 
782  MethodProxy::Methods_t& methods = pymeth->fMethodInfo->fMethods;
783  for ( Int_t i = 0; i < (Int_t)methods.size(); ++i ) {
784 
785  PyObject* sig2 = methods[ i ]->GetSignature();
786  if ( PyObject_RichCompareBool( sig1, sig2, Py_EQ ) ) {
787  Py_DECREF( sig2 );
788 
789  MethodProxy* newmeth = mp_new( NULL, NULL, NULL );
790  MethodProxy::Methods_t vec; vec.push_back( methods[ i ]->Clone() );
791  newmeth->Set( pymeth->fMethodInfo->fName, vec );
792 
793  if ( pymeth->fSelf && ! IsPseudoFunc( pymeth ) ) {
794  Py_INCREF( pymeth->fSelf );
795  newmeth->fSelf = pymeth->fSelf;
796  }
797 
798  Py_DECREF( sig1 );
799  return (PyObject*)newmeth;
800  }
801 
802  Py_DECREF( sig2 );
803  }
804 
805  Py_DECREF( sig1 );
806  PyErr_Format( PyExc_LookupError, "signature \"%s\" not found", PyROOT_PyUnicode_AsString( sigarg ) );
807  return 0;
808  }
809 
810 //= PyROOT method proxy access to internals =================================
811  PyObject* mp_add_overload( MethodProxy* pymeth, PyObject* new_overload )
812  {
813  TPythonCallback* cb = new TPythonCallback(new_overload);
814  pymeth->AddMethod( cb );
815  Py_INCREF( Py_None );
816  return Py_None;
817  }
818 
819  PyMethodDef mp_methods[] = {
820  { (char*)"disp", (PyCFunction)mp_disp, METH_O, (char*)"select overload for dispatch" },
821  { (char*)"__add_overload__", (PyCFunction)mp_add_overload, METH_O, (char*)"add a new overload" },
822  { (char*)NULL, NULL, 0, NULL }
823  };
824 
825 } // unnamed namespace
826 
827 ////////////////////////////////////////////////////////////////////////////////
828 
829 
830 //= PyROOT method proxy type =================================================
831 PyTypeObject MethodProxy_Type = {
832  PyVarObject_HEAD_INIT( &PyType_Type, 0 )
833  (char*)"ROOT.MethodProxy", // tp_name
834  sizeof(MethodProxy), // tp_basicsize
835  0, // tp_itemsize
836  (destructor)mp_dealloc, // tp_dealloc
837  0, // tp_print
838  0, // tp_getattr
839  0, // tp_setattr
840  0, // tp_compare
841  0, // tp_repr
842  0, // tp_as_number
843  0, // tp_as_sequence
844  0, // tp_as_mapping
845  (hashfunc)mp_hash, // tp_hash
846  (ternaryfunc)mp_call, // tp_call
847  0, // tp_str
848  0, // tp_getattro
849  0, // tp_setattro
850  0, // tp_as_buffer
851  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
852  (char*)"PyROOT method proxy (internal)", // tp_doc
853  (traverseproc)mp_traverse, // tp_traverse
854  (inquiry)mp_clear, // tp_clear
855  (richcmpfunc)mp_richcompare, // tp_richcompare
856  0, // tp_weaklistoffset
857  0, // tp_iter
858  0, // tp_iternext
859  mp_methods, // tp_methods
860  0, // tp_members
861  mp_getset, // tp_getset
862  0, // tp_base
863  0, // tp_dict
864  (descrgetfunc)mp_descrget, // tp_descr_get
865  0, // tp_descr_set
866  0, // tp_dictoffset
867  0, // tp_init
868  0, // tp_alloc
869  (newfunc)mp_new, // tp_new
870  0, // tp_free
871  0, // tp_is_gc
872  0, // tp_bases
873  0, // tp_mro
874  0, // tp_cache
875  0, // tp_subclasses
876  0 // tp_weaklist
877 #if PY_VERSION_HEX >= 0x02030000
878  , 0 // tp_del
879 #endif
880 #if PY_VERSION_HEX >= 0x02060000
881  , 0 // tp_version_tag
882 #endif
883 #if PY_VERSION_HEX >= 0x03040000
884  , 0 // tp_finalize
885 #endif
886 };
887 
888 } // namespace PyROOT
889 
890 
891 //- public members -----------------------------------------------------------
892 void PyROOT::MethodProxy::Set( const std::string& name, std::vector< PyCallable* >& methods )
893 {
894 // Fill in the data of a freshly created method proxy.
895  fMethodInfo->fName = name;
896  fMethodInfo->fMethods.swap( methods );
897  fMethodInfo->fFlags &= ~TCallContext::kIsSorted;
898  fMethodInfo->fFlags |= TCallContext::kManageSmartPtr;
899 
900 // special case: all constructors are considered creators by default
901  if ( name == "__init__" )
902  fMethodInfo->fFlags |= (TCallContext::kIsCreator | TCallContext::kIsConstructor);
903 
904 // special case, in heuristics mode also tag *Clone* methods as creators
906  name.find( "Clone" ) != std::string::npos )
907  fMethodInfo->fFlags |= TCallContext::kIsCreator;
908 }
909 
910 ////////////////////////////////////////////////////////////////////////////////
911 /// Fill in the data of a freshly created method proxy.
912 
914 {
915  fMethodInfo->fMethods.push_back( pc );
916  fMethodInfo->fFlags &= ~TCallContext::kIsSorted;
917 }
918 
919 ////////////////////////////////////////////////////////////////////////////////
920 
922 {
923  fMethodInfo->fMethods.insert( fMethodInfo->fMethods.end(),
924  meth->fMethodInfo->fMethods.begin(), meth->fMethodInfo->fMethods.end() );
925  fMethodInfo->fFlags &= ~TCallContext::kIsSorted;
926 }
927 
928 ////////////////////////////////////////////////////////////////////////////////
929 /// Destructor (this object is reference counted).
930 
932 {
933  for ( Methods_t::iterator it = fMethods.begin(); it != fMethods.end(); ++it ) {
934  delete *it;
935  }
936  fMethods.clear();
937  delete fRefCount;
938 }
#define PyROOT_PyUnicode_FromString
Definition: PyROOT.h:71
#define CO_NOFREE
Definition: MethodProxy.cxx:14
PyObject * fTrace
void Set(const std::string &name, std::vector< PyCallable * > &methods)
std::vector< PyCallable * > Methods_t
Definition: MethodProxy.h:24
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
#define Py_TYPE(ob)
Definition: PyROOT.h:151
Bool_t IsSorted(UInt_t flags)
Definition: TCallContext.h:61
#define PyROOT_PyUnicode_FromFormat
Definition: PyROOT.h:70
#define PyROOT_PyUnicode_Append
Definition: PyROOT.h:73
MethodProxy::Methods_t fMethods
Definition: MethodProxy.h:32
static const std::string separator("@@@")
#define PyROOT_PyUnicode_AsString
Definition: PyROOT.h:66
Bool_t IsConstructor(UInt_t flags)
Definition: TCallContext.h:69
Bool_t ObjectProxy_Check(T *object)
Definition: ObjectProxy.h:91
R__EXTERN PyObject * gROOTns
Definition: PyStrings.h:55
PyObject * fValue
TMarker * m
Definition: textangle.C:8
PyTypeObject MethodProxy_Type
PyObject * fType
long Long_t
Definition: RtypesCore.h:50
MethodInfo_t * fMethodInfo
Definition: MethodProxy.h:52
#define PyROOT_PyUnicode_AppendAndDel
Definition: PyROOT.h:74
void AddMethod(PyCallable *pc)
Fill in the data of a freshly created method proxy.
R__EXTERN PyObject * gLifeLine
Definition: PyStrings.h:30
unsigned long ULong_t
Definition: RtypesCore.h:51
#define PyROOT_PyUnicode_Check
Definition: PyROOT.h:64
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
~MethodInfo_t()
Destructor (this object is reference counted).
size_t SizeOf(TCppType_t klass)
Definition: Cppyy.cxx:192
int Py_ssize_t
Definition: PyROOT.h:156
Bool_t IsCreator(UInt_t flags)
Definition: TCallContext.h:65
PyObject * GetScopeProxy(Cppyy::TCppScope_t)
Retrieve scope proxy from the known ones.
#define PyVarObject_HEAD_INIT(type, size)
Definition: PyROOT.h:149
#define NULL
Definition: Rtypes.h:82
double result[121]
const Bool_t kTRUE
Definition: Rtypes.h:91
static ECallFlags sMemoryPolicy
Definition: TCallContext.h:49
char name[80]
Definition: TGX11.cxx:109
_object PyObject
Definition: TPyArg.h:22