11 #if PY_VERSION_HEX >= 0x03000000 12 static PyObject* PyBuffer_FromReadWriteMemory(
void* ptr,
int size ) {
13 #if PY_VERSION_HEX > 0x03000000 21 Py_buffer bufinfo = { ptr,
NULL, size, 1, 0, 1,
NULL,
NULL,
NULL,
NULL,
22 #if PY_VERSION_HEX < 0x03030000 26 return PyMemoryView_FromBuffer( &bufinfo );
35 struct PyBufferTop_t {
43 std::map< PyObject*, PyObject* > gSizeCallbacks;
46 #define PYROOT_PREPARE_PYBUFFER_TYPE( name ) \ 47 PyTypeObject Py##name##Buffer_Type; \ 48 PySequenceMethods Py##name##Buffer_SeqMethods = *(PyBuffer_Type.tp_as_sequence);\ 49 PyMappingMethods Py##name##Buffer_MapMethods; 65 #if PY_VERSION_HEX < 0x03000000 66 Py_ssize_t nlen = (*(PyBuffer_Type.tp_as_sequence->sq_length))(
self);
69 (*(
Py_TYPE(
self)->tp_as_buffer->bf_getbuffer))(
self, &bufinfo, PyBUF_SIMPLE );
72 if ( nlen != INT_MAX )
75 std::map< PyObject*, PyObject* >::iterator iscbp = gSizeCallbacks.find(
self );
76 if ( iscbp != gSizeCallbacks.end() ) {
77 PyObject* pylen = PyObject_CallObject( iscbp->second,
NULL );
81 if ( nlen2 == (
Py_ssize_t)-1 && PyErr_Occurred() )
93 const char* buffer_get(
PyObject*
self,
int idx )
95 if ( idx < 0 || idx >= buffer_length(
self ) ) {
96 PyErr_SetString( PyExc_IndexError,
"buffer index out of range" );
100 #if PY_VERSION_HEX < 0x02050000 105 #if PY_VERSION_HEX < 0x03000000 106 (*(PyBuffer_Type.tp_as_buffer->bf_getcharbuffer))(
self, 0, &buf );
109 (*(PyBuffer_Type.tp_as_buffer->bf_getbuffer))(
self, &bufinfo, PyBUF_SIMPLE );
110 buf = (
char*)bufinfo.buf;
114 PyErr_SetString( PyExc_IndexError,
"attempt to index a null-buffer" );
121 #define PYROOT_IMPLEMENT_PYBUFFER_METHODS( name, type, stype, F1, F2 ) \ 122 PyObject* name##_buffer_str( PyObject* self ) \ 124 Py_ssize_t l = buffer_length( self ); \ 125 return PyROOT_PyUnicode_FromFormat( "<"#type" buffer, size " PY_SSIZE_T_FORMAT ">", l );\ 128 PyObject* name##_buffer_item( PyObject* self, Py_ssize_t idx ) { \ 129 const char* buf = buffer_get( self, idx ); \ 131 return F1( (stype)*((type*)buf + idx) ); \ 135 int name##_buffer_ass_item( PyObject* self, Py_ssize_t idx, PyObject* val ) {\ 136 const char* buf = buffer_get( self, idx ); \ 140 type value = F2( val ); \ 141 if ( value == (type)-1 && PyErr_Occurred() ) \ 144 *((type*)buf+idx) = (type)value; \ 148 PyObject* name##_buffer_subscript( PyObject* self, PyObject* item ) { \ 149 if ( PyIndex_Check( item ) ) { \ 150 Py_ssize_t idx = PyNumber_AsSsize_t( item, PyExc_IndexError ); \ 151 if ( idx == -1 && PyErr_Occurred() ) \ 153 return name##_buffer_item( self, idx ); \ 172 if ( i == -1 && PyErr_Occurred() )
174 return Py_TYPE(
self)->tp_as_sequence->sq_ass_item(
self, i, val );
176 PyErr_SetString( PyExc_TypeError,
"buffer indices must be integers" );
188 if ( nlen == -1 && PyErr_Occurred() )
191 #if PY_VERSION_HEX < 0x03000000 192 ((PyBufferTop_t*)
self)->fSize = nlen;
194 PyMemoryView_GET_BUFFER(
self)->len = nlen;
197 Py_INCREF( Py_None );
206 if ( PyObject_TypeCheck( pyobject, &PyBoolBuffer_Type ) )
208 else if ( PyObject_TypeCheck( pyobject, &PyShortBuffer_Type ) )
210 else if ( PyObject_TypeCheck( pyobject, &PyUShortBuffer_Type ) )
212 else if ( PyObject_TypeCheck( pyobject, &PyIntBuffer_Type ) )
214 else if ( PyObject_TypeCheck( pyobject, &PyUIntBuffer_Type ) )
216 else if ( PyObject_TypeCheck( pyobject, &PyLongBuffer_Type ) )
218 else if ( PyObject_TypeCheck( pyobject, &PyULongBuffer_Type ) )
220 else if ( PyObject_TypeCheck( pyobject, &PyFloatBuffer_Type ) )
222 else if ( PyObject_TypeCheck( pyobject, &PyDoubleBuffer_Type ) )
225 PyErr_SetString( PyExc_TypeError,
"received unknown buffer object" );
231 PyGetSetDef buffer_getset[] = {
232 { (
char*)
"typecode", (getter)buf_typecode,
NULL,
NULL, NULL },
233 { (
char*)NULL, NULL, NULL, NULL, NULL }
238 PyMethodDef buffer_methods[] = {
239 { (
char*)
"SetSize", (PyCFunction)buffer_setsize, METH_O,
NULL },
240 { (
char*)NULL, NULL, 0, NULL }
256 #define PYROOT_INSTALL_PYBUFFER_METHODS( name, type ) \ 257 Py##name##Buffer_Type.tp_name = (char*)"ROOT.Py"#name"Buffer"; \ 258 Py##name##Buffer_Type.tp_base = &PyBuffer_Type; \ 259 Py##name##Buffer_Type.tp_as_buffer = PyBuffer_Type.tp_as_buffer; \ 260 Py##name##Buffer_SeqMethods.sq_item = (ssizeargfunc)name##_buffer_item; \ 261 Py##name##Buffer_SeqMethods.sq_ass_item = (ssizeobjargproc)name##_buffer_ass_item;\ 262 Py##name##Buffer_SeqMethods.sq_length = (lenfunc)buffer_length; \ 263 Py##name##Buffer_Type.tp_as_sequence = &Py##name##Buffer_SeqMethods; \ 264 if ( PyBuffer_Type.tp_as_mapping ) { \ 265 Py##name##Buffer_MapMethods.mp_length = (lenfunc)buffer_length; \ 266 Py##name##Buffer_MapMethods.mp_subscript = (binaryfunc)name##_buffer_subscript;\ 267 Py##name##Buffer_MapMethods.mp_ass_subscript = (objobjargproc)pyroot_buffer_ass_subscript;\ 268 Py##name##Buffer_Type.tp_as_mapping = &Py##name##Buffer_MapMethods; \ 270 Py##name##Buffer_Type.tp_str = (reprfunc)name##_buffer_str; \ 271 Py##name##Buffer_Type.tp_methods = buffer_methods; \ 272 Py##name##Buffer_Type.tp_getset = buffer_getset; \ 273 PyType_Ready( &Py##name##Buffer_Type ); 297 #define PYROOT_IMPLEMENT_PYBUFFER_FROM_MEMORY( name, type ) \ 298 PyObject* PyROOT::TPyBufferFactory::PyBuffer_FromMemory( type* address, Py_ssize_t size )\ 300 size = size < 0 ? INT_MAX : size; \ 301 PyObject* buf = PyBuffer_FromReadWriteMemory( (void*)address, size ); \ 303 Py_INCREF( (PyObject*)(void*)&Py##name##Buffer_Type ); \ 304 buf->ob_type = &Py##name##Buffer_Type; \ 309 PyObject* PyROOT::TPyBufferFactory::PyBuffer_FromMemory( type* address, PyObject* scb )\ 311 PyObject* buf = PyBuffer_FromMemory( address, Py_ssize_t(0) ); \ 312 if ( buf != 0 && PyCallable_Check( scb ) ) { \ 314 gSizeCallbacks[ buf ] = scb; \ #define PyROOT_PyUnicode_FromString
Py_ssize_t PyNumber_AsSsize_t(PyObject *obj, PyObject *)
#define PYROOT_INSTALL_PYBUFFER_METHODS(name, type)
static RooMathCoreReg dummy
Factory for python buffers of non-string type.
#define PYROOT_IMPLEMENT_PYBUFFER_FROM_MEMORY(name, type)
static TPyBufferFactory * Instance()
#define PyIndex_Check(obj)
#define PYROOT_PREPARE_PYBUFFER_TYPE(name)
#define PYROOT_IMPLEMENT_PYBUFFER_METHODS(name, type, stype, F1, F2)