Logo ROOT   6.08/07
Reference Guide
ObjectProxy.cxx
Go to the documentation of this file.
1 // @(#)root/pyroot:$Id$
2 // Author: Wim Lavrijsen, Jan 2005
3 
4 // Bindings
5 #include "PyROOT.h"
6 #include "PyStrings.h"
7 #include "ObjectProxy.h"
8 #include "RootWrapper.h"
9 #include "Utility.h"
10 
11 // ROOT
12 #include "TBufferFile.h" // for pickling
13 #include "TClass.h" // id.
14 #include "TObject.h" // for gROOT life-check
15 #include "TROOT.h" // id.
16 
17 // Standard
18 #include <algorithm>
19 
20 
21 //______________________________________________________________________________
22 // Python-side proxy objects
23 // =========================
24 //
25 // C++ objects are represented in Python by ObjectProxy's, which encapsulate
26 // them using either a pointer (normal), pointer-to-pointer (kIsReference set),
27 // or as an owned value (kIsValue set). Objects held as reference are never
28 // owned, otherwise the object is owned if kIsOwner is set.
29 //
30 // In addition to encapsulation, ObjectProxy offers pickling (using TBufferFile
31 // with a copy into a Python string); rudimentary comparison operators (based on
32 // pointer value and class comparisons); stubs for numeric operators; and a
33 // representation that prints the C++ pointer values, rather than the PyObject*
34 // ones as is the default.
35 
36 
37 //- data _______________________________________________________________________
38 namespace PyROOT {
39  R__EXTERN PyObject* gRootModule; // needed for pickling
40 }
41 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Destroy the held C++ object, if owned; does not deallocate the proxy.
45 
47  if ( gROOT && !gROOT->TestBit( TObject::kInvalidObject ) ) {
48  if ( pyobj->fFlags & ObjectProxy::kIsValue ) {
49  if ( ! (pyobj->fFlags & ObjectProxy::kIsSmartPtr) ) {
50  Cppyy::CallDestructor( pyobj->ObjectIsA(), pyobj->GetObject() );
51  Cppyy::Deallocate( pyobj->ObjectIsA(), pyobj->GetObject() );
52  } else {
54  Cppyy::Deallocate( pyobj->fSmartPtrType, pyobj->fSmartPtr );
55  }
56  }
57  else if ( pyobj->fObject && ( pyobj->fFlags & ObjectProxy::kIsOwner ) ) {
58  if ( ! (pyobj->fFlags & ObjectProxy::kIsSmartPtr) ) {
59  Cppyy::Destruct( pyobj->ObjectIsA(), pyobj->GetObject() );
60  } else {
61  Cppyy::Destruct( pyobj->fSmartPtrType, pyobj->fSmartPtr );
62  }
63  }
64  }
65  pyobj->fObject = NULL;
66 }
67 
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 
71 namespace PyROOT {
72 namespace {
73 
74 //= PyROOT object proxy null-ness checking ===================================
75  PyObject* op_nonzero( ObjectProxy* self )
76  {
77  // Null of the proxy is determined by null-ness of the held C++ object.
78  PyObject* result = self->GetObject() ? Py_True : Py_False;
79  Py_INCREF( result );
80  return result;
81  }
82 
83 //= PyROOT object explicit destruction =======================================
84  PyObject* op_destruct( ObjectProxy* self )
85  {
86  // User access to force deletion of the object. Needed in case of a true
87  // garbage collector (like in PyPy), to allow the user control over when
88  // the C++ destructor is called. This method requires that the C++ object
89  // is owned (no-op otherwise).
90  op_dealloc_nofree( self );
91  Py_INCREF( Py_None );
92  return Py_None;
93  }
94 
95 //= PyROOT object proxy pickle support =======================================
96  PyObject* op_reduce( ObjectProxy* self )
97  {
98  // Turn the object proxy instance into a character stream and return for
99  // pickle, together with the callable object that can restore the stream
100  // into the object proxy instance.
101 
102  // keep a borrowed reference around to the callable function for expanding;
103  // because it is borrowed, it means that there can be no pickling during the
104  // shutdown of the libPyROOT module
105  static PyObject* s_expand = PyDict_GetItemString(
106  PyModule_GetDict( gRootModule ), const_cast< char* >( "_ObjectProxy__expand__" ) );
107 
108  // TBuffer and its derived classes can't write themselves, but can be created
109  // directly from the buffer, so handle them in a special case
110  static Cppyy::TCppType_t s_bfClass = Cppyy::GetScope( "TBufferFile" );
111 
112  TBufferFile* buff = 0;
113  if ( s_bfClass == self->ObjectIsA() ) {
114  buff = (TBufferFile*)self->GetObject();
115  } else {
116  // no cast is needed, but WriteObject taking a TClass argument is protected,
117  // so use WriteObjectAny()
118  static TBufferFile s_buff( TBuffer::kWrite );
119  s_buff.Reset();
120  if ( s_buff.WriteObjectAny( self->GetObject(),
121  TClass::GetClass( Cppyy::GetFinalName( self->ObjectIsA() ).c_str() ) ) != 1 ) {
122  PyErr_Format( PyExc_IOError,
123  "could not stream object of type %s", Cppyy::GetFinalName( self->ObjectIsA() ).c_str() );
124  return 0;
125  }
126  buff = &s_buff;
127  }
128 
129  // use a string for the serialized result, as a python buffer will not copy
130  // the buffer contents; use a string for the class name, used when casting
131  // on reading back in (see RootModule.cxx:TObjectExpand)
132  PyObject* res2 = PyTuple_New( 2 );
133  PyTuple_SET_ITEM( res2, 0, PyBytes_FromStringAndSize( buff->Buffer(), buff->Length() ) );
134  PyTuple_SET_ITEM( res2, 1, PyBytes_FromString( Cppyy::GetFinalName( self->ObjectIsA() ).c_str() ) );
135 
136  PyObject* result = PyTuple_New( 2 );
137  Py_INCREF( s_expand );
138  PyTuple_SET_ITEM( result, 0, s_expand );
139  PyTuple_SET_ITEM( result, 1, res2 );
140 
141  return result;
142  }
143 
144 //= PyROOT object dispatch support ===========================================
145  PyObject* op_dispatch( PyObject* self, PyObject* args, PyObject* /* kdws */ )
146  {
147  // User-side __dispatch__ method to allow selection of a specific overloaded
148  // method. The actual selection is in the disp() method of MethodProxy.
149  PyObject *mname = 0, *sigarg = 0;
150  if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!O!:__dispatch__" ),
151  &PyROOT_PyUnicode_Type, &mname, &PyROOT_PyUnicode_Type, &sigarg ) )
152  return 0;
153 
154  // get the named overload
155  PyObject* pymeth = PyObject_GetAttr( self, mname );
156  if ( ! pymeth )
157  return 0;
158 
159  // get the 'disp' method to allow overload selection
160  PyObject* pydisp = PyObject_GetAttrString( pymeth, const_cast<char*>( "disp" ) );
161  if ( ! pydisp ) {
162  Py_DECREF( pymeth );
163  return 0;
164  }
165 
166  // finally, call dispatch to get the specific overload
167  PyObject* oload = PyObject_CallFunctionObjArgs( pydisp, sigarg, NULL );
168  Py_DECREF( pydisp );
169  Py_DECREF( pymeth );
170  return oload;
171  }
172 
173 //= PyROOT smart pointer support =============================================
174  PyObject* op_get_smart_ptr( ObjectProxy* self )
175  {
176  if ( !( self->fFlags & ObjectProxy::kIsSmartPtr ) ) {
177  Py_RETURN_NONE;
178  }
179 
180  return (PyObject*)PyROOT::BindCppObject( self->fSmartPtr, self->fSmartPtrType );
181  }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 
185  PyMethodDef op_methods[] = {
186  { (char*)"__nonzero__", (PyCFunction)op_nonzero, METH_NOARGS, NULL },
187  { (char*)"__bool__", (PyCFunction)op_nonzero, METH_NOARGS, NULL }, // for p3
188  { (char*)"__destruct__", (PyCFunction)op_destruct, METH_NOARGS, NULL },
189  { (char*)"__reduce__", (PyCFunction)op_reduce, METH_NOARGS, NULL },
190  { (char*)"__dispatch__", (PyCFunction)op_dispatch, METH_VARARGS, (char*)"dispatch to selected overload" },
191  { (char*)"_get_smart_ptr", (PyCFunction)op_get_smart_ptr, METH_NOARGS, (char*)"get associated smart pointer, if any" },
192  { (char*)NULL, NULL, 0, NULL }
193  };
194 
195 
196 //= PyROOT object proxy construction/destruction =============================
197  ObjectProxy* op_new( PyTypeObject* subtype, PyObject*, PyObject* )
198  {
199  // Create a new object proxy (holder only).
200  ObjectProxy* pyobj = (ObjectProxy*)subtype->tp_alloc( subtype, 0 );
201  pyobj->fObject = NULL;
202  pyobj->fFlags = 0;
203 
204  return pyobj;
205  }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// Remove (Python-side) memory held by the object proxy.
209 
210  void op_dealloc( ObjectProxy* pyobj )
211  {
212  op_dealloc_nofree( pyobj );
213  Py_TYPE(pyobj)->tp_free( (PyObject*)pyobj );
214  }
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 /// Rich set of comparison objects; only equals and not-equals are defined.
218 
219  PyObject* op_richcompare( ObjectProxy* self, ObjectProxy* other, int op )
220  {
221  if ( op != Py_EQ && op != Py_NE ) {
222  Py_INCREF( Py_NotImplemented );
223  return Py_NotImplemented;
224  }
225 
226  Bool_t bIsEq = false;
227 
228  // special case for None to compare True to a null-pointer
229  if ( (PyObject*)other == Py_None && ! self->fObject )
230  bIsEq = true;
231 
232  // type + held pointer value defines identity (will cover if other is not
233  // actually an ObjectProxy, as ob_type will be unequal)
234  else if ( Py_TYPE(self) == Py_TYPE(other) && self->GetObject() == other->GetObject() )
235  bIsEq = true;
236 
237  if ( ( op == Py_EQ && bIsEq ) || ( op == Py_NE && ! bIsEq ) ) {
238  Py_INCREF( Py_True );
239  return Py_True;
240  }
241 
242  Py_INCREF( Py_False );
243  return Py_False;
244  }
245 
246 ////////////////////////////////////////////////////////////////////////////////
247 /// Build a representation string of the object proxy that shows the address
248 /// of the C++ object that is held, as well as its type.
249 
250  PyObject* op_repr( ObjectProxy* pyobj )
251  {
252  Cppyy::TCppType_t klass = pyobj->ObjectIsA();
253  std::string clName = klass ? Cppyy::GetFinalName( klass ) : "<unknown>";
254  if ( pyobj->fFlags & ObjectProxy::kIsReference )
255  clName.append( "*" );
256 
257  std::string smartPtrName;
258  if ( pyobj->fFlags & ObjectProxy::kIsSmartPtr ) {
259  Cppyy::TCppType_t smartPtrType = pyobj->fSmartPtrType;
260  smartPtrName = smartPtrType ? Cppyy::GetFinalName( smartPtrType ) : "unknown smart pointer";
261  }
262 
263  // need to prevent accidental derefs when just printing (usually unsafe)
264  if ( ! PyObject_HasAttr( (PyObject*)pyobj, PyStrings::gDeref ) ) {
265  PyObject* name = PyObject_CallMethod( (PyObject*)pyobj,
266  const_cast< char* >( "GetName" ), const_cast< char* >( "" ) );
267 
268  if ( name ) {
269  if ( PyROOT_PyUnicode_GET_SIZE( name ) != 0 ) {
270  if ( pyobj->fFlags & ObjectProxy::kIsSmartPtr ) {
271  PyObject* repr = PyROOT_PyUnicode_FromFormat( "<ROOT.%s object (\"%s\") at %p held by %s at %p>",
272  clName.c_str(), PyROOT_PyUnicode_AsString( name ), pyobj->GetObject(), smartPtrName.c_str(), pyobj->fSmartPtr );
273  Py_DECREF( name );
274  return repr;
275  } else {
276  PyObject* repr = PyROOT_PyUnicode_FromFormat( "<ROOT.%s object (\"%s\") at %p>",
277  clName.c_str(), PyROOT_PyUnicode_AsString( name ), pyobj->GetObject() );
278  Py_DECREF( name );
279  return repr;
280  }
281  }
282  Py_DECREF( name );
283  } else
284  PyErr_Clear();
285  }
286 
287  // get here if object has no method GetName() or name = ""
288  if ( pyobj->fFlags & ObjectProxy::kIsSmartPtr ) {
289  return PyROOT_PyUnicode_FromFormat( const_cast< char* >( "<ROOT.%s object at %p held by %s at %p>" ),
290  clName.c_str(), pyobj->GetObject(), smartPtrName.c_str(), pyobj->fSmartPtr );
291  } else {
292  return PyROOT_PyUnicode_FromFormat( const_cast< char* >( "<ROOT.%s object at %p>" ),
293  clName.c_str(), pyobj->GetObject() );
294  }
295  }
296 
297 
298 //= PyROOT type number stubs to allow dynamic overrides ======================
299 #define PYROOT_STUB( name, op, pystring ) \
300  PyObject* op_##name##_stub( PyObject* left, PyObject* right ) \
301  { \
302  if ( ! ObjectProxy_Check( left ) ) { \
303  if ( ObjectProxy_Check( right ) ) { \
304  std::swap( left, right ); \
305  } else { \
306  Py_INCREF( Py_NotImplemented ); \
307  return Py_NotImplemented; \
308  } \
309  } \
310  /* place holder to lazily install __name__ if a global overload is available */ \
311  if ( ! Utility::AddBinaryOperator( \
312  left, right, #op, "__"#name"__", "__r"#name"__" ) ) { \
313  Py_INCREF( Py_NotImplemented ); \
314  return Py_NotImplemented; \
315  } \
316  \
317  /* redo the call, which will now go to the newly installed method */ \
318  return PyObject_CallMethodObjArgs( left, pystring, right, NULL ); \
319  }
320 
321 PYROOT_STUB( add, +, PyStrings::gAdd )
322 PYROOT_STUB( sub, -, PyStrings::gSub )
323 PYROOT_STUB( mul, *, PyStrings::gMul )
324 PYROOT_STUB( div, /, PyStrings::gDiv )
325 
326 ////////////////////////////////////////////////////////////////////////////////
327 
328  PyNumberMethods op_as_number = {
329  (binaryfunc)op_add_stub, // nb_add
330  (binaryfunc)op_sub_stub, // nb_subtract
331  (binaryfunc)op_mul_stub, // nb_multiply
332 #if PY_VERSION_HEX < 0x03000000
333  (binaryfunc)op_div_stub, // nb_divide
334 #endif
335  0, // nb_remainder
336  0, // nb_divmod
337  0, // nb_power
338  0, // nb_negative
339  0, // nb_positive
340  0, // nb_absolute
341  0, // tp_nonzero (nb_bool in p3)
342  0, // nb_invert
343  0, // nb_lshift
344  0, // nb_rshift
345  0, // nb_and
346  0, // nb_xor
347  0, // nb_or
348 #if PY_VERSION_HEX < 0x03000000
349  0, // nb_coerce
350 #endif
351  0, // nb_int
352  0, // nb_long (nb_reserved in p3)
353  0, // nb_float
354 #if PY_VERSION_HEX < 0x03000000
355  0, // nb_oct
356  0, // nb_hex
357 #endif
358  0, // nb_inplace_add
359  0, // nb_inplace_subtract
360  0, // nb_inplace_multiply
361 #if PY_VERSION_HEX < 0x03000000
362  0, // nb_inplace_divide
363 #endif
364  0, // nb_inplace_remainder
365  0, // nb_inplace_power
366  0, // nb_inplace_lshift
367  0, // nb_inplace_rshift
368  0, // nb_inplace_and
369  0, // nb_inplace_xor
370  0 // nb_inplace_or
371 #if PY_VERSION_HEX >= 0x02020000
372  , 0 // nb_floor_divide
373 #if PY_VERSION_HEX < 0x03000000
374  , 0 // nb_true_divide
375 #else
376  , (binaryfunc)op_div_stub // nb_true_divide
377 #endif
378  , 0 // nb_inplace_floor_divide
379  , 0 // nb_inplace_true_divide
380 #endif
381 #if PY_VERSION_HEX >= 0x02050000
382  , 0 // nb_index
383 #endif
384  };
385 
386 } // unnamed namespace
387 
388 
389 //= PyROOT object proxy type =================================================
390 PyTypeObject ObjectProxy_Type = {
392  (char*)"ROOT.ObjectProxy", // tp_name
393  sizeof(ObjectProxy), // tp_basicsize
394  0, // tp_itemsize
395  (destructor)op_dealloc, // tp_dealloc
396  0, // tp_print
397  0, // tp_getattr
398  0, // tp_setattr
399  0, // tp_compare
400  (reprfunc)op_repr, // tp_repr
401  &op_as_number, // tp_as_number
402  0, // tp_as_sequence
403  0, // tp_as_mapping
404  PyBaseObject_Type.tp_hash, // tp_hash
405  0, // tp_call
406  0, // tp_str
407  0, // tp_getattro
408  0, // tp_setattro
409  0, // tp_as_buffer
410  Py_TPFLAGS_DEFAULT |
411  Py_TPFLAGS_BASETYPE |
412  Py_TPFLAGS_HAVE_GC |
413  Py_TPFLAGS_CHECKTYPES, // tp_flags
414  (char*)"PyROOT object proxy (internal)", // tp_doc
415  0, // tp_traverse
416  0, // tp_clear
417  (richcmpfunc)op_richcompare, // tp_richcompare
418  0, // tp_weaklistoffset
419  0, // tp_iter
420  0, // tp_iternext
421  op_methods, // tp_methods
422  0, // tp_members
423  0, // tp_getset
424  0, // tp_base
425  0, // tp_dict
426  0, // tp_descr_get
427  0, // tp_descr_set
428  0, // tp_dictoffset
429  0, // tp_init
430  0, // tp_alloc
431  (newfunc)op_new, // tp_new
432  0, // tp_free
433  0, // tp_is_gc
434  0, // tp_bases
435  0, // tp_mro
436  0, // tp_cache
437  0, // tp_subclasses
438  0 // tp_weaklist
439 #if PY_VERSION_HEX >= 0x02030000
440  , 0 // tp_del
441 #endif
442 #if PY_VERSION_HEX >= 0x02060000
443  , 0 // tp_version_tag
444 #endif
445 #if PY_VERSION_HEX >= 0x03040000
446  , 0 // tp_finalize
447 #endif
448 };
449 
450 } // namespace PyROOT
#define PyBytes_FromString
Definition: PyROOT.h:59
TCppScope_t TCppType_t
Definition: Cppyy.h:13
The concrete implementation of TBuffer for writing/reading to/from a ROOT file or socket...
Definition: TBufferFile.h:51
#define PyROOT_PyUnicode_GET_SIZE
Definition: PyROOT.h:68
Cppyy::TCppType_t fSmartPtrType
Definition: ObjectProxy.h:80
R__EXTERN PyObject * gDiv
Definition: PyStrings.h:41
#define gROOT
Definition: TROOT.h:364
std::string GetFinalName(TCppType_t type)
Definition: Cppyy.cxx:556
bool Bool_t
Definition: RtypesCore.h:59
R__EXTERN PyObject * gDeref
Definition: PyStrings.h:21
#define PyVarObject_HEAD_INIT(type, size)
Definition: PyROOT.h:149
Int_t Length() const
Definition: TBuffer.h:96
R__EXTERN PyObject * gAdd
Definition: PyStrings.h:38
#define PYROOT_STUB(name, op, pystring)
#define PyROOT_PyUnicode_FromFormat
Definition: PyROOT.h:70
R__EXTERN PyObject * gRootModule
Definition: ObjectProxy.cxx:39
char * Buffer() const
Definition: TBuffer.h:93
#define PyROOT_PyUnicode_Type
Definition: PyROOT.h:77
#define PyROOT_PyUnicode_AsString
Definition: PyROOT.h:66
void CallDestructor(TCppType_t type, TCppObject_t self)
Definition: Cppyy.cxx:487
PyTypeObject PyRootType_Type
Definition: PyRootType.cxx:191
PyTypeObject ObjectProxy_Type
if object ctor succeeded but object should not be used
Definition: TObject.h:63
TCppScope_t GetScope(const std::string &scope_name)
Definition: Cppyy.cxx:171
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition: TClass.cxx:2893
void Deallocate(TCppType_t type, TCppObject_t instance)
Definition: Cppyy.cxx:255
R__EXTERN PyObject * gSub
Definition: PyStrings.h:39
PyObject_HEAD void * fObject
Definition: ObjectProxy.h:77
#define R__EXTERN
Definition: DllImport.h:27
#define Py_TYPE(ob)
Definition: PyROOT.h:151
void * GetObject() const
Definition: ObjectProxy.h:47
void op_dealloc_nofree(ObjectProxy *)
Destroy the held C++ object, if owned; does not deallocate the proxy.
Definition: ObjectProxy.cxx:46
#define NULL
Definition: Rtypes.h:82
virtual Int_t WriteObjectAny(const void *obj, const TClass *ptrClass)
Write object to I/O buffer.
void Reset()
Definition: TBufferFile.h:98
double result[121]
void Destruct(TCppType_t type, TCppObject_t instance)
Definition: Cppyy.cxx:266
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)
char name[80]
Definition: TGX11.cxx:109
_object PyObject
Definition: TPyArg.h:22
R__EXTERN PyObject * gMul
Definition: PyStrings.h:40
Cppyy::TCppType_t ObjectIsA() const
Definition: ObjectProxy.h:66
#define PyBytes_FromStringAndSize
Definition: PyROOT.h:60