Skip to content

Commit

Permalink
* add a warning when passing #pragmas to the cdef() (#46)
Browse files Browse the repository at this point in the history
* test and fix for a case where pycparser is left in a bogus state
  • Loading branch information
arigo authored Jan 22, 2024
1 parent feaff37 commit 39f9987
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/cffi/cparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ def _parse(self, csource):
# called <cdef source string> 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...
Expand Down Expand Up @@ -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()" %
Expand Down
5 changes: 5 additions & 0 deletions testing/cffi0/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 39f9987

Please sign in to comment.