Logo ROOT   6.08/07
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, sizeof(Long_t) );
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, sizeof(Long_t) );
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::gCppName );
504  if ( ! nattr ) nattr = PyObject_GetAttr( pyname, PyStrings::gName );
505  if ( nattr ) // object is actually a class
506  pyname = nattr;
507  pyname = PyObject_Str( pyname );
508  Py_XDECREF( nattr );
509  } else {
510  Py_INCREF( pyname );
511  }
512 
514  Py_DECREF( pyname );
515 
516  if ( ! klass ) {
517  PyErr_SetString( PyExc_TypeError,
518  "BindObject expects a valid class or class name as an argument" );
519  return 0;
520  }
521 
522  return BindCppObjectNoCast( addr, klass, kFALSE );
523  }
524 
525 ////////////////////////////////////////////////////////////////////////////////
526 /// From a long representing an address or a PyCapsule/CObject, bind to a class.
527 
528  PyObject* BindObject( PyObject*, PyObject* args )
529  {
530  Py_ssize_t argc = PyTuple_GET_SIZE( args );
531  if ( argc != 2 ) {
532  PyErr_Format( PyExc_TypeError,
533  "BindObject takes exactly 2 argumenst (" PY_SSIZE_T_FORMAT " given)", argc );
534  return 0;
535  }
536 
537  // try to convert first argument: either PyCapsule/CObject or long integer
538  PyObject* pyaddr = PyTuple_GET_ITEM( args, 0 );
539  void* addr = PyROOT_PyCapsule_GetPointer( pyaddr, NULL );
540  if ( PyErr_Occurred() ) {
541  PyErr_Clear();
542 
543  addr = PyLong_AsVoidPtr( pyaddr );
544  if ( PyErr_Occurred() ) {
545  PyErr_Clear();
546 
547  // last chance, perhaps it's a buffer/array (return from void*)
548  int buflen = Utility::GetBuffer( PyTuple_GetItem( args, 0 ), '*', 1, addr, kFALSE );
549  if ( ! addr || ! buflen ) {
550  PyErr_SetString( PyExc_TypeError,
551  "BindObject requires a CObject or long integer as first argument" );
552  return 0;
553  }
554  }
555  }
556 
557  return BindObject_( addr, PyTuple_GET_ITEM( args, 1 ) );
558  }
559 
560 ////////////////////////////////////////////////////////////////////////////////
561 /// Create an object of the given type point to NULL (historic note: this
562 /// function is older than BindObject(), which can be used instead).
563 
564  PyObject* MakeNullPointer( PyObject*, PyObject* args )
565  {
566  Py_ssize_t argc = PyTuple_GET_SIZE( args );
567  if ( argc != 0 && argc != 1 ) {
568  PyErr_Format( PyExc_TypeError,
569  "MakeNullPointer takes at most 1 argument (" PY_SSIZE_T_FORMAT " given)", argc );
570  return 0;
571  }
572 
573  // no class given, use None as generic
574  if ( argc == 0 ) {
575  Py_INCREF( Py_None );
576  return Py_None;
577  }
578 
579  return BindObject_( 0, PyTuple_GET_ITEM( args, 0 ) );
580  }
581 
582 ////////////////////////////////////////////////////////////////////////////////
583 /// This method is a helper for (un)pickling of ObjectProxy instances.
584 
585  PyObject* ObjectProxyExpand( PyObject*, PyObject* args )
586  {
587  PyObject* pybuf = 0, *pyname = 0;
588  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!O!:__expand__" ),
589  &PyBytes_Type, &pybuf, &PyBytes_Type, &pyname ) )
590  return 0;
591 
592  const char* clname = PyBytes_AS_STRING(pyname);
593 
594  // make sure that ROOT.py is loaded and fully initialized by accessing on it
595  PyObject* mod = PyImport_ImportModule( (char*)"ROOT" );
596  if ( mod ) {
597  PyObject* dummy = PyObject_GetAttrString( mod, (char*)"kRed" );
598  Py_XDECREF( dummy );
599  Py_DECREF( mod );
600  }
601 
602  // TBuffer and its derived classes can't write themselves, but can be created
603  // directly from the buffer, so handle them in a special case
604  void* newObj = 0;
605  if ( strcmp( clname, "TBufferFile" ) == 0 ) {
607  buf->WriteFastArray( PyBytes_AS_STRING(pybuf), PyBytes_GET_SIZE( pybuf ) );
608  newObj = buf;
609  } else {
610  // use the PyString macro's to by-pass error checking; do not adopt the buffer,
611  // as the local TBufferFile can go out of scope (there is no copying)
613  PyBytes_GET_SIZE( pybuf ), PyBytes_AS_STRING( pybuf ), kFALSE );
614  newObj = buf.ReadObjectAny( 0 );
615  }
616 
617  PyObject* result = BindCppObject( newObj, clname );
618  if ( result ) {
619  // this object is to be owned by the interpreter, assuming that the call
620  // originated from there
621  ((ObjectProxy*)result)->HoldOn();
622  }
623 
624  return result;
625  }
626 
627 ////////////////////////////////////////////////////////////////////////////////
628 /// Set the global memory policy, which affects object ownership when objects
629 /// are passed as function arguments.
630 
631  PyObject* SetMemoryPolicy( PyObject*, PyObject* args )
632  {
633  PyObject* policy = 0;
634  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!" ), &PyInt_Type, &policy ) )
635  return 0;
636 
637  Long_t l = PyInt_AS_LONG( policy );
638  if ( TCallContext::SetMemoryPolicy( (TCallContext::ECallFlags)l ) ) {
639  Py_INCREF( Py_None );
640  return Py_None;
641  }
642 
643  PyErr_Format( PyExc_ValueError, "Unknown policy %ld", l );
644  return 0;
645  }
646 
647 ////////////////////////////////////////////////////////////////////////////////
648 /// Set the global signal policy, which determines whether a jmp address
649 /// should be saved to return to after a C++ segfault.
650 
651  PyObject* SetSignalPolicy( PyObject*, PyObject* args )
652  {
653  PyObject* policy = 0;
654  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!" ), &PyInt_Type, &policy ) )
655  return 0;
656 
657  Long_t l = PyInt_AS_LONG( policy );
658  if ( TCallContext::SetSignalPolicy( (TCallContext::ECallFlags)l ) ) {
659  Py_INCREF( Py_None );
660  return Py_None;
661  }
662 
663  PyErr_Format( PyExc_ValueError, "Unknown policy %ld", l );
664  return 0;
665  }
666 
667 ////////////////////////////////////////////////////////////////////////////////
668 /// Set the ownership (True is python-owns) for the given object.
669 
670  PyObject* SetOwnership( PyObject*, PyObject* args )
671  {
672  ObjectProxy* pyobj = 0; PyObject* pykeep = 0;
673  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!O!" ),
674  &ObjectProxy_Type, (void*)&pyobj, &PyInt_Type, &pykeep ) )
675  return 0;
676 
677  (Bool_t)PyLong_AsLong( pykeep ) ? pyobj->HoldOn() : pyobj->Release();
678 
679  Py_INCREF( Py_None );
680  return Py_None;
681  }
682 
683 ////////////////////////////////////////////////////////////////////////////////
684 /// Add a smart pointer to the list of known smart pointer types.
685 
687  {
688  const char* type_name;
689  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "s" ), &type_name ) )
690  return nullptr;
691 
692  Cppyy::AddSmartPtrType( type_name );
693 
694  Py_RETURN_NONE;
695  }
696 
697 
698 ////////////////////////////////////////////////////////////////////////////////
699 /// Add a pinning so that objects of type `derived' are interpreted as
700 /// objects of type `base'.
701 
702  PyObject* SetTypePinning( PyObject*, PyObject* args )
703  {
704  PyRootClass* derived = nullptr, *base = nullptr;
705  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!O!" ),
706  &PyRootType_Type, &derived,
707  &PyRootType_Type, &base ) )
708  return nullptr;
709  gPinnedTypes.push_back( std::make_pair( derived->fCppType, base->fCppType ) );
710 
711  Py_RETURN_NONE;
712  }
713 
714 ////////////////////////////////////////////////////////////////////////////////
715 /// Add an exception to the type pinning for objects of type `derived'.
716 
717  PyObject* IgnoreTypePinning( PyObject*, PyObject* args )
718  {
719  PyRootClass* derived = nullptr;
720  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!" ),
721  &PyRootType_Type, &derived ) )
722  return nullptr;
723  gIgnorePinnings.push_back( derived->fCppType );
724 
725  Py_RETURN_NONE;
726  }
727 
728 ////////////////////////////////////////////////////////////////////////////////
729 /// Cast `obj' to type `type'.
730 
731  PyObject* Cast( PyObject*, PyObject* args )
732  {
733  ObjectProxy* obj = nullptr;
734  PyRootClass* type = nullptr;
735  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!O!" ),
736  &ObjectProxy_Type, &obj,
737  &PyRootType_Type, &type ) )
738  return nullptr;
739  // TODO: this misses an offset calculation, and reference type must not
740  // be cast ...
741  return BindCppObjectNoCast( obj->GetObject(), type->fCppType,
743  }
744 
745 } // unnamed namespace
746 
747 
748 //- data -----------------------------------------------------------------------
749 static PyMethodDef gPyROOTMethods[] = {
750  { (char*) "CreateScopeProxy", (PyCFunction)PyROOT::CreateScopeProxy,
751  METH_VARARGS, (char*) "PyROOT internal function" },
752  { (char*) "GetCppGlobal", (PyCFunction)PyROOT::GetCppGlobal,
753  METH_VARARGS, (char*) "PyROOT internal function" },
754  { (char*) "LookupCppEntity", (PyCFunction)LookupCppEntity,
755  METH_VARARGS, (char*) "PyROOT internal function" },
756  { (char*) "SetRootLazyLookup", (PyCFunction)SetRootLazyLookup,
757  METH_VARARGS, (char*) "PyROOT internal function" },
758  { (char*) "MakeRootTemplateClass", (PyCFunction)MakeRootTemplateClass,
759  METH_VARARGS, (char*) "PyROOT internal function" },
760  { (char*) "_DestroyPyStrings", (PyCFunction)PyROOT::DestroyPyStrings,
761  METH_NOARGS, (char*) "PyROOT internal function" },
762  { (char*) "_ResetRootModule", (PyCFunction)RootModuleResetCallback,
763  METH_NOARGS, (char*) "PyROOT internal function" },
764  { (char*) "AddressOf", (PyCFunction)AddressOf,
765  METH_VARARGS, (char*) "Retrieve address of held object in a buffer" },
766  { (char*) "addressof", (PyCFunction)addressof,
767  METH_VARARGS, (char*) "Retrieve address of held object as a value" },
768  { (char*) "AsCObject", (PyCFunction)AsCObject,
769  METH_VARARGS, (char*) "Retrieve held object in a CObject" },
770  { (char*) "BindObject", (PyCFunction)BindObject,
771  METH_VARARGS, (char*) "Create an object of given type, from given address" },
772  { (char*) "MakeNullPointer", (PyCFunction)MakeNullPointer,
773  METH_VARARGS, (char*) "Create a NULL pointer of the given type" },
774  { (char*) "_ObjectProxy__expand__", (PyCFunction)ObjectProxyExpand,
775  METH_VARARGS, (char*) "Helper method for pickling" },
776  { (char*) "SetMemoryPolicy", (PyCFunction)SetMemoryPolicy,
777  METH_VARARGS, (char*) "Determines object ownership model" },
778  { (char*) "SetSignalPolicy", (PyCFunction)SetSignalPolicy,
779  METH_VARARGS, (char*) "Trap signals in safe mode to prevent interpreter abort" },
780  { (char*) "SetOwnership", (PyCFunction)SetOwnership,
781  METH_VARARGS, (char*) "Modify held C++ object ownership" },
782  { (char*) "AddSmartPtrType", (PyCFunction)AddSmartPtrType,
783  METH_VARARGS, (char*) "Add a smart pointer to the list of known smart pointer types" },
784  { (char*) "InstallGUIEventInputHook", (PyCFunction)PyROOT::Utility::InstallGUIEventInputHook,
785  METH_NOARGS, (char*) "Install input hook to sent GUI events" },
786  { (char*) "RemoveGUIEventInputHook", (PyCFunction)PyROOT::Utility::RemoveGUIEventInputHook,
787  METH_NOARGS, (char*) "Remove input hook to sent GUI events" },
788  { (char*) "SetTypePinning", (PyCFunction)SetTypePinning,
789  METH_VARARGS, (char*) "Install a type pinning" },
790  { (char*) "IgnoreTypePinning", (PyCFunction)IgnoreTypePinning,
791  METH_VARARGS, (char*) "Don't pin the given type" },
792  { (char*) "Cast", (PyCFunction)Cast,
793  METH_VARARGS, (char*) "Cast the given object to the given type" },
794  { NULL, NULL, 0, NULL }
795 };
796 
797 
798 #if PY_VERSION_HEX >= 0x03000000
799 struct module_state {
800  PyObject *error;
801 };
802 
803 #define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
804 
805 static int rootmodule_traverse( PyObject* m, visitproc visit, void* arg )
806 {
807  Py_VISIT( GETSTATE( m )->error );
808  return 0;
809 }
810 
811 static int rootmodule_clear( PyObject* m )
812 {
813  Py_CLEAR( GETSTATE( m )->error );
814  return 0;
815 }
816 
817 
818 static struct PyModuleDef moduledef = {
819  PyModuleDef_HEAD_INIT,
820  "libPyROOT",
821  NULL,
822  sizeof(struct module_state),
823  gPyROOTMethods,
824  NULL,
825  rootmodule_traverse,
826  rootmodule_clear,
827  NULL
828 };
829 
830 ////////////////////////////////////////////////////////////////////////////////
831 /// Initialization of extension module libPyROOT.
832 
833 #define PYROOT_INIT_ERROR return NULL
834 extern "C" PyObject* PyInit_libPyROOT()
835 #else
836 #define PYROOT_INIT_ERROR return
837 extern "C" void initlibPyROOT()
838 #endif
839 {
840  using namespace PyROOT;
841 
842 // load commonly used python strings
843  if ( ! PyROOT::CreatePyStrings() )
845 
846 // prepare for lazyness
847  PyObject* dict = PyDict_New();
848 #if PY_VERSION_HEX >= 0x03030000
849  gDictLookupOrg = (dict_lookup_func)((PyDictObject*)dict)->ma_keys->dk_lookup;
850 #else
851  gDictLookupOrg = (dict_lookup_func)((PyDictObject*)dict)->ma_lookup;
852 #endif
853  Py_DECREF( dict );
854 
855 // setup PyROOT
856 #if PY_VERSION_HEX >= 0x03000000
857  gRootModule = PyModule_Create( &moduledef );
858 #else
859  gRootModule = Py_InitModule( const_cast< char* >( "libPyROOT" ), gPyROOTMethods );
860 #endif
861  if ( ! gRootModule )
863 
864 // keep gRootModule, but do not increase its reference count even as it is borrowed,
865 // or a self-referencing cycle would be created
866 
867 // Pythonizations ...
868  PyObject* userPythonizations = PyDict_New();
869  PyObject* gblList = PyList_New( 0 );
870  PyDict_SetItemString( userPythonizations, "__global__", gblList );
871  Py_DECREF( gblList );
872  PyModule_AddObject( gRootModule, "UserPythonizations", userPythonizations );
873  PyModule_AddObject( gRootModule, "UserExceptions", PyDict_New() );
874  PyModule_AddObject( gRootModule, "PythonizationScope", PyROOT_PyUnicode_FromString( "__global__" ) );
875 
876 // inject meta type
877  if ( ! Utility::InitProxy( gRootModule, &PyRootType_Type, "PyRootType" ) )
879 
880 // inject object proxy type
881  if ( ! Utility::InitProxy( gRootModule, &ObjectProxy_Type, "ObjectProxy" ) )
883 
884 // inject method proxy type
885  if ( ! Utility::InitProxy( gRootModule, &MethodProxy_Type, "MethodProxy" ) )
887 
888 // inject template proxy type
889  if ( ! Utility::InitProxy( gRootModule, &TemplateProxy_Type, "TemplateProxy" ) )
891 
892 // inject property proxy type
893  if ( ! Utility::InitProxy( gRootModule, &PropertyProxy_Type, "PropertyProxy" ) )
895 
896 // inject custom data types
897  if ( ! Utility::InitProxy( gRootModule, &TCustomFloat_Type, "Double" ) )
899 
900  if ( ! Utility::InitProxy( gRootModule, &TCustomInt_Type, "Long" ) )
902 
903  if ( ! Utility::InitProxy( gRootModule, &TCustomFloat_Type, "double" ) )
905 
906  if ( ! Utility::InitProxy( gRootModule, &TCustomInt_Type, "long" ) )
908 
909  if ( ! Utility::InitProxy( gRootModule, &TCustomInstanceMethod_Type, "InstanceMethod" ) )
911 
912  if ( ! Utility::InitProxy( gRootModule, &TTupleOfInstances_Type, "InstancesArray" ) )
914 
915  if ( ! Utility::InitProxy( gRootModule, &PyNullPtr_t_Type, "nullptr_t" ) )
917 
918 // inject identifiable nullptr
920  Py_INCREF( gNullPtrObject );
921  PyModule_AddObject( gRootModule, (char*)"nullptr", gNullPtrObject );
922 
923 // policy labels
924  PyModule_AddObject( gRootModule, (char*)"kMemoryHeuristics",
925  PyInt_FromLong( (int)TCallContext::kUseHeuristics ) );
926  PyModule_AddObject( gRootModule, (char*)"kMemoryStrict",
927  PyInt_FromLong( (int)TCallContext::kUseStrict ) );
928  PyModule_AddObject( gRootModule, (char*)"kSignalFast",
929  PyInt_FromLong( (int)TCallContext::kFast ) );
930  PyModule_AddObject( gRootModule, (char*)"kSignalSafe",
931  PyInt_FromLong( (int)TCallContext::kSafe ) );
932 
933 // setup ROOT
935 
936 // signal policy: don't abort interpreter in interactive mode
937  TCallContext::SetSignalPolicy( gROOT->IsBatch() ? TCallContext::kFast : TCallContext::kSafe );
938 
939 // inject ROOT namespace for convenience
940  PyModule_AddObject( gRootModule, (char*)"ROOT", CreateScopeProxy( "ROOT" ) );
941 
942 #if PY_VERSION_HEX >= 0x03000000
943  Py_INCREF( gRootModule );
944  return gRootModule;
945 #endif
946 }
#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:51
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: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:462
bool Bool_t
Definition: RtypesCore.h:59
void initlibPyROOT()
Definition: RootModule.cxx:837
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:836
static PyMethodDef gPyROOTMethods[]
Definition: RootModule.cxx:749
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: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:191
PyTypeObject ObjectProxy_Type
void AddSmartPtrType(const std::string &)
Definition: Cppyy.cxx:602
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: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
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:171
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:134
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.
#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
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:416
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:550
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