21 # pragma warning( disable : 2259 )
26 #include <TBufferFile.h>
28 #include <TClassRef.h>
32 #if ROOT_VERSION_CODE < ROOT_VERSION( 5, 19, 0 )
38 #if ROOT_VERSION_CODE < ROOT_VERSION( 5, 19, 0 )
49 void* vself = TPython::ObjectProxy_AsVoidPtr(
self );
51 PyErr_SetString( PyExc_TypeError,
"__reduce__ requires an object proxy instance as first argument" );
56 PyObject* pyname = PyObject_Str( nattr );
59 static TClass* bufferclass = TClass::GetClass(
"TBufferFile" );
60 TClass* klass = TClass::GetClass( PyString_AS_STRING( pyname ) );
65 if ( klass == bufferclass ) {
66 buf = (TBufferFile*)vself;
68 static TBufferFile buffer( TBuffer::kWrite );
70 if ( buffer.WriteObjectAny( vself, klass ) != 1 ) {
71 PyErr_Format( PyExc_IOError,
"could not stream object of type %s", PyString_AS_STRING( pyname ) );
82 PyTuple_SET_ITEM( res2, 0, PyString_FromStringAndSize( buf->Buffer(), buf->Length() ) );
83 PyTuple_SET_ITEM( res2, 1, pyname );
87 PyTuple_SET_ITEM( result, 0, gExpand );
88 PyTuple_SET_ITEM( result, 1, res2 );
95 enum EFlags { kNone = 0x0, kIsOwner = 0x0001, kIsReference = 0x0002 };
98 void HoldOn() { fFlags |= kIsOwner; }
99 void Release() { fFlags &= ~kIsOwner; }
102 PyObject_HEAD
void* fObject;
117 const char* clname = 0;
118 if ( !PyArg_ParseTuple(
args,
const_cast<char*
>(
"O!s:__expand__" ), &PyString_Type, &pybuf, &clname ) )
return 0;
123 if (
strcmp( clname,
"TBufferFile" ) == 0 ) {
124 TBufferFile* buf =
new TBufferFile( TBuffer::kWrite );
125 buf->WriteFastArray( PyString_AS_STRING( pybuf ), PyString_GET_SIZE( pybuf ) );
128 TBufferFile buf( TBuffer::kRead, 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;
144 Py_INCREF( libpyroot_pymodule );
145 PyTypeObject* pytype = (PyTypeObject*)objectproxy_pytype;
147 static PyMethodDef s_pdefExp = { (
char*)
"_ObjectProxy__expand__", (PyCFunction)ObjectProxyExpand, METH_VARARGS,
148 (
char*)
"internal function" };
150 PyObject* pymname = PyString_FromString( PyModule_GetName( libpyroot_pymodule ) );
151 gExpand = PyCFunction_NewEx( &s_pdefExp, NULL, pymname );
152 Py_DECREF( pymname );
153 Bool_t isOk = PyObject_SetAttrString( libpyroot_pymodule, s_pdefExp.ml_name, gExpand ) == 0;
154 Py_DECREF( gExpand );
157 Py_DECREF( libpyroot_pymodule );
158 PyErr_SetString( PyExc_TypeError,
"could not add expand function to libPyROOT" );
162 static PyMethodDef s_pdefRed = { (
char*)
"__reduce__", (PyCFunction)ObjectProxyReduce, METH_NOARGS,
163 (
char*)
"internal function" };
165 PyObject* descr = PyDescr_NewMethod( pytype, &s_pdefRed );
166 isOk = PyDict_SetItemString( pytype->tp_dict, s_pdefRed.ml_name, descr ) == 0;
169 Py_DECREF( libpyroot_pymodule );
170 PyErr_SetString( PyExc_TypeError,
"could not add __reduce__ function to ObjectProxy" );
174 Py_DECREF( libpyroot_pymodule );
177 #else // ROOT_VERSION_CODE < ROOT_VERSION(5,19,0)
182 #endif // ROOT_VERSION_CODE < ROOT_VERSION(5,19,0)