Author: Ronan Lamy <[email protected]>
Branch: rffi-parser-2
Changeset: r89249:6c787b358130
Date: 2016-12-18 20:34 +0000
http://bitbucket.org/pypy/pypy/changeset/6c787b358130/
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
@@ -695,9 +695,9 @@
self.structs.update(other.structs)
self.includes.append(other)
- def add_typedef(self, name, obj, configure_now=False):
+ def add_typedef(self, name, obj, quals, configure_now=False):
assert name not in self.definitions
- tp = self.convert_type(obj)
+ tp = self.convert_type(obj, quals)
if isinstance(tp, DelayedStruct):
tp = self.realize_struct(tp, name, configure_now=configure_now)
self.definitions[name] = tp
@@ -735,7 +735,7 @@
continue
if name.startswith('typedef '):
name = name[8:]
- self.add_typedef(name, obj, configure_now=configure_now)
+ self.add_typedef(name, obj, quals, configure_now=configure_now)
elif name.startswith('macro '):
name = name[6:]
self.add_macro(name, obj)
@@ -743,7 +743,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):
@@ -754,11 +754,16 @@
TO = self.convert_type(obj.totype)
if TO is lltype.Void:
return rffi.VOIDP
- elif isinstance(obj.totype, model.PrimitiveType):
- return rffi.CArrayPtr(TO)
elif isinstance(TO, DelayedStruct):
TO = TO.TYPE
- return lltype.Ptr(TO)
+ if isinstance(TO, lltype.ContainerType):
+ return lltype.Ptr(TO)
+ else:
+ 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
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
@@ -129,3 +129,16 @@
Object = foo_h.definitions['Object']
assert isinstance(Object, lltype.Struct)
hash(Object)
+
+def test_const(tmpdir):
+ cdef = """
+ typedef struct {
+ const char * const foo;
+ } bar;
+ """
+ (tmpdir / 'foo.h').write(cdef)
+ eci = ExternalCompilationInfo(
+ include_dirs=[str(tmpdir)],
+ includes=['sys/types.h', 'foo.h'])
+ hdr = parse_source(cdef, eci=eci, configure_now=True)
+ assert hdr.definitions['bar'].c_foo == rffi.CONST_CCHARP != rffi.CCHARP
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit