ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TemplateProxy.cxx
Go to the documentation of this file.
1 // Author: Wim Lavrijsen, Jan 2005
2 
3 // Bindings
4 #include "PyROOT.h"
5 #include "TemplateProxy.h"
6 #include "MethodProxy.h"
7 #include "TFunctionHolder.h"
8 #include "TMethodHolder.h"
9 #include "PyCallable.h"
10 #include "PyStrings.h"
11 #include "Utility.h"
12 
13 // ROOT
14 #include "TClass.h"
15 #include "TMethod.h"
16 
17 
18 namespace PyROOT {
19 
20 ////////////////////////////////////////////////////////////////////////////////
21 /// Initialize the proxy for the given 'pyclass.'
22 
23 void TemplateProxy::Set( const std::string& name, PyObject* pyclass )
24 {
25  fPyName = PyROOT_PyUnicode_FromString( const_cast< char* >( name.c_str() ) );
26  Py_XINCREF( pyclass );
27  fPyClass = pyclass;
28  fSelf = NULL;
29  std::vector< PyCallable* > dummy;
30  fNonTemplated = MethodProxy_New( name, dummy );
31  fTemplated = MethodProxy_New( name, dummy );
32 }
33 
34 ////////////////////////////////////////////////////////////////////////////////
35 /// Store overloads of this templated method.
36 
38  fNonTemplated->AddMethod( mp );
39 }
40 
42 // Store overload of this templated method.
43  fNonTemplated->AddMethod( pc );
44 }
45 
47 {
48 // Store know template methods.
49  fTemplated->AddMethod( pc );
50 }
51 
52 
53 namespace {
54 
55 //= PyROOT template proxy construction/destruction ===========================
56  TemplateProxy* tpp_new( PyTypeObject*, PyObject*, PyObject* )
57  {
58  // Create a new empty template method proxy.
59  TemplateProxy* pytmpl = PyObject_GC_New( TemplateProxy, &TemplateProxy_Type );
60  pytmpl->fPyName = NULL;
61  pytmpl->fPyClass = NULL;
62  pytmpl->fSelf = NULL;
63  pytmpl->fNonTemplated = NULL;
64  pytmpl->fTemplated = NULL;
65 
66  PyObject_GC_Track( pytmpl );
67  return pytmpl;
68  }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Garbage collector clear of held python member objects.
72 
73  int tpp_clear( TemplateProxy* pytmpl )
74  {
75  Py_CLEAR( pytmpl->fPyName );
76  Py_CLEAR( pytmpl->fPyClass );
77  Py_CLEAR( pytmpl->fSelf );
78  Py_CLEAR( pytmpl->fNonTemplated );
79  Py_CLEAR( pytmpl->fTemplated );
80 
81  return 0;
82  }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Destroy the given template method proxy.
86 
87  void tpp_dealloc( TemplateProxy* pytmpl )
88  {
89  PyObject_GC_UnTrack( pytmpl );
90  tpp_clear( pytmpl );
91  PyObject_GC_Del( pytmpl );
92  }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Forward to method proxies to doc all overloads
96 
97  PyObject* tpp_doc( TemplateProxy* pytmpl, void* )
98  {
99  PyObject* doc = nullptr;
100  if ( pytmpl->fNonTemplated )
101  doc = PyObject_GetAttrString( (PyObject*)pytmpl->fNonTemplated, "__doc__" );
102  if ( pytmpl->fTemplated ) {
103  PyObject* doc2 = PyObject_GetAttrString( (PyObject*)pytmpl->fTemplated, "__doc__" );
104  if ( doc && doc2 ) {
106  PyROOT_PyUnicode_AppendAndDel( &doc, doc2 );
107  } else if ( !doc && doc2 ) {
108  doc = doc2;
109  }
110  }
111 
112  if ( doc )
113  return doc;
114 
115  return PyROOT_PyUnicode_FromString( TemplateProxy_Type.tp_doc );
116  }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Garbage collector traverse of held python member objects.
120 
121  int tpp_traverse( TemplateProxy* pytmpl, visitproc visit, void* arg )
122  {
123  Py_VISIT( pytmpl->fPyName );
124  Py_VISIT( pytmpl->fPyClass );
125  Py_VISIT( pytmpl->fSelf );
126  Py_VISIT( pytmpl->fNonTemplated );
127  Py_VISIT( pytmpl->fTemplated );
128 
129  return 0;
130  }
131 
132 //= PyROOT template proxy callable behavior ==================================
133  PyObject* tpp_call( TemplateProxy* pytmpl, PyObject* args, PyObject* kwds )
134  {
135  // Dispatcher to the actual member method, several uses possible; in order:
136  //
137  // case 1:
138  //
139  // obj.method( a0, a1, ... ) # non-template
140  // => obj->method( a0, a1, ... ) // non-template
141  //
142  // case 2:
143  //
144  // obj.method( t0, t1, ... )( a0, a1, ... )
145  // -> getattr( obj, 'method< t0, t1, ... >' )( a0, a1, ... )
146  // => obj->method< t0, t1, ... >( a0, a1, ... )
147  //
148  // case 3:
149  //
150  // obj.method( a0, a1, ... ) # all known templates
151  // => obj->method( a0, a1, ... ) // all known templates
152  //
153  // case 4:
154  //
155  // collect types of arguments unless they are a type themselves (the idea
156  // here is it is more likely for e.g. (1, 3.14) to be real arguments and
157  // e.g. (int, 'double') to be template arguments:
158  // a) if ! is_type(arg)
159  // template<> method< T0, T1, ... >( type(a0), type(a1), ... )
160  // b) else
161  // drop to case 5
162  // TODO: there is ambiguity in the above if the arguments are strings
163  //
164  // case 5:
165  //
166  // instantiate template<> method< t0, t1, ... >
167 
168  // case 1: obj->method( a0, a1, ... )
169 
170  // simply forward the call: all non-templated methods are defined on class definition
171  // and thus already available
172  PyObject* pymeth = MethodProxy_Type.tp_descr_get(
173  (PyObject*)pytmpl->fNonTemplated, pytmpl->fSelf, (PyObject*)&MethodProxy_Type );
174  if ( MethodProxy_Check( pymeth ) ) {
175  // now call the method with the arguments
176  PyObject* result = MethodProxy_Type.tp_call( pymeth, args, kwds );
177  Py_DECREF( pymeth ); pymeth = 0;
178  if ( result )
179  return result;
180  // TODO: collect error here, as the failure may be either an overload
181  // failure after which we should continue; or a real failure, which should
182  // be reported.
183  }
184  Py_XDECREF( pymeth ); pymeth = 0;
185  PyErr_Clear();
186 
187  // error check on method() which can not be derived if non-templated case fails
188  Py_ssize_t nArgs = PyTuple_GET_SIZE( args );
189  if ( nArgs == 0 ) {
190  PyErr_Format( PyExc_TypeError, "template method \'%s\' with no arguments must be explicit",
191  PyROOT_PyUnicode_AsString( pytmpl->fPyName ) );
192  return 0;
193  }
194 
195  // case 2: non-instantiating obj->method< t0, t1, ... >( a0, a1, ... )
196 
197  // build "< type, type, ... >" part of method name
198  PyObject* pyname_v1 = Utility::BuildTemplateName( pytmpl->fPyName, args, 0 );
199  if ( pyname_v1 ) {
200  // lookup method on self (to make sure it propagates), which is readily callable
201  pymeth = PyObject_GetAttr( pytmpl->fSelf ? pytmpl->fSelf : pytmpl->fPyClass, pyname_v1 );
202  if ( pymeth ) { // overloads stop here, as this is an explicit match
203  Py_DECREF( pyname_v1 );
204  return pymeth; // callable method, next step is by user
205  }
206  }
207  PyErr_Clear();
208 
209  // case 3: loop over all previously instantiated templates
210  pymeth = MethodProxy_Type.tp_descr_get(
211  (PyObject*)pytmpl->fTemplated, pytmpl->fSelf, (PyObject*)&MethodProxy_Type );
212  if ( MethodProxy_Check( pymeth ) ) {
213  // now call the method with the arguments
214  PyObject* result = MethodProxy_Type.tp_call( pymeth, args, kwds );
215  Py_DECREF( pymeth ); pymeth = 0;
216  if ( result ) {
217  Py_XDECREF( pyname_v1 );
218  return result;
219  }
220  // TODO: collect error here, as the failure may be either an overload
221  // failure after which we should continue; or a real failure, which should
222  // be reported.
223  }
224  Py_XDECREF( pymeth ); pymeth = 0;
225  PyErr_Clear();
226 
227  // still here? try instantiating methods
228 
229  Bool_t isType = kFALSE;
230  Int_t nStrings = 0;
231  PyObject* tpArgs = PyTuple_New( nArgs );
232  for ( Int_t i = 0; i < nArgs; ++i ) {
233  PyObject* itemi = PyTuple_GET_ITEM( args, i );
234  if ( PyType_Check( itemi ) ) isType = kTRUE;
235  else if ( ! isType && PyBytes_Check( itemi ) ) nStrings += 1;
236  // special case for arrays
237  PyObject* pytc = PyObject_GetAttr( itemi, PyStrings::gTypeCode );
238  if ( ! ( pytc && PyROOT_PyUnicode_Check( pytc ) ) ) {
239  // normal case (not an array)
240  PyErr_Clear();
241  PyObject* tp = (PyObject*)Py_TYPE( itemi );
242  Py_INCREF( tp );
243  PyTuple_SET_ITEM( tpArgs, i, tp );
244  } else {
245  // array, build up a pointer type
246  char tc = ((char*)PyROOT_PyUnicode_AsString( pytc ))[0];
247  const char* ptrname = 0;
248  switch ( tc ) {
249  case 'b': ptrname = "char*"; break;
250  case 'h': ptrname = "short*"; break;
251  case 'H': ptrname = "unsigned short*"; break;
252  case 'i': ptrname = "int*"; break;
253  case 'I': ptrname = "unsigned int*"; break;
254  case 'l': ptrname = "long*"; break;
255  case 'L': ptrname = "unsigned long*"; break;
256  case 'f': ptrname = "float*"; break;
257  case 'd': ptrname = "double*"; break;
258  default: ptrname = "void*"; // TODO: verify if this is right
259  }
260  if ( ptrname ) {
261  PyObject* pyptrname = PyBytes_FromString( ptrname );
262  PyTuple_SET_ITEM( tpArgs, i, pyptrname );
263  // string added, but not counted towards nStrings
264  } else {
265  // this will cleanly fail instantiation
266  Py_INCREF( pytc );
267  PyTuple_SET_ITEM( tpArgs, i, pytc );
268  }
269  }
270  Py_XDECREF( pytc );
271  }
272 
273  PyObject* clName = PyObject_GetAttr( pytmpl->fPyClass, PyStrings::gName );
274  TClass* klass = TClass::GetClass( PyROOT_PyUnicode_AsString( clName ) );
275  Py_DECREF( clName );
276  const std::string& tmplname = pytmpl->fNonTemplated->fMethodInfo->fName;
277 
278  // case 4a: instantiating obj->method< T0, T1, ... >( type(a0), type(a1), ... )( a0, a1, ... )
279  if ( ! isType && ! ( nStrings == nArgs ) ) { // no types among args and not all strings
280  PyObject* pyname_v2 = Utility::BuildTemplateName( NULL, tpArgs, 0 );
281  if ( pyname_v2 ) {
282  std::string mname = PyROOT_PyUnicode_AsString( pyname_v2 );
283  Py_DECREF( pyname_v2 );
284  std::string proto = mname.substr( 1, mname.size() - 2 );
285  // the following causes instantiation as necessary
286  TMethod* cppmeth = klass ? klass->GetMethodWithPrototype( tmplname.c_str(), proto.c_str() ) : 0;
287  if ( cppmeth ) { // overload stops here
288  Py_XDECREF( pyname_v1 );
289  Cppyy::TCppScope_t scope = Cppyy::GetScope( klass->GetName() );
290  if ( (klass->Property() & kIsNamespace) || (cppmeth->Property() & kIsStatic) ) {
291  pytmpl->fTemplated->AddMethod( new TFunctionHolder( scope, (Cppyy::TCppMethod_t)cppmeth ) );
292  pymeth = (PyObject*)MethodProxy_New(
293  cppmeth->GetName(), new TFunctionHolder( scope, (Cppyy::TCppMethod_t)cppmeth ) );
294  } else {
295  pytmpl->fTemplated->AddMethod( new TMethodHolder( scope, (Cppyy::TCppMethod_t)cppmeth ) );
296  pymeth = (PyObject*)MethodProxy_New(
297  cppmeth->GetName(), new TMethodHolder( scope, (Cppyy::TCppMethod_t)cppmeth ) );
298  }
299  PyObject_SetAttrString( pytmpl->fPyClass, (char*)cppmeth->GetName(), (PyObject*)pymeth );
300  Py_DECREF( pymeth );
301  pymeth = PyObject_GetAttrString(
302  pytmpl->fSelf ? pytmpl->fSelf : pytmpl->fPyClass, (char*)cppmeth->GetName() );
303  PyObject* result = MethodProxy_Type.tp_call( pymeth, args, kwds );
304  Py_DECREF( pymeth );
305  return result;
306  }
307  }
308  }
309 
310  // case 4b/5: instantiating obj->method< t0, t1, ... >( a0, a1, ... )
311  if ( pyname_v1 ) {
312  std::string mname = PyROOT_PyUnicode_AsString( pyname_v1 );
313  // the following causes instantiation as necessary
314  TMethod* cppmeth = klass ? klass->GetMethodAny( mname.c_str() ) : 0;
315  if ( cppmeth ) { // overload stops here
316  pymeth = (PyObject*)MethodProxy_New(
317  mname, new TMethodHolder( Cppyy::GetScope( klass->GetName() ), (Cppyy::TCppMethod_t)cppmeth ) );
318  PyObject_SetAttr( pytmpl->fPyClass, pyname_v1, (PyObject*)pymeth );
319  if ( mname != cppmeth->GetName() ) // happens with typedefs and template default arguments
320  PyObject_SetAttrString( pytmpl->fPyClass, (char*)mname.c_str(), (PyObject*)pymeth );
321  Py_DECREF( pymeth );
322  pymeth = PyObject_GetAttr( pytmpl->fSelf ? pytmpl->fSelf : pytmpl->fPyClass, pyname_v1 );
323  Py_DECREF( pyname_v1 );
324  return pymeth; // callable method, next step is by user
325  }
326  Py_DECREF( pyname_v1 );
327  }
328 
329  // moderately generic error message, but should be clear enough
330  PyErr_Format( PyExc_TypeError, "can not resolve method template call for \'%s\'",
331  PyROOT_PyUnicode_AsString( pytmpl->fPyName ) );
332  return 0;
333  }
334 
335 ////////////////////////////////////////////////////////////////////////////////
336 /// create and use a new template proxy (language requirement)
337 
338  TemplateProxy* tpp_descrget( TemplateProxy* pytmpl, PyObject* pyobj, PyObject* )
339  {
340  TemplateProxy* newPyTmpl = (TemplateProxy*)TemplateProxy_Type.tp_alloc( &TemplateProxy_Type, 0 );
341 
342  // copy name and class pointers
343  Py_INCREF( pytmpl->fPyName );
344  newPyTmpl->fPyName = pytmpl->fPyName;
345 
346  Py_XINCREF( pytmpl->fPyClass );
347  newPyTmpl->fPyClass = pytmpl->fPyClass;
348 
349  // copy non-templated method proxy pointer
350  Py_INCREF( pytmpl->fNonTemplated );
351  newPyTmpl->fNonTemplated = pytmpl->fNonTemplated;
352 
353  // copy templated method proxy pointer
354  Py_INCREF( pytmpl->fTemplated );
355  newPyTmpl->fTemplated = pytmpl->fTemplated;
356 
357  // new method is to be bound to current object (may be NULL)
358  Py_XINCREF( pyobj );
359  newPyTmpl->fSelf = pyobj;
360 
361  return newPyTmpl;
362  }
363 
364 ////////////////////////////////////////////////////////////////////////////////
365 
366  PyGetSetDef tpp_getset[] = {
367  { (char*)"__doc__", (getter)tpp_doc, NULL, NULL, NULL },
368  { (char*)NULL, NULL, NULL, NULL, NULL }
369  };
370 
371 } // unnamed namespace
372 
373 
374 //= PyROOT template proxy type ===============================================
375 PyTypeObject TemplateProxy_Type = {
376  PyVarObject_HEAD_INIT( &PyType_Type, 0 )
377  (char*)"ROOT.TemplateProxy", // tp_name
378  sizeof(TemplateProxy), // tp_basicsize
379  0, // tp_itemsize
380  (destructor)tpp_dealloc, // tp_dealloc
381  0, // tp_print
382  0, // tp_getattr
383  0, // tp_setattr
384  0, // tp_compare
385  0, // tp_repr
386  0, // tp_as_number
387  0, // tp_as_sequence
388  0, // tp_as_mapping
389  0, // tp_hash
390  (ternaryfunc)tpp_call, // tp_call
391  0, // tp_str
392  0, // tp_getattro
393  0, // tp_setattro
394  0, // tp_as_buffer
395  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, // tp_flags
396  (char*)"PyROOT template proxy (internal)", // tp_doc
397  (traverseproc)tpp_traverse,// tp_traverse
398  (inquiry)tpp_clear, // tp_clear
399  0, // tp_richcompare
400  0, // tp_weaklistoffset
401  0, // tp_iter
402  0, // tp_iternext
403  0, // tp_methods
404  0, // tp_members
405  tpp_getset, // tp_getset
406  0, // tp_base
407  0, // tp_dict
408  (descrgetfunc)tpp_descrget,// tp_descr_get
409  0, // tp_descr_set
410  0, // tp_dictoffset
411  0, // tp_init
412  0, // tp_alloc
413  (newfunc)tpp_new, // tp_new
414  0, // tp_free
415  0, // tp_is_gc
416  0, // tp_bases
417  0, // tp_mro
418  0, // tp_cache
419  0, // tp_subclasses
420  0 // tp_weaklist
421 #if PY_VERSION_HEX >= 0x02030000
422  , 0 // tp_del
423 #endif
424 #if PY_VERSION_HEX >= 0x02060000
425  , 0 // tp_version_tag
426 #endif
427 #if PY_VERSION_HEX >= 0x03040000
428  , 0 // tp_finalize
429 #endif
430 };
431 
432 } // namespace PyROOT
#define PyBytes_FromString
Definition: PyROOT.h:59
#define PyROOT_PyUnicode_FromString
Definition: PyROOT.h:71
void AddOverload(MethodProxy *mp)
Store overloads of this templated method.
MethodProxy * fNonTemplated
Definition: TemplateProxy.h:33
Long_t Property() const
Set TObject::fBits and fStreamerType to cache information about the class.
Definition: TClass.cxx:5560
MethodProxy * MethodProxy_New(const std::string &name, std::vector< PyCallable * > &methods)
Definition: MethodProxy.h:75
int Int_t
Definition: RtypesCore.h:41
PyObject * BuildTemplateName(PyObject *pyname, PyObject *args, int argoff)
Helper to construct the "< type, type, ... >" part of a templated name (either for a class as in Make...
Definition: Utility.cxx:459
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
#define PyVarObject_HEAD_INIT(type, size)
Definition: PyROOT.h:147
#define PyROOT_PyUnicode_AsString
Definition: PyROOT.h:66
MethodProxy * fTemplated
Definition: TemplateProxy.h:34
#define PyBytes_Check
Definition: PyROOT.h:52
ptrdiff_t TCppMethod_t
Definition: Cppyy.h:15
PyObject_HEAD PyObject * fSelf
Definition: TemplateProxy.h:30
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
Long_t Property() const
Get property description word. For meaning of bits see EProperty.
Definition: TFunction.cxx:183
void AddTemplate(PyCallable *pc)
PyTypeObject MethodProxy_Type
void Set(const std::string &name, PyObject *pyclass)
Initialize the proxy for the given 'pyclass.'.
#define PyROOT_PyUnicode_AppendAndDel
Definition: PyROOT.h:74
TCppScope_t GetScope(const std::string &scope_name)
Definition: Cppyy.cxx:150
R__EXTERN PyObject * gName
Definition: PyStrings.h:33
void AddMethod(PyCallable *pc)
Fill in the data of a freshly created method proxy.
R__EXTERN PyObject * gTypeCode
Definition: PyStrings.h:35
static RooMathCoreReg dummy
#define PyROOT_PyUnicode_Check
Definition: PyROOT.h:64
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:2801
#define name(a, b)
Definition: linkTestLib0.cpp:5
#define Py_TYPE(ob)
Definition: PyROOT.h:149
int Py_ssize_t
Definition: PyROOT.h:154
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:40
Bool_t MethodProxy_Check(T *object)
Definition: MethodProxy.h:63
#define NULL
Definition: Rtypes.h:82
double result[121]
TMethod * GetMethodAny(const char *method)
Return pointer to method without looking at parameters.
Definition: TClass.cxx:4027
const Bool_t kTRUE
Definition: Rtypes.h:91
TMethod * GetMethodWithPrototype(const char *method, const char *proto, Bool_t objectIsConst=kFALSE, ROOT::EFunctionMatchMode mode=ROOT::kConversionMatch)
Find the method with a given prototype.
Definition: TClass.cxx:4113
ptrdiff_t TCppScope_t
Definition: Cppyy.h:12
Template proxy object to return functions and methods.
Definition: TemplateProxy.h:24
_object PyObject
Definition: TPyArg.h:22