11 #pragma warning(disable:2259) 16 #include "TClassRef.h" 17 #include "TBufferFile.h" 22 #if ROOT_VERSION_CODE < ROOT_VERSION(5,19,0) 23 static PyObject* gExpand = 0;
28 #if ROOT_VERSION_CODE < ROOT_VERSION(5,19,0) 34 PyObject* ObjectProxyReduce( PyObject*
self )
40 void* vself = TPython::ObjectProxy_AsVoidPtr(
self );
42 PyErr_SetString( PyExc_TypeError,
43 "__reduce__ requires an object proxy instance as first argument" );
47 PyObject* nattr = PyObject_GetAttrString( (PyObject*)self->ob_type, (
char*)
"__name__" );
48 PyObject* pyname = PyObject_Str( nattr );
51 static TClass* bufferclass = TClass::GetClass(
"TBufferFile");
52 TClass* klass = TClass::GetClass( PyString_AS_STRING( pyname ) );
57 if ( klass == bufferclass ) {
58 buf = (TBufferFile*)vself;
61 static TBufferFile buffer( TBuffer::kWrite );
63 if ( buffer.WriteObjectAny( vself, klass ) != 1 ) {
64 PyErr_Format( PyExc_IOError,
65 "could not stream object of type %s", PyString_AS_STRING( pyname ) );
75 PyObject* res2 = PyTuple_New( 2 );
76 PyTuple_SET_ITEM( res2, 0, PyString_FromStringAndSize( buf->Buffer(), buf->Length() ) );
77 PyTuple_SET_ITEM( res2, 1, pyname );
79 PyObject* result = PyTuple_New( 2 );
81 PyTuple_SET_ITEM( result, 0, gExpand );
82 PyTuple_SET_ITEM( result, 1, res2 );
90 enum EFlags { kNone = 0x0, kIsOwner = 0x0001, kIsReference = 0x0002 };
93 void HoldOn() { fFlags |= kIsOwner; }
94 void Release() { fFlags &= ~kIsOwner; }
109 PyObject* ObjectProxyExpand( PyObject*, PyObject*
args )
113 const char* clname = 0;
114 if ( ! PyArg_ParseTuple( args, const_cast< char* >(
"O!s:__expand__" ),
115 &PyString_Type, &pybuf, &clname ) )
121 if(
strcmp(clname,
"TBufferFile") == 0) {
122 TBufferFile* buf =
new TBufferFile( TBuffer::kWrite);
123 buf->WriteFastArray( PyString_AS_STRING(pybuf), PyString_GET_SIZE( pybuf ));
127 TBufferFile buf( TBuffer::kRead,
128 PyString_GET_SIZE( pybuf ), PyString_AS_STRING( pybuf ), kFALSE );
129 result = buf.ReadObjectAny( 0 );
131 PyObject* pobj = TPython::ObjectProxy_FromVoidPtr( result, clname );
133 ObjectProxy* obj = (ObjectProxy*)pobj;
146 Py_INCREF( libpyroot_pymodule );
147 PyTypeObject* pytype = (PyTypeObject*)objectproxy_pytype;
149 static PyMethodDef s_pdefExp = { (
char*)
"_ObjectProxy__expand__",
150 (PyCFunction)ObjectProxyExpand, METH_VARARGS, (
char*)
"internal function" };
152 PyObject* pymname = PyString_FromString( PyModule_GetName( libpyroot_pymodule ) );
153 gExpand = PyCFunction_NewEx( &s_pdefExp, NULL, pymname );
154 Py_DECREF( pymname );
155 Bool_t isOk = PyObject_SetAttrString( libpyroot_pymodule, s_pdefExp.ml_name, gExpand ) == 0;
156 Py_DECREF( gExpand );
159 Py_DECREF( libpyroot_pymodule );
160 PyErr_SetString( PyExc_TypeError,
"could not add expand function to libPyROOT" );
164 static PyMethodDef s_pdefRed = { (
char*)
"__reduce__",
165 (PyCFunction)ObjectProxyReduce, METH_NOARGS, (
char*)
"internal function" };
167 PyObject* descr = PyDescr_NewMethod( pytype, &s_pdefRed );
168 isOk = PyDict_SetItemString( pytype->tp_dict, s_pdefRed.ml_name, descr) == 0;
171 Py_DECREF( libpyroot_pymodule );
172 PyErr_SetString( PyExc_TypeError,
"could not add __reduce__ function to ObjectProxy" );
176 Py_DECREF( libpyroot_pymodule );
179 #else // ROOT_VERSION_CODE < ROOT_VERSION(5,19,0) 186 #endif // ROOT_VERSION_CODE < ROOT_VERSION(5,19,0)
A class to serialize/deserialize TES objects to and from a TBufferFile Author: P. ...
static void Initialize(PyObject *libpyroot_pymodule, PyObject *objectproxy_pytype)
Install the pickling of ObjectProxy's functionality.
Port pickling functionality while awaiting newer release.