Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and new warning for cdef() #46

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading