Logo ROOT   6.10/09
Reference Guide
RootModule.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 "PyRootType.h"
8 #include "ObjectProxy.h"
9 #include "MethodProxy.h"
10 #include "TemplateProxy.h"
11 #include "PropertyProxy.h"
12 #include "TPyBufferFactory.h"
13 #include "TCustomPyTypes.h"
14 #include "TTupleOfInstances.h"
15 #include "RootWrapper.h"
16 #include "TCallContext.h"
17 #include "Utility.h"
18 
19 // ROOT
20 #include "TObject.h" // for FindObject
21 #include "TROOT.h" // for ProcessLine and FindObject
22 
23 
24 #include "TBufferFile.h" // for pickling
25 
26 // Standard
27 #include <string>
28 #include <sstream>
29 #include <utility>
30 #include <vector>
31 
32 
33 //- from Python's dictobject.c -------------------------------------------------
34 #if PY_VERSION_HEX >= 0x03030000
35  typedef struct PyDictKeyEntry {
36  /* Cached hash code of me_key. */
37  Py_hash_t me_hash;
38  PyObject *me_key;
39  PyObject *me_value; /* This field is only meaningful for combined tables */
40  } PyDictEntry;
41 
42  typedef struct _dictkeysobject {
43  Py_ssize_t dk_refcnt;
44  Py_ssize_t dk_size;
45  dict_lookup_func dk_lookup;
46  Py_ssize_t dk_usable;
47  PyDictKeyEntry dk_entries[1];
48  } PyDictKeysObject;
49 
50 #define PYROOT_GET_DICT_LOOKUP( mp )\
51  ((dict_lookup_func&)mp->ma_keys->dk_lookup)
52 
53 #else
54 
55 #define PYROOT_GET_DICT_LOOKUP( mp )\
56  ((dict_lookup_func&)mp->ma_lookup)
57 
58 #endif
59 
60 //- data -----------------------------------------------------------------------
62 {
63  return PyBytes_FromString( "nullptr" );
64 }
65 
66 static void nullptr_dealloc( PyObject* )
67 {
68  Py_FatalError( "deallocating nullptr" );
69 }
70 
71 static int nullptr_nonzero( PyObject* )
72 {
73  return 0;
74 }
75 
76 static PyNumberMethods nullptr_as_number = {
77  0, 0, 0,
78 #if PY_VERSION_HEX < 0x03000000
79  0,
80 #endif
81  0, 0, 0, 0, 0, 0,
82  (inquiry)nullptr_nonzero, // tp_nonzero (nb_bool in p3)
83  0, 0, 0, 0, 0, 0,
84 #if PY_VERSION_HEX < 0x03000000
85  0, // nb_coerce
86 #endif
87  0, 0, 0,
88 #if PY_VERSION_HEX < 0x03000000
89  0, 0,
90 #endif
91  0, 0, 0,
92 #if PY_VERSION_HEX < 0x03000000
93  0, // nb_inplace_divide
94 #endif
95  0, 0, 0, 0, 0, 0, 0
96 #if PY_VERSION_HEX >= 0x02020000
97  , 0 // nb_floor_divide
98 #if PY_VERSION_HEX < 0x03000000
99  , 0 // nb_true_divide
100 #else
101  , 0 // nb_true_divide
102 #endif
103  , 0, 0
104 #endif
105 #if PY_VERSION_HEX >= 0x02050000
106  , 0 // nb_index
107 #endif
108 #if PY_VERSION_HEX >= 0x03050000
109  , 0 // nb_matrix_multiply
110  , 0 // nb_inplace_matrix_multiply
111 #endif
112 
113  };
114 
115 static PyTypeObject PyNullPtr_t_Type = {
116  PyVarObject_HEAD_INIT( &PyType_Type, 0 )
117  "nullptr_t", // tp_name
118  sizeof(PyObject), // tp_basicsize
119  0, // tp_itemsize
120  nullptr_dealloc, // tp_dealloc (never called)
121  0, 0, 0, 0,
122  nullptr_repr, // tp_repr
123  &nullptr_as_number, // tp_as_number
124  0, 0,
125  (hashfunc)_Py_HashPointer, // tp_hash
126  0, 0, 0, 0, 0, Py_TPFLAGS_DEFAULT, 0, 0, 0, 0, 0, 0, 0,
127  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
128 #if PY_VERSION_HEX >= 0x02030000
129  , 0 // tp_del
130 #endif
131 #if PY_VERSION_HEX >= 0x02060000
132  , 0 // tp_version_tag
133 #endif
134 #if PY_VERSION_HEX >= 0x03040000
135  , 0 // tp_finalize
136 #endif
137 };
138 
140  _PyObject_EXTRA_INIT
141  1, &PyNullPtr_t_Type
142 };
143 
144 namespace PyROOT {
145  PyObject* gRootModule = 0;
147  std::vector<std::pair<Cppyy::TCppType_t, Cppyy::TCppType_t> > gPinnedTypes;
148  std::vector<Cppyy::TCppType_t> gIgnorePinnings;
149 }
150 
151 
152 //- private helpers ------------------------------------------------------------
153 namespace {
154 
155  using namespace PyROOT;
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 
159  PyObject* RootModuleResetCallback( PyObject*, PyObject* )
160  {
161  gRootModule = 0; // reference was borrowed
162  Py_INCREF( Py_None );
163  return Py_None;
164  }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// Find a match within the ROOT module for something with name 'pyname'.
168 
169  PyObject* LookupCppEntity( PyObject* pyname, PyObject* args )
170  {
171  const char* cname = 0; long macro_ok = 0;
172  if ( pyname && PyROOT_PyUnicode_CheckExact( pyname ) )
173  cname = PyROOT_PyUnicode_AsString( pyname );
174  else if ( ! ( args && PyArg_ParseTuple( args, const_cast< char* >( "s|l" ), &cname, &macro_ok ) ) )
175  return 0;
176 
177  // we may have been destroyed if this code is called during shutdown
178  if ( !gRootModule ) {
179  PyErr_Format( PyExc_AttributeError, "%s", cname );
180  return 0;
181  }
182 
183  std::string name = cname;
184 
185  // block search for privates
186  if ( name.size() <= 2 || name.substr( 0, 2 ) != "__" ) {
187  // 1st attempt: look in myself
188  PyObject* attr = PyObject_GetAttrString( gRootModule, const_cast< char* >( cname ) );
189  if ( attr != 0 )
190  return attr;
191 
192  // 2nd attempt: construct name as a class
193  PyErr_Clear();
194  attr = CreateScopeProxy( name, 0 /* parent */);
195  if ( attr != 0 )
196  return attr;
197 
198  // 3rd attempt: lookup name as global variable
199  PyErr_Clear();
200  attr = GetCppGlobal( name );
201  if ( attr != 0 )
202  return attr;
203 
204  // 4th attempt: find existing object (e.g. from file)
205  PyErr_Clear();
206  TObject* object = gROOT->FindObject( name.c_str() );
207  if ( object != 0 )
208  return BindCppObject( object, object->IsA()->GetName() );
209 
210  // 5th attempt: global enum (pretend int, TODO: is fine for C++98, not in C++11)
211  if ( Cppyy::IsEnum( name ) ) {
212  Py_INCREF( &PyInt_Type );
213  return (PyObject*)&PyInt_Type;
214  }
215 
216  // 6th attempt: check macro's (debatable, but this worked in CINT)
217  if ( macro_ok ) {
218  PyErr_Clear();
219  std::ostringstream ismacro;
220  ismacro << "#ifdef " << name << "\n_pyroot_" << name << "=" << name
221  << ";true;\n#else\nfalse;\n#endif";
222  if ( gROOT->ProcessLine( ismacro.str().c_str() ) ) {
223  // can now retrieve this as a global
224  attr = GetCppGlobal( "_pyroot_"+name );
225  if ( attr != 0 )
226  return attr;
227  }
228  }
229  }
230 
231  // still here? raise attribute error
232  PyErr_Format( PyExc_AttributeError, "%s", name.c_str() );
233  return 0;
234  }
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 
238 #if PY_VERSION_HEX >= 0x03030000
239  inline PyDictKeyEntry* OrgDictLookup(
240  PyDictObject* mp, PyObject* key, Py_hash_t hash, PyObject*** value_addr )
241  {
242  return (*gDictLookupOrg)( mp, key, hash, value_addr );
243  }
244 
245 #define PYROOT_ORGDICT_LOOKUP( mp, key, hash, value_addr )\
246  OrgDictLookup( mp, key, hash, value_addr )
247 
248  PyDictKeyEntry* RootLookDictString(
249  PyDictObject* mp, PyObject* key, Py_hash_t hash, PyObject*** value_addr )
250 #else
251  inline PyDictEntry* OrgDictLookup( PyDictObject* mp, PyObject* key, Long_t hash )
252  {
253  return (*gDictLookupOrg)( mp, key, hash );
254  }
255 
256 #define PYROOT_ORGDICT_LOOKUP( mp, key, hash, value_addr )\
257  OrgDictLookup( mp, key, hash )
258 
259  PyDictEntry* RootLookDictString( PyDictObject* mp, PyObject* key, Long_t hash )
260 #endif
261  {
262  // first search dictionary itself
263  PyDictEntry* ep = PYROOT_ORGDICT_LOOKUP( mp, key, hash, value_addr );
264  if ( ! ep || (ep->me_key && ep->me_value) || gDictLookupActive )
265  return ep;
266 
267  // filter for builtins
268  if ( PyDict_GetItem( PyEval_GetBuiltins(), key ) != 0 ) {
269  return ep;
270  }
271 
272  // all failed, start calling into ROOT
274 
275  // ROOT globals (the round-about lookup is to prevent recursion)
276  PyObject* gval = PyDict_GetItem( PyModule_GetDict( gRootModule ), key );
277  if ( gval ) {
278  Py_INCREF( gval );
279  ep->me_value = gval;
280  ep->me_key = key;
281  ep->me_hash = hash;
282 #if PY_VERSION_HEX >= 0x03030000
283  *value_addr = &gval;
284 #endif
286  return ep;
287  }
288 
289  // attempt to get ROOT enum/global/class
290  PyObject* val = LookupCppEntity( key, 0 );
291 
292  if ( val != 0 ) {
293  // success ...
294 
295  if ( PropertyProxy_CheckExact( val ) ) {
296  // don't want to add to dictionary (the proper place would be the
297  // dictionary of the (meta)class), but modifying ep will be noticed no
298  // matter what; just return the actual value and live with the copy in
299  // the dictionary (mostly, this is correct)
300  PyObject* actual_val = Py_TYPE(val)->tp_descr_get( val, NULL, NULL );
301  Py_DECREF( val );
302  val = actual_val;
303  }
304 
305  // add reference to ROOT entity in the given dictionary
306  PYROOT_GET_DICT_LOOKUP( mp ) = gDictLookupOrg; // prevent recursion
307  if ( PyDict_SetItem( (PyObject*)mp, key, val ) == 0 ) {
308  ep = PYROOT_ORGDICT_LOOKUP( mp, key, hash, value_addr );
309  } else {
310  ep->me_key = 0;
311  ep->me_value = 0;
312  }
313  PYROOT_GET_DICT_LOOKUP( mp ) = RootLookDictString; // restore
314 
315  // done with val
316  Py_DECREF( val );
317  } else
318  PyErr_Clear();
319 
320 #if PY_VERSION_HEX >= 0x03030000
321  if ( mp->ma_keys->dk_usable <= 0 ) {
322  // big risk that this lookup will result in a resize, so force it here
323  // to be able to reset the lookup function; of course, this is nowhere
324  // near fool-proof, but should cover interactive usage ...
326  const int maxinsert = 5;
327  PyObject* buf[maxinsert];
328  for ( int varmax = 1; varmax <= maxinsert; ++varmax ) {
329  for ( int ivar = 0; ivar < varmax; ++ivar ) {
330  buf[ivar] = PyROOT_PyUnicode_FromFormat( "__ROOT_FORCE_RESIZE_%d", ivar );
331  PyDict_SetItem( (PyObject*)mp, buf[ivar], Py_None);
332  }
333  for ( int ivar = 0; ivar < varmax; ++ivar ) {
334  PyDict_DelItem( (PyObject*)mp, buf[ivar] );
335  Py_DECREF( buf[ivar] );
336  }
337  if ( 0 < mp->ma_keys->dk_usable )
338  break;
339  }
340 
341  // make sure the entry pointer is still valid by re-doing the lookup
342  ep = PYROOT_ORGDICT_LOOKUP( mp, key, hash, value_addr );
343 
344  // full reset of all lookup functions
346  PYROOT_GET_DICT_LOOKUP( mp ) = RootLookDictString; // restore
347  }
348 #endif
349 
350  // stopped calling into ROOT
352 
353  return ep;
354  }
355 
356 ////////////////////////////////////////////////////////////////////////////////
357 /// Modify the given dictionary to install the lookup function that also
358 /// tries the ROOT namespace before failing. Called on a module's dictionary,
359 /// this allows for lazy lookups.
360 
361  PyObject* SetRootLazyLookup( PyObject*, PyObject* args )
362  {
363  PyDictObject* dict = 0;
364  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!" ), &PyDict_Type, &dict ) )
365  return 0;
366 
367  // Notwithstanding the code changes, the following does not work for p3.3 and
368  // later: once the dictionary is resized for anything other than an insert (see
369  // hack in RootLookDictString), its lookup function on its keys will revert to
370  // the default (lookdict_unicode_nodummy) and only if the resizing dictionary
371  // has the generic lookdict function as dk_lookup for its keys, will this be
372  // set on the new keys.
373  PYROOT_GET_DICT_LOOKUP( dict ) = RootLookDictString;
374 
375  Py_INCREF( Py_None );
376  return Py_None;
377  }
378 
379 ////////////////////////////////////////////////////////////////////////////////
380 /// Create a binding for a templated class instantiation.
381 
382  PyObject* MakeRootTemplateClass( PyObject*, PyObject* args )
383  {
384  // args is class name + template arguments; build full instantiation
385  Py_ssize_t nArgs = PyTuple_GET_SIZE( args );
386  if ( nArgs < 2 ) {
387  PyErr_Format( PyExc_TypeError, "too few arguments for template instantiation" );
388  return 0;
389  }
390 
391  // build "< type, type, ... >" part of class name (modifies pyname)
392  PyObject* pyname = Utility::BuildTemplateName( PyTuple_GET_ITEM( args, 0 ), args, 1 );
393  if ( ! pyname )
394  return 0;
395 
396  std::string name = PyROOT_PyUnicode_AsString( pyname );
397  Py_DECREF( pyname );
398 
399  return CreateScopeProxy( name );
400  }
401 
402 ////////////////////////////////////////////////////////////////////////////////
403 /// Helper to get the address (address-of-address) of various object proxy types.
404 
405  void* GetObjectProxyAddress( PyObject*, PyObject* args )
406  {
407  ObjectProxy* pyobj = 0;
408  PyObject* pyname = 0;
409  if ( PyArg_ParseTuple( args, const_cast< char* >( "O|O!" ), &pyobj,
410  &PyROOT_PyUnicode_Type, &pyname ) &&
411  ObjectProxy_Check( pyobj ) && pyobj->fObject ) {
412 
413  if ( pyname != 0 ) {
414  // locate property proxy for offset info
415  PropertyProxy* pyprop = 0;
416 
417  PyObject* pyclass = PyObject_GetAttr( (PyObject*)pyobj, PyStrings::gClass );
418 
419  if ( pyclass ) {
420  PyObject* dict = PyObject_GetAttr( pyclass, PyStrings::gDict );
421  pyprop = (PropertyProxy*)PyObject_GetItem( dict, pyname );
422  Py_DECREF( dict );
423  }
424  Py_XDECREF( pyclass );
425 
426  if ( PropertyProxy_Check( pyprop ) ) {
427  // this is an address of a value (i.e. &myobj->prop)
428  void* addr = (void*)pyprop->GetAddress( pyobj );
429  Py_DECREF( pyprop );
430  return addr;
431  }
432 
433  Py_XDECREF( pyprop );
434 
435  PyErr_Format( PyExc_TypeError,
436  "%s is not a valid data member", PyROOT_PyUnicode_AsString( pyname ) );
437  return 0;
438  }
439 
440  // this is an address of an address (i.e. &myobj, with myobj of type MyObj*)
441  return (void*)&pyobj->fObject;
442  }
443 
444  PyErr_SetString( PyExc_ValueError, "invalid argument for AddressOf()" );
445  return 0;
446  }
447 
448  PyObject* _addressof_common( PyObject* dummy ) {
449  if ( dummy == Py_None || dummy == gNullPtrObject ) {
450  Py_INCREF( gNullPtrObject );
451  return gNullPtrObject;
452  }
453  if ( !PyErr_Occurred() ) {
454  PyObject* str = PyObject_Str( dummy );
455  if ( str && PyROOT_PyUnicode_Check( str ) )
456  PyErr_Format( PyExc_ValueError, "unknown object %s", PyBytes_AS_STRING( str ) );
457  else
458  PyErr_Format( PyExc_ValueError, "unknown object at %p", (void*)dummy );
459  Py_XDECREF( str );
460  }
461  return 0;
462  }
463 
464  PyObject* AddressOf( PyObject* dummy, PyObject* args )
465  {
466  // Return object proxy address as an indexable buffer.
467  void* addr = GetObjectProxyAddress( dummy, args );
468  if ( addr )
469  return BufFac_t::Instance()->PyBuffer_FromMemory( (Long_t*)addr, 1 );
470  if ( ! addr && PyTuple_Size( args ) ) {
471  Utility::GetBuffer( PyTuple_GetItem( args, 0 ), '*', 1, addr, kFALSE );
472  if ( addr )
473  return BufFac_t::Instance()->PyBuffer_FromMemory( (Long_t*)&addr, 1 );
474  }
475  return 0;//_addressof_common( dummy );
476  }
477 
478  PyObject* addressof( PyObject* dummy, PyObject* args )
479  {
480  // Return object proxy address as a value (cppyy-style), or the same for an array.
481  void* addr = GetObjectProxyAddress( dummy, args );
482  if ( addr )
483  return PyLong_FromLong( *(Long_t*)addr );
484  else if ( PyTuple_Size( args ) ) {
485  PyErr_Clear();
486  Utility::GetBuffer( PyTuple_GetItem( args, 0 ), '*', 1, addr, kFALSE );
487  if ( addr ) return PyLong_FromLong( (Long_t)addr );
488  }
489  return _addressof_common( dummy );
490  }
491 
492  PyObject* AsCObject( PyObject* dummy, PyObject* args )
493  {
494  // Return object proxy as an opaque CObject.
495  void* addr = GetObjectProxyAddress( dummy, args );
496  if ( addr )
497  return PyROOT_PyCapsule_New( (void*)(*(Long_t*)addr), NULL, NULL );
498 
499  return 0;
500  }
501 
502 ////////////////////////////////////////////////////////////////////////////////
503 /// Helper to factorize the common code between MakeNullPointer and BindObject.
504 
505  PyObject* BindObject_( void* addr, PyObject* pyname )
506  {
507  if ( ! PyROOT_PyUnicode_Check( pyname ) ) { // name given as string
508  PyObject* nattr = PyObject_GetAttr( pyname, PyStrings::gCppName );
509  if ( ! nattr ) nattr = PyObject_GetAttr( pyname, PyStrings::gName );
510  if ( nattr ) // object is actually a class
511  pyname = nattr;
512  pyname = PyObject_Str( pyname );
513  Py_XDECREF( nattr );
514  } else {
515  Py_INCREF( pyname );
516  }
517 
519  Py_DECREF( pyname );
520 
521  if ( ! klass ) {
522  PyErr_SetString( PyExc_TypeError,
523  "BindObject expects a valid class or class name as an argument" );
524  return 0;
525  }
526 
527  return BindCppObjectNoCast( addr, klass, kFALSE );
528  }
529 
530 ////////////////////////////////////////////////////////////////////////////////
531 /// From a long representing an address or a PyCapsule/CObject, bind to a class.
532 
533  PyObject* BindObject( PyObject*, PyObject* args )
534  {
535  Py_ssize_t argc = PyTuple_GET_SIZE( args );
536  if ( argc != 2 ) {
537  PyErr_Format( PyExc_TypeError,
538  "BindObject takes exactly 2 argumenst (" PY_SSIZE_T_FORMAT " given)", argc );
539  return 0;
540  }
541 
542  // try to convert first argument: either PyCapsule/CObject or long integer
543  PyObject* pyaddr = PyTuple_GET_ITEM( args, 0 );
544  void* addr = PyROOT_PyCapsule_GetPointer( pyaddr, NULL );
545  if ( PyErr_Occurred() ) {
546  PyErr_Clear();
547 
548  addr = PyLong_AsVoidPtr( pyaddr );
549  if ( PyErr_Occurred() ) {
550  PyErr_Clear();
551 
552  // last chance, perhaps it's a buffer/array (return from void*)
553  int buflen = Utility::GetBuffer( PyTuple_GetItem( args, 0 ), '*', 1, addr, kFALSE );
554  if ( ! addr || ! buflen ) {
555  PyErr_SetString( PyExc_TypeError,
556  "BindObject requires a CObject or long integer as first argument" );
557  return 0;
558  }
559  }
560  }
561 
562  return BindObject_( addr, PyTuple_GET_ITEM( args, 1 ) );
563  }
564 
565 ////////////////////////////////////////////////////////////////////////////////
566 /// Create an object of the given type point to NULL (historic note: this
567 /// function is older than BindObject(), which can be used instead).
568 
569  PyObject* MakeNullPointer( PyObject*, PyObject* args )
570  {
571  Py_ssize_t argc = PyTuple_GET_SIZE( args );
572  if ( argc != 0 && argc != 1 ) {
573  PyErr_Format( PyExc_TypeError,
574  "MakeNullPointer takes at most 1 argument (" PY_SSIZE_T_FORMAT " given)", argc );
575  return 0;
576  }
577 
578  // no class given, use None as generic
579  if ( argc == 0 ) {
580  Py_INCREF( Py_None );
581  return Py_None;
582  }
583 
584  return BindObject_( 0, PyTuple_GET_ITEM( args, 0 ) );
585  }
586 
587 ////////////////////////////////////////////////////////////////////////////////
588 /// This method is a helper for (un)pickling of ObjectProxy instances.
589 
590  PyObject* ObjectProxyExpand( PyObject*, PyObject* args )
591  {
592  PyObject* pybuf = 0, *pyname = 0;
593  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!O!:__expand__" ),
594  &PyBytes_Type, &pybuf, &PyBytes_Type, &pyname ) )
595  return 0;
596 
597  const char* clname = PyBytes_AS_STRING(pyname);
598 
599  // make sure that ROOT.py is loaded and fully initialized by accessing on it
600  PyObject* mod = PyImport_ImportModule( (char*)"ROOT" );
601  if ( mod ) {
602  PyObject* dummy = PyObject_GetAttrString( mod, (char*)"kRed" );
603  Py_XDECREF( dummy );
604  Py_DECREF( mod );
605  }
606 
607  // TBuffer and its derived classes can't write themselves, but can be created
608  // directly from the buffer, so handle them in a special case
609  void* newObj = 0;
610  if ( strcmp( clname, "TBufferFile" ) == 0 ) {
612  buf->WriteFastArray( PyBytes_AS_STRING(pybuf), PyBytes_GET_SIZE( pybuf ) );
613  newObj = buf;
614  } else {
615  // use the PyString macro's to by-pass error checking; do not adopt the buffer,
616  // as the local TBufferFile can go out of scope (there is no copying)
618  PyBytes_GET_SIZE( pybuf ), PyBytes_AS_STRING( pybuf ), kFALSE );
619  newObj = buf.ReadObjectAny( 0 );
620  }
621 
622  PyObject* result = BindCppObject( newObj, clname );
623  if ( result ) {
624  // this object is to be owned by the interpreter, assuming that the call
625  // originated from there
626  ((ObjectProxy*)result)->HoldOn();
627  }
628 
629  return result;
630  }
631 
632 ////////////////////////////////////////////////////////////////////////////////
633 /// Set the global memory policy, which affects object ownership when objects
634 /// are passed as function arguments.
635 
636  PyObject* SetMemoryPolicy( PyObject*, PyObject* args )
637  {
638  PyObject* policy = 0;
639  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!" ), &PyInt_Type, &policy ) )
640  return 0;
641 
642  Long_t l = PyInt_AS_LONG( policy );
643  if ( TCallContext::SetMemoryPolicy( (TCallContext::ECallFlags)l ) ) {
644  Py_INCREF( Py_None );
645  return Py_None;
646  }
647 
648  PyErr_Format( PyExc_ValueError, "Unknown policy %ld", l );
649  return 0;
650  }
651 
652 ////////////////////////////////////////////////////////////////////////////////
653 /// Set the global signal policy, which determines whether a jmp address
654 /// should be saved to return to after a C++ segfault.
655 
656  PyObject* SetSignalPolicy( PyObject*, PyObject* args )
657  {
658  PyObject* policy = 0;
659  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!" ), &PyInt_Type, &policy ) )
660  return 0;
661 
662  Long_t l = PyInt_AS_LONG( policy );
663  if ( TCallContext::SetSignalPolicy( (TCallContext::ECallFlags)l ) ) {
664  Py_INCREF( Py_None );
665  return Py_None;
666  }
667 
668  PyErr_Format( PyExc_ValueError, "Unknown policy %ld", l );
669  return 0;
670  }
671 
672 ////////////////////////////////////////////////////////////////////////////////
673 /// Set the ownership (True is python-owns) for the given object.
674 
675  PyObject* SetOwnership( PyObject*, PyObject* args )
676  {
677  ObjectProxy* pyobj = 0; PyObject* pykeep = 0;
678  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!O!" ),
679  &ObjectProxy_Type, (void*)&pyobj, &PyInt_Type, &pykeep ) )
680  return 0;
681 
682  (Bool_t)PyLong_AsLong( pykeep ) ? pyobj->HoldOn() : pyobj->Release();
683 
684  Py_INCREF( Py_None );
685  return Py_None;
686  }
687 
688 ////////////////////////////////////////////////////////////////////////////////
689 /// Add a smart pointer to the list of known smart pointer types.
690 
692  {
693  const char* type_name;
694  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "s" ), &type_name ) )
695  return nullptr;
696 
697  Cppyy::AddSmartPtrType( type_name );
698 
699  Py_RETURN_NONE;
700  }
701 
702 
703 ////////////////////////////////////////////////////////////////////////////////
704 /// Add a pinning so that objects of type `derived' are interpreted as
705 /// objects of type `base'.
706 
707  PyObject* SetTypePinning( PyObject*, PyObject* args )
708  {
709  PyRootClass* derived = nullptr, *base = nullptr;
710  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!O!" ),
711  &PyRootType_Type, &derived,
712  &PyRootType_Type, &base ) )
713  return nullptr;
714  gPinnedTypes.push_back( std::make_pair( derived->fCppType, base->fCppType ) );
715 
716  Py_RETURN_NONE;
717  }
718 
719 ////////////////////////////////////////////////////////////////////////////////
720 /// Add an exception to the type pinning for objects of type `derived'.
721 
722  PyObject* IgnoreTypePinning( PyObject*, PyObject* args )
723  {
724  PyRootClass* derived = nullptr;
725  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!" ),
726  &PyRootType_Type, &derived ) )
727  return nullptr;
728  gIgnorePinnings.push_back( derived->fCppType );
729 
730  Py_RETURN_NONE;
731  }
732 
733 ////////////////////////////////////////////////////////////////////////////////
734 /// Cast `obj' to type `type'.
735 
736  PyObject* Cast( PyObject*, PyObject* args )
737  {
738  ObjectProxy* obj = nullptr;
739  PyRootClass* type = nullptr;
740  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!O!" ),
741  &ObjectProxy_Type, &obj,
742  &PyRootType_Type, &type ) )
743  return nullptr;
744  // TODO: this misses an offset calculation, and reference type must not
745  // be cast ...
746  return BindCppObjectNoCast( obj->GetObject(), type->fCppType,
748  }
749 
750 } // unnamed namespace
751 
752 
753 //- data -----------------------------------------------------------------------
754 static PyMethodDef gPyROOTMethods[] = {
755  { (char*) "CreateScopeProxy", (PyCFunction)PyROOT::CreateScopeProxy,
756  METH_VARARGS, (char*) "PyROOT internal function" },
757  { (char*) "GetCppGlobal", (PyCFunction)PyROOT::GetCppGlobal,
758  METH_VARARGS, (char*) "PyROOT internal function" },
759  { (char*) "LookupCppEntity", (PyCFunction)LookupCppEntity,
760  METH_VARARGS, (char*) "PyROOT internal function" },
761  { (char*) "SetRootLazyLookup", (PyCFunction)SetRootLazyLookup,
762  METH_VARARGS, (char*) "PyROOT internal function" },
763  { (char*) "MakeRootTemplateClass", (PyCFunction)MakeRootTemplateClass,
764  METH_VARARGS, (char*) "PyROOT internal function" },
765  { (char*) "_DestroyPyStrings", (PyCFunction)PyROOT::DestroyPyStrings,
766  METH_NOARGS, (char*) "PyROOT internal function" },
767  { (char*) "_ResetRootModule", (PyCFunction)RootModuleResetCallback,
768  METH_NOARGS, (char*) "PyROOT internal function" },
769  { (char*) "AddressOf", (PyCFunction)AddressOf,
770  METH_VARARGS, (char*) "Retrieve address of held object in a buffer" },
771  { (char*) "addressof", (PyCFunction)addressof,
772  METH_VARARGS, (char*) "Retrieve address of held object as a value" },
773  { (char*) "AsCObject", (PyCFunction)AsCObject,
774  METH_VARARGS, (char*) "Retrieve held object in a CObject" },
775  { (char*) "BindObject", (PyCFunction)BindObject,
776  METH_VARARGS, (char*) "Create an object of given type, from given address" },
777  { (char*) "MakeNullPointer", (PyCFunction)MakeNullPointer,
778  METH_VARARGS, (char*) "Create a NULL pointer of the given type" },
779  { (char*) "_ObjectProxy__expand__", (PyCFunction)ObjectProxyExpand,
780  METH_VARARGS, (char*) "Helper method for pickling" },
781  { (char*) "SetMemoryPolicy", (PyCFunction)SetMemoryPolicy,
782  METH_VARARGS, (char*) "Determines object ownership model" },
783  { (char*) "SetSignalPolicy", (PyCFunction)SetSignalPolicy,
784  METH_VARARGS, (char*) "Trap signals in safe mode to prevent interpreter abort" },
785  { (char*) "SetOwnership", (PyCFunction)SetOwnership,
786  METH_VARARGS, (char*) "Modify held C++ object ownership" },
787  { (char*) "AddSmartPtrType", (PyCFunction)AddSmartPtrType,
788  METH_VARARGS, (char*) "Add a smart pointer to the list of known smart pointer types" },
789  { (char*) "InstallGUIEventInputHook", (PyCFunction)PyROOT::Utility::InstallGUIEventInputHook,
790  METH_NOARGS, (char*) "Install input hook to sent GUI events" },
791  { (char*) "RemoveGUIEventInputHook", (PyCFunction)PyROOT::Utility::RemoveGUIEventInputHook,
792  METH_NOARGS, (char*) "Remove input hook to sent GUI events" },
793  { (char*) "SetTypePinning", (PyCFunction)SetTypePinning,
794  METH_VARARGS, (char*) "Install a type pinning" },
795  { (char*) "IgnoreTypePinning", (PyCFunction)IgnoreTypePinning,
796  METH_VARARGS, (char*) "Don't pin the given type" },
797  { (char*) "Cast", (PyCFunction)Cast,
798  METH_VARARGS, (char*) "Cast the given object to the given type" },
799  { NULL, NULL, 0, NULL }
800 };
801 
802 
803 #if PY_VERSION_HEX >= 0x03000000
804 struct module_state {
805  PyObject *error;
806 };
807 
808 #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
809 
810 static int rootmodule_traverse( PyObject* m, visitproc visit, void* arg )
811 {
812  Py_VISIT( GETSTATE( m )->error );
813  return 0;
814 }
815 
816 static int rootmodule_clear( PyObject* m )
817 {
818  Py_CLEAR( GETSTATE( m )->error );
819  return 0;
820 }
821 
822 
823 static struct PyModuleDef moduledef = {
824  PyModuleDef_HEAD_INIT,
825  "libPyROOT",
826  NULL,
827  sizeof(struct module_state),
828  gPyROOTMethods,
829  NULL,
830  rootmodule_traverse,
831  rootmodule_clear,
832  NULL
833 };
834 
835 ////////////////////////////////////////////////////////////////////////////////
836 /// Initialization of extension module libPyROOT.
837 
838 #define PYROOT_INIT_ERROR return NULL
839 extern "C" PyObject* PyInit_libPyROOT()
840 #else
841 #define PYROOT_INIT_ERROR return
842 extern "C" void initlibPyROOT()
843 #endif
844 {
845  using namespace PyROOT;
846 
847 // load commonly used python strings
848  if ( ! PyROOT::CreatePyStrings() )
850 
851 // prepare for lazyness
852  PyObject* dict = PyDict_New();
853 #if PY_VERSION_HEX >= 0x03030000
854  gDictLookupOrg = (dict_lookup_func)((PyDictObject*)dict)->ma_keys->dk_lookup;
855 #else
856  gDictLookupOrg = (dict_lookup_func)((PyDictObject*)dict)->ma_lookup;
857 #endif
858  Py_DECREF( dict );
859 
860 // setup PyROOT
861 #if PY_VERSION_HEX >= 0x03000000
862  gRootModule = PyModule_Create( &moduledef );
863 #else
864  gRootModule = Py_InitModule( const_cast< char* >( "libPyROOT" ), gPyROOTMethods );
865 #endif
866  if ( ! gRootModule )
868 
869 // keep gRootModule, but do not increase its reference count even as it is borrowed,
870 // or a self-referencing cycle would be created
871 
872 // Pythonizations ...
873  PyObject* userPythonizations = PyDict_New();
874  PyObject* gblList = PyList_New( 0 );
875  PyDict_SetItemString( userPythonizations, "__global__", gblList );
876  Py_DECREF( gblList );
877  PyModule_AddObject( gRootModule, "UserPythonizations", userPythonizations );
878  PyModule_AddObject( gRootModule, "UserExceptions", PyDict_New() );
879  PyModule_AddObject( gRootModule, "PythonizationScope", PyROOT_PyUnicode_FromString( "__global__" ) );
880 
881 // inject meta type
882  if ( ! Utility::InitProxy( gRootModule, &PyRootType_Type, "PyRootType" ) )
884 
885 // inject object proxy type
886  if ( ! Utility::InitProxy( gRootModule, &ObjectProxy_Type, "ObjectProxy" ) )
888 
889 // inject method proxy type
890  if ( ! Utility::InitProxy( gRootModule, &MethodProxy_Type, "MethodProxy" ) )
892 
893 // inject template proxy type
894  if ( ! Utility::InitProxy( gRootModule, &TemplateProxy_Type, "TemplateProxy" ) )
896 
897 // inject property proxy type
898  if ( ! Utility::InitProxy( gRootModule, &PropertyProxy_Type, "PropertyProxy" ) )
900 
901 // inject custom data types
902  if ( ! Utility::InitProxy( gRootModule, &TCustomFloat_Type, "Double" ) )
904 
905  if ( ! Utility::InitProxy( gRootModule, &TCustomInt_Type, "Long" ) )
907 
908  if ( ! Utility::InitProxy( gRootModule, &TCustomFloat_Type, "double" ) )
910 
911  if ( ! Utility::InitProxy( gRootModule, &TCustomInt_Type, "long" ) )
913 
914  if ( ! Utility::InitProxy( gRootModule, &TCustomInstanceMethod_Type, "InstanceMethod" ) )
916 
917  if ( ! Utility::InitProxy( gRootModule, &TTupleOfInstances_Type, "InstancesArray" ) )
919 
920  if ( ! Utility::InitProxy( gRootModule, &PyNullPtr_t_Type, "nullptr_t" ) )
922 
923 // inject identifiable nullptr
925  Py_INCREF( gNullPtrObject );
926  PyModule_AddObject( gRootModule, (char*)"nullptr", gNullPtrObject );
927 
928 // policy labels
929  PyModule_AddObject( gRootModule, (char*)"kMemoryHeuristics",
930  PyInt_FromLong( (int)TCallContext::kUseHeuristics ) );
931  PyModule_AddObject( gRootModule, (char*)"kMemoryStrict",
932  PyInt_FromLong( (int)TCallContext::kUseStrict ) );
933  PyModule_AddObject( gRootModule, (char*)"kSignalFast",
934  PyInt_FromLong( (int)TCallContext::kFast ) );
935  PyModule_AddObject( gRootModule, (char*)"kSignalSafe",
936  PyInt_FromLong( (int)TCallContext::kSafe ) );
937 
938 // setup ROOT
940 
941 // signal policy: don't abort interpreter in interactive mode
942  TCallContext::SetSignalPolicy( gROOT->IsBatch() ? TCallContext::kFast : TCallContext::kSafe );
943 
944 // inject ROOT namespace for convenience
945  PyModule_AddObject( gRootModule, (char*)"ROOT", CreateScopeProxy( "ROOT" ) );
946 
947 #if PY_VERSION_HEX >= 0x03000000
948  Py_INCREF( gRootModule );
949  return gRootModule;
950 #endif
951 }
#define PyBytes_FromString
Definition: PyROOT.h:59
Bool_t InitProxy(PyObject *module, PyTypeObject *pytype, const char *name)
Initialize a proxy class for use by python, and add it to the ROOT module.
Definition: Utility.cxx:519
TCppScope_t TCppType_t
Definition: Cppyy.h:13
#define PyROOT_PyUnicode_FromString
Definition: PyROOT.h:71
PyDictEntry *(* dict_lookup_func)(PyDictObject *, PyObject *, Long_t)
Definition: PyROOT.h:43
#define pyname
Definition: TMCParticle.cxx:19
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket...
Definition: TBufferFile.h:47
R__EXTERN PyObject * gDict
Definition: PyStrings.h:22
PyObject * RemoveGUIEventInputHook()
Definition: Utility.cxx:949
static void nullptr_dealloc(PyObject *)
Definition: RootModule.cxx:66
PyTypeObject TTupleOfInstances_Type
Representation of C-style array of instances.
static PyTypeObject PyNullPtr_t_Type
Definition: RootModule.cxx:115
PyTypeObject PropertyProxy_Type
#define gROOT
Definition: TROOT.h:375
R__EXTERN dict_lookup_func gDictLookupOrg
Definition: Utility.h:15
static PyObject * nullptr_repr(PyObject *)
Definition: RootModule.cxx:61
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:462
bool Bool_t
Definition: RtypesCore.h:59
void initlibPyROOT()
Definition: RootModule.cxx:842
virtual void * ReadObjectAny(const TClass *cast)
Read object from I/O buffer.
#define PYROOT_INIT_ERROR
Definition: RootModule.cxx:841
#define NULL
Definition: RtypesCore.h:88
static PyMethodDef gPyROOTMethods[]
Definition: RootModule.cxx:754
static int nullptr_nonzero(PyObject *)
Definition: RootModule.cxx:71
R__EXTERN Bool_t gDictLookupActive
Definition: Utility.h:18
#define PyVarObject_HEAD_INIT(type, size)
Definition: PyROOT.h:149
#define PY_SSIZE_T_FORMAT
Definition: PyROOT.h:159
#define PyBytes_GET_SIZE
Definition: PyROOT.h:56
#define PyROOT_PyUnicode_FromFormat
Definition: PyROOT.h:70
void * GetAddress(ObjectProxy *pyobj)
class attributes, global properties
std::vector< Cppyy::TCppType_t > gIgnorePinnings
Definition: RootModule.cxx:148
R__EXTERN PyObject * gRootModule
Definition: ObjectProxy.cxx:39
PyObject * GetCppGlobal(const std::string &name)
try named global variable/enum (first ROOT, then Cling: sync is too slow)
PyTypeObject TCustomInt_Type
#define PyROOT_PyUnicode_Type
Definition: PyROOT.h:77
#define PyROOT_PyUnicode_AsString
Definition: PyROOT.h:66
Bool_t PropertyProxy_Check(T *object)
Definition: PropertyProxy.h:50
static PyNumberMethods nullptr_as_number
Definition: RootModule.cxx:76
R__EXTERN PyObject * gNullPtrObject
Definition: Converters.cxx:39
PyTypeObject PyRootType_Type
Definition: PyRootType.cxx:191
PyTypeObject ObjectProxy_Type
void AddSmartPtrType(const std::string &)
Definition: Cppyy.cxx:607
Bool_t ObjectProxy_Check(T *object)
Definition: ObjectProxy.h:91
R__EXTERN PyObject * gClass
Definition: PyStrings.h:18
std::vector< std::pair< Cppyy::TCppType_t, Cppyy::TCppType_t > > gPinnedTypes
Definition: RootModule.cxx:147
TMarker * m
Definition: textangle.C:8
void InitRoot()
#define PYROOT_GET_DICT_LOOKUP(mp)
Definition: RootModule.cxx:55
PyObject * DestroyPyStrings()
Remove all cached python strings.
Definition: PyStrings.cxx:116
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
PyTypeObject MethodProxy_Type
PyTypeObject TemplateProxy_Type
const Bool_t kFALSE
Definition: RtypesCore.h:92
Bool_t CreatePyStrings()
Definition: PyStrings.cxx:61
long Long_t
Definition: RtypesCore.h:50
Type object to hold TClassRef instance (this is only semantically a presentation of PyRootType instan...
Definition: PyRootType.h:37
PyObject * CreateScopeProxy(Cppyy::TCppScope_t)
Convenience function with a lookup first through the known existing proxies.
PyObject * InstallGUIEventInputHook()
Definition: Utility.cxx:936
TCppScope_t GetScope(const std::string &scope_name)
Definition: Cppyy.cxx:176
R__EXTERN PyObject * gName
Definition: PyStrings.h:33
PyTypeObject TCustomInstanceMethod_Type
int type
Definition: TGX11.cxx:120
static RooMathCoreReg dummy
#define PyROOT_PyUnicode_Check
Definition: PyROOT.h:64
#define PyBytes_Type
Definition: PyROOT.h:62
Cppyy::TCppType_t fCppType
Definition: PyRootType.h:40
static void * PyROOT_PyCapsule_GetPointer(PyObject *capsule, const char *)
Definition: PyROOT.h:84
R__EXTERN PyObject * gCppName
Definition: PyStrings.h:34
Mother of all ROOT objects.
Definition: TObject.h:37
#define PyROOT_PyUnicode_CheckExact
Definition: PyROOT.h:65
PyObject_HEAD void * fObject
Definition: ObjectProxy.h:77
#define Py_TYPE(ob)
Definition: PyROOT.h:151
int Py_ssize_t
Definition: PyROOT.h:156
PyObject _PyROOT_NullPtrStruct
Definition: RootModule.cxx:139
void * GetObject() const
Definition: ObjectProxy.h:47
int GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, Bool_t check=kTRUE)
Retrieve a linear buffer pointer from the given pyobject.
Definition: Utility.cxx:539
virtual void WriteFastArray(const Bool_t *b, Int_t n)
Write array of n bools into the I/O buffer.
Bool_t PropertyProxy_CheckExact(T *object)
Definition: PropertyProxy.h:56
PyTypeObject TCustomFloat_Type
Custom builtins, detectable by type, for pass by ref.
double result[121]
static PyObject * PyROOT_PyCapsule_New(void *cobj, const char *, void(*destr)(void *))
Definition: PyROOT.h:79
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:364
#define PyBytes_AS_STRING
Definition: PyROOT.h:54
const Bool_t kTRUE
Definition: RtypesCore.h:91
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)
Bool_t IsEnum(const std::string &type_name)
Definition: Cppyy.cxx:555
_object PyObject
Definition: TPyArg.h:20
#define PYROOT_ORGDICT_LOOKUP(mp, key, hash, value_addr)
Definition: RootModule.cxx:256