From 3ce9a73e17198513638e6739479fcf2130feea04 Mon Sep 17 00:00:00 2001 From: Armin Rigo Date: Thu, 11 Jan 2024 10:03:11 +0000 Subject: [PATCH] * add a warning when passing #pragmas to the cdef() * test and fix for a case where pycparser is left in a bogus state --- src/cffi/cparser.py | 11 ++++++++++- testing/cffi0/test_parsing.py | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/cffi/cparser.py b/src/cffi/cparser.py index 74830e91..eee83caf 100644 --- a/src/cffi/cparser.py +++ b/src/cffi/cparser.py @@ -329,6 +329,7 @@ def _parse(self, csource): # called from line 1 csourcelines.append('# 1 "%s"' % (CDEF_SOURCE_STRING,)) csourcelines.append(csource) + csourcelines.append('') # see test_missing_newline_bug fullcsource = '\n'.join(csourcelines) if lock is not None: lock.acquire() # pycparser is not thread-safe... @@ -430,7 +431,15 @@ def _internal_parse(self, csource): typedef_example="*(%s *)0" % (decl.name,)) self._declare('typedef ' + decl.name, realtype, quals=quals) elif decl.__class__.__name__ == 'Pragma': - pass # skip pragma, only in pycparser 2.15 + # skip pragma, only in pycparser 2.15 + import warnings + warnings.warn( + "#pragma in cdef() are entirely ignored. " + "They should be removed for now, otherwise your " + "code might behave differently in a future version " + "of CFFI if #pragma support gets added. Note that " + "'#pragma pack' needs to be replaced with the " + "'packed' keyword argument to cdef().") else: raise CDefError("unexpected <%s>: this construct is valid " "C but not valid in cdef()" % diff --git a/testing/cffi0/test_parsing.py b/testing/cffi0/test_parsing.py index af6293ed..f10b989a 100644 --- a/testing/cffi0/test_parsing.py +++ b/testing/cffi0/test_parsing.py @@ -613,3 +613,8 @@ def test_unsigned_int_suffix_for_constant(): for base, expected_result in (('bin', 2), ('oct', 8), ('dec', 10), ('hex', 16)): for index in range(7): assert getattr(C, '{base}_{index}'.format(base=base, index=index)) == expected_result + +def test_missing_newline_bug(): + ffi = FFI(backend=FakeBackend()) + ffi.cdef("#pragma foobar") + ffi.cdef("#pragma foobar") # used to crash the second time