[pypy-commit] pypy rffi-parser: Handle const pointers (we probably only care about 'const char *')

rlamy pypy.commits at gmail.com
Sun Dec 18 15:35:39 EST 2016


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: rffi-parser
Changeset: r89164:94988b4687ff
Date: 2016-12-18 20:34 +0000
http://bitbucket.org/pypy/pypy/changeset/94988b4687ff/

Log:	Handle const pointers (we probably only care about 'const char *')

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
@@ -693,9 +693,9 @@
         self.structs.update(other.structs)
         self.includes.append(other)
 
-    def add_typedef(self, name, obj):
+    def add_typedef(self, name, obj, quals):
         assert name not in self.definitions
-        tp = self.convert_type(obj)
+        tp = self.convert_type(obj, quals)
         if isinstance(tp, DelayedStruct):
             self.realize_struct(tp, name)
             tp = self.structs[obj] = tp.TYPE
@@ -728,7 +728,7 @@
             if name in self._TYPES:
                 self._TYPES[name].become(TYPE)
 
-    def convert_type(self, obj):
+    def convert_type(self, obj, quals=0):
         if isinstance(obj, model.PrimitiveType):
             return cname_to_lltype(obj.name)
         elif isinstance(obj, model.StructType):
@@ -746,7 +746,11 @@
             if isinstance(TO, lltype.ContainerType):
                 return lltype.Ptr(TO)
             else:
-                return rffi.CArrayPtr(TO)
+                if obj.quals & model.Q_CONST:
+                    return lltype.Ptr(lltype.Array(
+                        TO, hints={'nolength': True, 'render_as_const': True}))
+                else:
+                    return rffi.CArrayPtr(TO)
         elif isinstance(obj, model.FunctionPtrType):
             if obj.ellipsis:
                 raise NotImplementedError
@@ -773,7 +777,7 @@
             continue
         if name.startswith('typedef '):
             name = name[8:]
-            src.add_typedef(name, obj)
+            src.add_typedef(name, obj, quals)
         elif name.startswith('macro '):
             name = name[6:]
             src.add_macro(name, obj)
diff --git a/pypy/module/cpyext/test/test_cparser.py b/pypy/module/cpyext/test/test_cparser.py
--- a/pypy/module/cpyext/test/test_cparser.py
+++ b/pypy/module/cpyext/test/test_cparser.py
@@ -63,3 +63,11 @@
     hdr2 = parse_source(cdef2, includes=[hdr1])
     assert 'Object' in hdr2.definitions
     assert 'Type' not in hdr2.definitions
+
+def test_const():
+    cdef = """
+    typedef struct {
+        const char * const foo;
+    } bar;
+    """
+    hdr = parse_source(cdef)
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -702,9 +702,11 @@
         heaptype = rffi.cast(PyHeapTypeObject, pto)
         heaptype.c_ht_name = make_ref(space, w_typename)
         from pypy.module.cpyext.bytesobject import PyString_AsString
-        pto.c_tp_name = PyString_AsString(space, heaptype.c_ht_name)
+        pto.c_tp_name = rffi.cast(
+            rffi.CONST_CCHARP, PyString_AsString(space, heaptype.c_ht_name))
     else:
-        pto.c_tp_name = rffi.str2charp(w_type.name)
+        pto.c_tp_name = rffi.cast(
+            rffi.CONST_CCHARP, rffi.str2charp(w_type.name))
     # uninitialized fields:
     # c_tp_print
     # XXX implement


More information about the pypy-commit mailing list