22 #include "TBufferFile.h"
42 void* vself = TPython::CPPInstance_AsVoidPtr(
self );
44 PyErr_SetString( PyExc_TypeError,
45 "__reduce__ requires an object proxy instance as first argument" );
49 PyObject* nattr = PyObject_GetAttrString( (
PyObject*)self->ob_type, (
char*)
"__name__" );
50 PyObject* pyname = PyObject_Str( nattr );
53 std::string klassname =
PyGetString( pyname ).first;
54 TClass*
klass = TClass::GetClass( klassname.c_str() );
58 TBufferFile buf( TBuffer::kWrite );
59 if ( buf.WriteObjectAny( vself,
klass ) != 1 ) {
60 PyErr_Format( PyExc_IOError,
61 "could not stream object of type %s", klassname.c_str() );
70 PyTuple_SET_ITEM( res2, 0, PyUnicode_FromStringAndSize( buf.Buffer(), buf.Length() ) );
71 PyTuple_SET_ITEM( res2, 1, pyname );
75 PyTuple_SET_ITEM(
result, 0, gExpand );
76 PyTuple_SET_ITEM(
result, 1, res2 );
90 const char* clname = 0;
91 if ( ! PyArg_ParseTuple(
args,
const_cast< char*
>(
"O!s:__expand__" ),
92 &PyUnicode_Type, &pybuf, &clname ) )
97 TBufferFile buf( TBuffer::kRead,
98 PyUnicode_GET_LENGTH( pybuf ),
102 void*
result = buf.ReadObjectAny( 0 );
103 return TPython::CPPInstance_FromVoidPtr(
result, clname );
114 Py_INCREF( libpyroot_pymodule );
115 PyTypeObject* pytype = (PyTypeObject*)cppinstance_pytype;
118 static PyMethodDef s_pdefExp = { (
char*)
"_ObjectProxy__expand__",
121 PyObject* pymname = PyUnicode_FromString( PyModule_GetName( libpyroot_pymodule ) );
122 gExpand = PyCFunction_NewEx( &s_pdefExp, NULL, pymname );
123 Py_DECREF( pymname );
124 Bool_t isOk = PyObject_SetAttrString( libpyroot_pymodule, s_pdefExp.ml_name, gExpand ) == 0;
125 Py_DECREF( gExpand );
128 Py_DECREF( libpyroot_pymodule );
129 PyErr_SetString( PyExc_TypeError,
"could not add expand function to libPyROOT" );
133 static PyMethodDef s_pdefRed = { (
char*)
"__reduce__",
137 isOk = PyDict_SetItemString( pytype->tp_dict, s_pdefRed.ml_name,
descr) == 0;
140 Py_DECREF( libpyroot_pymodule );
141 PyErr_SetString( PyExc_TypeError,
"could not add __reduce__ function to CPPInstance" );
145 Py_DECREF( libpyroot_pymodule );