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