[pypy-commit] pypy rffi-parser: Add buffer to the pseudo-header, handle pointers to primitive types and fixed-size arrays

rlamy pypy.commits at gmail.com
Sat Dec 17 22:54:42 EST 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: rffi-parser
Changeset: r89128:456561b7910a
Date: 2016-12-18 01:24 +0000
http://bitbucket.org/pypy/pypy/changeset/456561b7910a/

Log:	Add buffer to the pseudo-header, handle pointers to primitive types
	and fixed-size arrays

diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -623,6 +623,38 @@
 typedef struct _typeobject PyTypeObject;
 
 typedef void (*freefunc)(void *);
+
+/* Py3k buffer interface, adapted for PyPy */
+#define Py_MAX_NDIMS 32
+#define Py_MAX_FMT 128
+typedef struct bufferinfo {
+    void *buf;
+    PyObject *obj;        /* owned reference */
+    Py_ssize_t len;
+
+    /* This is Py_ssize_t so it can be
+       pointed to by strides in simple case.*/
+    Py_ssize_t itemsize;
+    int readonly;
+    int ndim;
+    char *format;
+    Py_ssize_t *shape;
+    Py_ssize_t *strides;
+    Py_ssize_t *suboffsets; /* alway NULL for app-level objects*/
+    unsigned char _format[Py_MAX_FMT];
+    Py_ssize_t _strides[Py_MAX_NDIMS];
+    Py_ssize_t _shape[Py_MAX_NDIMS];
+    /* static store for shape and strides of
+       mono-dimensional buffers. */
+    /* Py_ssize_t smalltable[2]; */
+    void *internal; /* always NULL for app-level objects */
+} Py_buffer;
+
+
+typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
+typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
+/* end Py3k buffer interface */
+
 """)
 h.configure_types()
 
@@ -645,26 +677,10 @@
 PyVarObjectStruct = h.definitions['PyVarObject'].OF
 PyVarObject = lltype.Ptr(PyVarObjectStruct)
 
-Py_buffer = cpython_struct(
-    "Py_buffer", (
-        ('buf', rffi.VOIDP),
-        ('obj', PyObject),
-        ('len', Py_ssize_t),
-        ('itemsize', Py_ssize_t),
+Py_buffer = h.definitions['Py_buffer']
+getbufferproc = h.definitions['getbufferproc']
+releasebufferproc = h.definitions['releasebufferproc']
 
-        ('readonly', lltype.Signed),
-        ('ndim', lltype.Signed),
-        ('format', rffi.CCHARP),
-        ('shape', Py_ssize_tP),
-        ('strides', Py_ssize_tP),
-        ('_format', rffi.CFixedArray(rffi.UCHAR, Py_MAX_FMT)),
-        ('_shape', rffi.CFixedArray(Py_ssize_t, Py_MAX_NDIMS)),
-        ('_strides', rffi.CFixedArray(Py_ssize_t, Py_MAX_NDIMS)),
-        ('suboffsets', Py_ssize_tP),
-        #('smalltable', rffi.CFixedArray(Py_ssize_t, 2)),
-        ('internal', rffi.VOIDP)
-        ))
-Py_bufferP = lltype.Ptr(Py_buffer)
 
 @specialize.memo()
 def is_PyObject(TYPE):
diff --git a/pypy/module/cpyext/cparser.py b/pypy/module/cpyext/cparser.py
--- a/pypy/module/cpyext/cparser.py
+++ b/pypy/module/cpyext/cparser.py
@@ -732,6 +732,8 @@
                 return rffi.VOIDP
             elif isinstance(TO, DelayedStruct):
                 TO = TO.TYPE
+            elif isinstance(obj.totype, model.PrimitiveType):
+                return rffi.CArrayPtr(TO)
             return lltype.Ptr(TO)
         elif isinstance(obj, model.FunctionPtrType):
             if obj.ellipsis:
@@ -741,6 +743,8 @@
             return lltype.Ptr(lltype.FuncType(args, res))
         elif isinstance(obj, model.VoidType):
             return lltype.Void
+        elif isinstance(obj, model.ArrayType):
+            return rffi.CFixedArray(self.convert_type(obj.item), obj.length)
         else:
             raise NotImplementedError
 


More information about the pypy-commit mailing list