00001
00008 #ifdef __ICC
00009
00010
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
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
00037
00038
00039
00040 void* vself = TPython::ObjectProxy_AsVoidPtr( self );
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
00055
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
00073
00074
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:
00096 PyObject_HEAD
00097 void* fObject;
00098 TClassRef fClass;
00099 int fFlags;
00100 private:
00101 ObjectProxy() {}
00102 };
00103
00104
00109 PyObject* ObjectProxyExpand( PyObject*, PyObject* args )
00110 {
00111
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
00119
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
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 );
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
00184 }
00185
00186 #endif // ROOT_VERSION_CODE < ROOT_VERSION(5,19,0)
00187
00188 }