Author: Armin Rigo <[email protected]>
Branch: qualtypes
Changeset: r2283:f8c4c25618be
Date: 2015-09-30 11:34 +0200
http://bitbucket.org/cffi/cffi/changeset/f8c4c25618be/
Log: Tests and fixes for "const" fields
diff --git a/cffi/model.py b/cffi/model.py
--- a/cffi/model.py
+++ b/cffi/model.py
@@ -20,7 +20,7 @@
is_array_type = False
is_raw_function = False
- def get_c_name(self, replace_with='', context='a C file'):
+ def get_c_name(self, replace_with='', context='a C file', quals=0):
result = self.c_name_with_marker
assert result.count('&') == 1
# some logic duplication with ffi.getctype()... :-(
@@ -30,6 +30,7 @@
replace_with = '(%s)' % replace_with
elif not replace_with[0] in '[(':
replace_with = ' ' + replace_with
+ replace_with = qualify(quals, replace_with)
result = result.replace('&', replace_with)
if '$' in result:
from .ffiplatform import VerificationError
diff --git a/cffi/recompiler.py b/cffi/recompiler.py
--- a/cffi/recompiler.py
+++ b/cffi/recompiler.py
@@ -788,9 +788,9 @@
and (ftype.length is None or ftype.length == '...')):
ftype = ftype.item
fname = fname + '[0]'
- tmp = model.qualify(fqual, '*tmp')
prnt(' { %s = &p->%s; (void)tmp; }' % (
- ftype.get_c_name(tmp, 'field %r'%fname), fname))
+ ftype.get_c_name('*tmp', 'field %r'%fname, quals=fqual),
+ fname))
except ffiplatform.VerificationError as e:
prnt(' /* %s */' % str(e)) # cannot verify it, ignore
prnt('}')
@@ -1087,8 +1087,7 @@
# (If 'tp' is a function _pointer_ type, then casts from "fn_t
# **" to "void *" are again no-ops, as far as I can tell.)
decl = '*_cffi_var_%s(void)' % (name,)
- decl = model.qualify(self._current_quals, decl)
- prnt('static ' + tp.get_c_name(decl))
+ prnt('static ' + tp.get_c_name(decl, quals=self._current_quals))
prnt('{')
prnt(' return %s(%s);' % (ampersand, name))
prnt('}')
diff --git a/testing/cffi1/test_recompiler.py b/testing/cffi1/test_recompiler.py
--- a/testing/cffi1/test_recompiler.py
+++ b/testing/cffi1/test_recompiler.py
@@ -1191,3 +1191,43 @@
py.test.raises(ffi.error, getattr, lib, 'my_value')
e = py.test.raises(ffi.error, setattr, lib, 'my_value', 50)
assert str(e.value) == "global variable 'my_value' is at address NULL"
+
+def test_const_fields():
+ ffi = FFI()
+ ffi.cdef("""struct foo_s { const int a; void *const b; };""")
+ lib = verify(ffi, 'test_const_fields', """
+ struct foo_s { const int a; void *const b; };""")
+ foo_s = ffi.typeof("struct foo_s")
+ assert foo_s.fields[0][0] == 'a'
+ assert foo_s.fields[0][1].type is ffi.typeof("int")
+ assert foo_s.fields[1][0] == 'b'
+ assert foo_s.fields[1][1].type is ffi.typeof("void *")
+
+def test_restrict_fields():
+ if sys.platform == 'win32':
+ py.test.skip("'__restrict__' probably not recognized")
+ ffi = FFI()
+ ffi.cdef("""struct foo_s { void * restrict b; };""")
+ lib = verify(ffi, 'test_const_fields', """
+ struct foo_s { void * __restrict__ b; };""")
+ foo_s = ffi.typeof("struct foo_s")
+ assert foo_s.fields[0][0] == 'b'
+ assert foo_s.fields[0][1].type is ffi.typeof("void *")
+
+def test_const_array_fields():
+ ffi = FFI()
+ ffi.cdef("""struct foo_s { const int a[4]; };""")
+ lib = verify(ffi, 'test_const_array_fields', """
+ struct foo_s { const int a[4]; };""")
+ foo_s = ffi.typeof("struct foo_s")
+ assert foo_s.fields[0][0] == 'a'
+ assert foo_s.fields[0][1].type is ffi.typeof("int[4]")
+
+def test_const_array_fields_varlength():
+ ffi = FFI()
+ ffi.cdef("""struct foo_s { const int a[]; ...; };""")
+ lib = verify(ffi, 'test_const_array_fields_varlength', """
+ struct foo_s { const int a[4]; };""")
+ foo_s = ffi.typeof("struct foo_s")
+ assert foo_s.fields[0][0] == 'a'
+ assert foo_s.fields[0][1].type is ffi.typeof("int[]")
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit