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 {
44 std::map< PyObject*, PyObject* > gSizeCallbacks;
47 #define PYROOT_PREPARE_PYBUFFER_TYPE( name ) \ 48 PyTypeObject Py##name##Buffer_Type; \ 49 PySequenceMethods Py##name##Buffer_SeqMethods = *(PyBuffer_Type.tp_as_sequence);\ 50 PyMappingMethods Py##name##Buffer_MapMethods; 68 #if PY_VERSION_HEX < 0x03000000 69 Py_ssize_t nlen = ((PyBufferTop_t*)
self)->fSize;
70 Py_ssize_t item = ((PyBufferTop_t*)
self)->fItemSize;
72 Py_buffer* bufinfo = PyMemoryView_GET_BUFFER(
self);
76 if ( nlen != INT_MAX )
79 std::map< PyObject*, PyObject* >::iterator iscbp = gSizeCallbacks.find(
self );
80 if ( iscbp != gSizeCallbacks.end() ) {
81 PyObject* pylen = PyObject_CallObject( iscbp->second, NULL );
85 if ( nlen2 == (
Py_ssize_t)-1 && PyErr_Occurred() )
97 const char* buffer_get(
PyObject*
self,
int idx )
99 if ( idx < 0 || idx >= buffer_length(
self ) ) {
100 PyErr_SetString( PyExc_IndexError,
"buffer index out of range" );
104 #if PY_VERSION_HEX < 0x02050000 109 #if PY_VERSION_HEX < 0x03000000 110 (*(PyBuffer_Type.tp_as_buffer->bf_getcharbuffer))(
self, 0, &buf );
113 (*(PyBuffer_Type.tp_as_buffer->bf_getbuffer))(
self, &bufinfo, PyBUF_SIMPLE );
114 buf = (
char*)bufinfo.buf;
115 (*(PyBuffer_Type.tp_as_buffer->bf_releasebuffer))(
self, &bufinfo);
116 Py_DECREF(bufinfo.obj);
120 PyErr_SetString( PyExc_IndexError,
"attempt to index a null-buffer" );
127 #define PYROOT_IMPLEMENT_PYBUFFER_METHODS( name, type, stype, F1, F2 ) \ 128 PyObject* name##_buffer_str( PyObject* self ) \ 130 Py_ssize_t l = buffer_length( self ); \ 131 return PyROOT_PyUnicode_FromFormat( "<"#type" buffer, size " PY_SSIZE_T_FORMAT ">", l );\ 134 PyObject* name##_buffer_item( PyObject* self, Py_ssize_t idx ) { \ 135 const char* buf = buffer_get( self, idx ); \ 137 return F1( (stype)*((type*)buf + idx) ); \ 141 int name##_buffer_ass_item( PyObject* self, Py_ssize_t idx, PyObject* val ) {\ 142 const char* buf = buffer_get( self, idx ); \ 146 type value = F2( val ); \ 147 if ( value == (type)-1 && PyErr_Occurred() ) \ 150 *((type*)buf+idx) = (type)value; \ 154 PyObject* name##_buffer_subscript( PyObject* self, PyObject* item ) { \ 155 if ( PyIndex_Check( item ) ) { \ 156 Py_ssize_t idx = PyNumber_AsSsize_t( item, PyExc_IndexError ); \ 157 if ( idx == -1 && PyErr_Occurred() ) \ 159 return name##_buffer_item( self, idx ); \ 180 if ( i == -1 && PyErr_Occurred() )
182 return Py_TYPE(
self)->tp_as_sequence->sq_ass_item(
self, i, val );
184 PyErr_SetString( PyExc_TypeError,
"buffer indices must be integers" );
196 if ( nlen == -1 && PyErr_Occurred() )
199 #if PY_VERSION_HEX < 0x03000000 200 ((PyBufferTop_t*)
self)->fSize = nlen * ((PyBufferTop_t*)
self)->fItemSize;
202 PyMemoryView_GET_BUFFER(
self)->len = nlen * PyMemoryView_GET_BUFFER(
self)->itemsize;
205 Py_INCREF( Py_None );
214 if ( PyObject_TypeCheck( pyobject, &PyBoolBuffer_Type ) )
216 else if ( PyObject_TypeCheck( pyobject, &PyCharBuffer_Type ) )
218 else if ( PyObject_TypeCheck( pyobject, &PyUCharBuffer_Type ) )
220 else if ( PyObject_TypeCheck( pyobject, &PyShortBuffer_Type ) )
222 else if ( PyObject_TypeCheck( pyobject, &PyUShortBuffer_Type ) )
224 else if ( PyObject_TypeCheck( pyobject, &PyIntBuffer_Type ) )
226 else if ( PyObject_TypeCheck( pyobject, &PyUIntBuffer_Type ) )
228 else if ( PyObject_TypeCheck( pyobject, &PyLongBuffer_Type ) )
230 else if ( PyObject_TypeCheck( pyobject, &PyULongBuffer_Type ) )
232 else if ( PyObject_TypeCheck( pyobject, &PyFloatBuffer_Type ) )
234 else if ( PyObject_TypeCheck( pyobject, &PyDoubleBuffer_Type ) )
237 PyErr_SetString( PyExc_TypeError,
"received unknown buffer object" );
243 PyGetSetDef buffer_getset[] = {
244 { (
char*)
"typecode", (getter)buf_typecode, NULL, NULL, NULL },
245 { (
char*)NULL, NULL, NULL, NULL, NULL }
250 PyMethodDef buffer_methods[] = {
251 { (
char*)
"SetSize", (PyCFunction)buffer_setsize, METH_O, NULL },
252 { (
char*)NULL, NULL, 0, NULL }
268 #define PYROOT_INSTALL_PYBUFFER_METHODS( name, type ) \ 269 Py##name##Buffer_Type.tp_name = (char*)"ROOT.Py"#name"Buffer"; \ 270 Py##name##Buffer_Type.tp_base = &PyBuffer_Type; \ 271 Py##name##Buffer_Type.tp_as_buffer = PyBuffer_Type.tp_as_buffer; \ 272 Py##name##Buffer_SeqMethods.sq_item = (ssizeargfunc)name##_buffer_item; \ 273 Py##name##Buffer_SeqMethods.sq_ass_item = (ssizeobjargproc)name##_buffer_ass_item;\ 274 Py##name##Buffer_SeqMethods.sq_length = (lenfunc)buffer_length; \ 275 Py##name##Buffer_Type.tp_as_sequence = &Py##name##Buffer_SeqMethods; \ 276 if ( PyBuffer_Type.tp_as_mapping ) { \ 277 Py##name##Buffer_MapMethods.mp_length = (lenfunc)buffer_length; \ 278 Py##name##Buffer_MapMethods.mp_subscript = (binaryfunc)name##_buffer_subscript;\ 279 Py##name##Buffer_MapMethods.mp_ass_subscript = (objobjargproc)pyroot_buffer_ass_subscript;\ 280 Py##name##Buffer_Type.tp_as_mapping = &Py##name##Buffer_MapMethods; \ 282 Py##name##Buffer_Type.tp_str = (reprfunc)name##_buffer_str; \ 283 Py##name##Buffer_Type.tp_methods = buffer_methods; \ 284 Py##name##Buffer_Type.tp_getset = buffer_getset; \ 285 PyType_Ready( &Py##name##Buffer_Type ); 321 #if PY_VERSION_HEX < 0x03000000 322 #define PYBUFFER_SETITEMSIZE(buf,type) ((PyBufferTop_t*)buf)->fItemSize = Py_ssize_t(sizeof(type)) 323 #define PYBUFFER_SETFORMAT(buf,name) 325 #define PYBUFFER_SETITEMSIZE(buf,type) PyMemoryView_GET_BUFFER(buf)->itemsize = Py_ssize_t(sizeof(type)) 326 #define PYBUFFER_SETFORMAT(buf,name) PyMemoryView_GET_BUFFER(buf)->format = (char *)get##name##Format() 330 #define PYROOT_IMPLEMENT_PYBUFFER_FROM_MEMORY( name, type ) \ 331 PyObject* PyROOT::TPyBufferFactory::PyBuffer_FromMemory( type* address, Py_ssize_t size )\ 333 size = size < 0 ? INT_MAX : size; \ 334 PyObject* buf = PyBuffer_FromReadWriteMemory( (void*)address, size ); \ 336 Py_INCREF( (PyObject*)(void*)&Py##name##Buffer_Type ); \ 337 buf->ob_type = &Py##name##Buffer_Type; \ 338 PYBUFFER_SETITEMSIZE(buf,type); \ 339 PYBUFFER_SETFORMAT(buf, name); \ 344 PyObject* PyROOT::TPyBufferFactory::PyBuffer_FromMemory( type* address, PyObject* scb )\ 346 PyObject* buf = PyBuffer_FromMemory( address, Py_ssize_t(0) ); \ 347 if ( buf != 0 && PyCallable_Check( scb ) ) { \ 349 gSizeCallbacks[ buf ] = scb; \ const char * getBoolFormat()
#define PyROOT_PyUnicode_FromString
const char * getCharFormat()
const char * getULongFormat()
Py_ssize_t PyNumber_AsSsize_t(PyObject *obj, PyObject *)
const char * getDoubleFormat()
const char * getLongFormat()
const char * getIntFormat()
#define PYROOT_INSTALL_PYBUFFER_METHODS(name, type)
const char * getUIntFormat()
const char * getUShortFormat()
const char * getShortFormat()
static RooMathCoreReg dummy
const char * getFloatFormat()
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)
const char * getUCharFormat()