Gaudi Framework, version v23r4

Home   Generated: Mon Sep 17 2012

PyROOTPickle.cpp

Go to the documentation of this file.
00001 
00008 #ifdef __ICC
00009 // disable icc remark #2259: non-pointer conversion from "X" to "Y" may lose significant bits
00010 //   TODO: To be removed, since it comes from ROOT
00011 #pragma warning(disable:2259)
00012 #endif
00013 
00014 #include "GaudiMP/PyROOTPickle.h"
00015 #include "TClass.h"
00016 #include "TClassRef.h"
00017 #include "TBufferFile.h"
00018 #include "TPython.h"
00019 #include "RVersion.h"
00020 
00021 //- data _______________________________________________________________________
00022 #if ROOT_VERSION_CODE < ROOT_VERSION(5,19,0)
00023 static PyObject* gExpand = 0;
00024 #endif
00025 
00026 namespace GaudiMP {
00027 
00028 #if ROOT_VERSION_CODE < ROOT_VERSION(5,19,0)
00029 
00034 PyObject* ObjectProxyReduce( PyObject* self )
00035 {
00036   // Turn the object proxy instance into a character stream and return for
00037   // pickle, together with the callable object that can restore the stream
00038   // into the object proxy instance.
00039 
00040   void* vself = TPython::ObjectProxy_AsVoidPtr( self );    // checks type
00041   if ( ! vself ) {
00042      PyErr_SetString( PyExc_TypeError,
00043        "__reduce__ requires an object proxy instance as first argument" );
00044      return 0;
00045   }
00046 
00047   PyObject* nattr = PyObject_GetAttrString( (PyObject*)self->ob_type, (char*)"__name__" );
00048   PyObject* pyname = PyObject_Str( nattr );
00049   Py_DECREF( nattr );
00050 
00051   static TClass* bufferclass =  TClass::GetClass("TBufferFile");
00052   TClass* klass = TClass::GetClass( PyString_AS_STRING( pyname ) );
00053 
00054   // no cast is needed, but WriteObject taking a TClass argument is protected,
00055   // so use WriteObjectAny()
00056   TBufferFile* buf = 0;
00057   if ( klass == bufferclass ) {
00058      buf = (TBufferFile*)vself;
00059   }
00060   else {
00061      static TBufferFile buffer( TBuffer::kWrite );
00062      buffer.Reset();
00063      if ( buffer.WriteObjectAny( vself, klass ) != 1 ) {
00064         PyErr_Format( PyExc_IOError,
00065            "could not stream object of type %s", PyString_AS_STRING( pyname ) );
00066         Py_DECREF( pyname );
00067         return 0;
00068      }
00069      buf = &buffer;
00070   }
00071 
00072   // use a string for the serialized result, as a python buffer will not copy
00073   // the buffer contents; use a string for the class name, used when casting
00074   // on reading back in
00075   PyObject* res2 = PyTuple_New( 2 );
00076   PyTuple_SET_ITEM( res2, 0, PyString_FromStringAndSize( buf->Buffer(), buf->Length() ) );
00077   PyTuple_SET_ITEM( res2, 1, pyname );
00078 
00079   PyObject* result = PyTuple_New( 2 );
00080   Py_INCREF( gExpand );
00081   PyTuple_SET_ITEM( result, 0, gExpand );
00082   PyTuple_SET_ITEM( result, 1, res2 );
00083 
00084   return result;
00085 }
00086 
00087 
00088 class ObjectProxy {
00089    public:
00090       enum EFlags { kNone = 0x0, kIsOwner = 0x0001, kIsReference = 0x0002 };
00091 
00092    public:
00093       void HoldOn() { fFlags |= kIsOwner; }
00094       void Release() { fFlags &= ~kIsOwner; }
00095    public:               // public, as the python C-API works with C structs
00096       PyObject_HEAD
00097       void*     fObject;
00098       TClassRef fClass;
00099       int       fFlags;
00100    private:              // private, as the python C-API will handle creation
00101       ObjectProxy() {}
00102 };
00103 
00104 
00109 PyObject* ObjectProxyExpand( PyObject*, PyObject* args )
00110 {
00111   // This method is a helper for (un)pickling of ObjectProxy instances.
00112   PyObject* pybuf = 0;
00113   const char* clname = 0;
00114   if ( ! PyArg_ParseTuple( args, const_cast< char* >( "O!s:__expand__" ),
00115            &PyString_Type, &pybuf, &clname ) )
00116     return 0;
00117 
00118   // use the PyString macro's to by-pass error checking; do not adopt the buffer,
00119   // as the local TBufferFile can go out of scope (there is no copying)
00120   void* result;
00121   if( strcmp(clname, "TBufferFile") == 0) {
00122     TBufferFile* buf = new TBufferFile( TBuffer::kWrite);
00123     buf->WriteFastArray( PyString_AS_STRING(pybuf), PyString_GET_SIZE( pybuf ));
00124     result = buf;
00125   }
00126   else {
00127     TBufferFile buf( TBuffer::kRead,
00128        PyString_GET_SIZE( pybuf ), PyString_AS_STRING( pybuf ), kFALSE );
00129     result = buf.ReadObjectAny( 0 );
00130   }
00131   PyObject* pobj =  TPython::ObjectProxy_FromVoidPtr( result, clname );
00132   // set Ownership of the returned object
00133   ObjectProxy* obj = (ObjectProxy*)pobj;
00134   obj->HoldOn();
00135   return pobj;
00136 }
00137 
00138 
00144 void PyROOTPickle::Initialize( PyObject* libpyroot_pymodule, PyObject* objectproxy_pytype )
00145 {
00146   Py_INCREF( libpyroot_pymodule );
00147   PyTypeObject* pytype = (PyTypeObject*)objectproxy_pytype;
00148 
00149   static PyMethodDef s_pdefExp = { (char*)"_ObjectProxy__expand__",
00150             (PyCFunction)ObjectProxyExpand, METH_VARARGS, (char*)"internal function" };
00151 
00152   PyObject* pymname = PyString_FromString( PyModule_GetName( libpyroot_pymodule ) );
00153   gExpand = PyCFunction_NewEx( &s_pdefExp, NULL, pymname );
00154   Py_DECREF( pymname );
00155   Bool_t isOk = PyObject_SetAttrString( libpyroot_pymodule, s_pdefExp.ml_name, gExpand ) == 0;
00156   Py_DECREF( gExpand );      // is moderately risky, but Weakref not allowed (?)
00157 
00158   if ( ! isOk ) {
00159     Py_DECREF( libpyroot_pymodule );
00160     PyErr_SetString( PyExc_TypeError, "could not add expand function to libPyROOT" );
00161     return;
00162   }
00163 
00164   static PyMethodDef s_pdefRed = { (char*)"__reduce__",
00165             (PyCFunction)ObjectProxyReduce, METH_NOARGS, (char*)"internal function" };
00166 
00167   PyObject* descr = PyDescr_NewMethod( pytype, &s_pdefRed );
00168   isOk = PyDict_SetItemString( pytype->tp_dict, s_pdefRed.ml_name, descr) == 0;
00169   Py_DECREF( descr );
00170   if ( ! isOk ) {
00171     Py_DECREF( libpyroot_pymodule );
00172     PyErr_SetString( PyExc_TypeError, "could not add __reduce__ function to ObjectProxy" );
00173     return;
00174   }
00175 
00176   Py_DECREF( libpyroot_pymodule );
00177 }
00178 
00179 #else //  ROOT_VERSION_CODE < ROOT_VERSION(5,19,0)
00180 
00181 void PyROOTPickle::Initialize( PyObject*, PyObject* )
00182 {
00183   /* dummy. It is not needed for this version of ROOT */
00184 }
00185 
00186 #endif //  ROOT_VERSION_CODE < ROOT_VERSION(5,19,0)
00187 
00188 } // namespace GaudiMP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Mon Sep 17 2012 13:49:35 for Gaudi Framework, version v23r4 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004