1 #include "Python.h"
2
3 /* cython does not support this preprocessor check => write it in raw C */
4 #if PY_MAJOR_VERSION == 2
5 static PyObject *
buff_to_buff(char * buff,Py_ssize_t size)6 buff_to_buff(char *buff, Py_ssize_t size)
7 {
8 return PyBuffer_FromMemory(buff, size);
9 }
10
11 #elif (PY_MAJOR_VERSION == 3) && (PY_MINOR_VERSION >= 3)
12 static PyObject *
buff_to_buff(char * buff,Py_ssize_t size)13 buff_to_buff(char *buff, Py_ssize_t size)
14 {
15 return PyMemoryView_FromMemory(buff, size, PyBUF_READ);
16 }
17 #else
18 static PyObject *
buff_to_buff(char * buff,Py_ssize_t size)19 buff_to_buff(char *buff, Py_ssize_t size)
20 {
21 Py_buffer pybuf;
22 if (PyBuffer_FillInfo(&pybuf, NULL, buff, size, 1, PyBUF_FULL_RO) == -1) {
23 return NULL;
24 }
25
26 return PyMemoryView_FromBuffer(&pybuf);
27 }
28 #endif
29