11#if PY_VERSION_HEX >= 0x03000000
12static 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 );
211 if (PyErr_WarnEx(PyExc_FutureWarning,
212 "buffer.SetSize(N) is deprecated and will disappear in a future version of ROOT. "
213 "Instead, use buffer.reshape((N,))", 1) < 0) {
217 return buffer_setsize(self, pynlen);
223 if (!PyTuple_Check(shape) || PyTuple_GET_SIZE(shape) != 1) {
225 PyObject* pystr = PyObject_Str(shape);
227 PyErr_Format(PyExc_TypeError,
"tuple object of length 1 expected, received %s",
233 PyErr_SetString(PyExc_TypeError,
"tuple object of length 1 expected");
237 return buffer_setsize(self, PyTuple_GET_ITEM(shape, 0));
245 if ( PyObject_TypeCheck( pyobject, &PyBoolBuffer_Type ) )
247 else if ( PyObject_TypeCheck( pyobject, &PyCharBuffer_Type ) )
249 else if ( PyObject_TypeCheck( pyobject, &PyUCharBuffer_Type ) )
251 else if ( PyObject_TypeCheck( pyobject, &PyShortBuffer_Type ) )
253 else if ( PyObject_TypeCheck( pyobject, &PyUShortBuffer_Type ) )
255 else if ( PyObject_TypeCheck( pyobject, &PyIntBuffer_Type ) )
257 else if ( PyObject_TypeCheck( pyobject, &PyUIntBuffer_Type ) )
259 else if ( PyObject_TypeCheck( pyobject, &PyLongBuffer_Type ) )
261 else if ( PyObject_TypeCheck( pyobject, &PyULongBuffer_Type ) )
263 else if ( PyObject_TypeCheck( pyobject, &PyFloatBuffer_Type ) )
265 else if ( PyObject_TypeCheck( pyobject, &PyDoubleBuffer_Type ) )
268 PyErr_SetString( PyExc_TypeError,
"received unknown buffer object" );
274 PyGetSetDef buffer_getset[] = {
275 { (
char*)
"typecode", (getter)buf_typecode, NULL, NULL, NULL },
276 { (
char*)NULL, NULL, NULL, NULL, NULL }
281 PyMethodDef buffer_methods[] = {
282 { (
char*)
"SetSize", (PyCFunction)SetSize, METH_O, NULL },
283 { (
char*)
"reshape", (PyCFunction)reshape, METH_O, NULL },
284 { (
char*)NULL, NULL, 0, NULL }
300#define PYROOT_INSTALL_PYBUFFER_METHODS( name, type ) \
301 Py##name##Buffer_Type.tp_name = (char*)"ROOT.Py"#name"Buffer"; \
302 Py##name##Buffer_Type.tp_base = &PyBuffer_Type; \
303 Py##name##Buffer_Type.tp_as_buffer = PyBuffer_Type.tp_as_buffer; \
304 Py##name##Buffer_SeqMethods.sq_item = (ssizeargfunc)name##_buffer_item; \
305 Py##name##Buffer_SeqMethods.sq_ass_item = (ssizeobjargproc)name##_buffer_ass_item;\
306 Py##name##Buffer_SeqMethods.sq_length = (lenfunc)buffer_length; \
307 Py##name##Buffer_Type.tp_as_sequence = &Py##name##Buffer_SeqMethods; \
308 if ( PyBuffer_Type.tp_as_mapping ) {
\
309 Py##name##Buffer_MapMethods.mp_length = (lenfunc)buffer_length; \
310 Py##name##Buffer_MapMethods.mp_subscript = (binaryfunc)name##_buffer_subscript;\
311 Py##name##Buffer_MapMethods.mp_ass_subscript = (objobjargproc)pyroot_buffer_ass_subscript;\
312 Py##name##Buffer_Type.tp_as_mapping = &Py##name##Buffer_MapMethods; \
314 Py##name##Buffer_Type.tp_str = (reprfunc)name##_buffer_str; \
315 Py##name##Buffer_Type.tp_methods = buffer_methods; \
316 Py##name##Buffer_Type.tp_getset = buffer_getset; \
317 PyType_Ready( &Py##name##Buffer_Type );
353#if PY_VERSION_HEX < 0x03000000
354 #define PYBUFFER_SETITEMSIZE(buf,type) ((PyBufferTop_t*)buf)->fItemSize = Py_ssize_t(sizeof(type))
355 #define PYBUFFER_SETFORMAT(buf,name)
357 #define PYBUFFER_SETITEMSIZE(buf,type) PyMemoryView_GET_BUFFER(buf)->itemsize = Py_ssize_t(sizeof(type))
358 #define PYBUFFER_SETFORMAT(buf,name) PyMemoryView_GET_BUFFER(buf)->format = (char *)get##name##Format()
362#define PYROOT_IMPLEMENT_PYBUFFER_FROM_MEMORY( name, type ) \
363PyObject* PyROOT::TPyBufferFactory::PyBuffer_FromMemory( type* address, Py_ssize_t size )\
365 size = size < 0 ? INT_MAX : size; \
366 PyObject* buf = PyBuffer_FromReadWriteMemory( (void*)address, size ); \
368 Py_INCREF( (PyObject*)(void*)&Py##name##Buffer_Type ); \
369 buf->ob_type = &Py##name##Buffer_Type; \
370 PYBUFFER_SETITEMSIZE(buf,type); \
371 PYBUFFER_SETFORMAT(buf, name); \
376PyObject* PyROOT::TPyBufferFactory::PyBuffer_FromMemory( type* address, PyObject* scb )\
378 PyObject* buf = PyBuffer_FromMemory( address, Py_ssize_t(0) ); \
379 if ( buf != 0 && PyCallable_Check( scb ) ) { \
381 gSizeCallbacks[ buf ] = scb; \
Py_ssize_t PyNumber_AsSsize_t(PyObject *obj, PyObject *)
#define PyROOT_PyUnicode_FromString
#define PyROOT_PyUnicode_AsStringChecked
#define PyIndex_Check(obj)
static RooMathCoreReg dummy
#define PYROOT_IMPLEMENT_PYBUFFER_FROM_MEMORY(name, type)
const char * getShortFormat()
const char * getUShortFormat()
const char * getIntFormat()
#define PYROOT_PREPARE_PYBUFFER_TYPE(name)
const char * getULongFormat()
const char * getUCharFormat()
const char * getBoolFormat()
const char * getUIntFormat()
#define PYROOT_INSTALL_PYBUFFER_METHODS(name, type)
const char * getFloatFormat()
const char * getLongFormat()
#define PYROOT_IMPLEMENT_PYBUFFER_METHODS(name, type, stype, F1, F2)
const char * getDoubleFormat()
const char * getCharFormat()
Factory for python buffers of non-string type.
static TPyBufferFactory * Instance()