Author: Armin Rigo <[email protected]>
Branch:
Changeset: r2230:d5d809e7081b
Date: 2015-07-30 17:05 +0200
http://bitbucket.org/cffi/cffi/changeset/d5d809e7081b/
Log: Support "\" escapes in a #define
diff --git a/cffi/cparser.py b/cffi/cparser.py
--- a/cffi/cparser.py
+++ b/cffi/cparser.py
@@ -17,8 +17,9 @@
_r_comment = re.compile(r"/\*.*?\*/|//([^\n\\]|\\.)*?$",
re.DOTALL | re.MULTILINE)
-_r_define = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)\s+(.*?)$",
- re.MULTILINE)
+_r_define = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)"
+ r"\b((?:[^\n\\]|\\.)*?)$",
+ re.DOTALL | re.MULTILINE)
_r_partial_enum = re.compile(r"=\s*\.\.\.\s*[,}]|\.\.\.\s*\}")
_r_enum_dotdotdot = re.compile(r"__dotdotdot\d+__$")
_r_partial_array = re.compile(r"\[\s*\.\.\.\s*\]")
@@ -40,6 +41,7 @@
macros = {}
for match in _r_define.finditer(csource):
macroname, macrovalue = match.groups()
+ macrovalue = macrovalue.replace('\\\n', '').strip()
macros[macroname] = macrovalue
csource = _r_define.sub('', csource)
# Replace "[...]" with "[__dotdotdotarray__]"
diff --git a/testing/cffi0/test_parsing.py b/testing/cffi0/test_parsing.py
--- a/testing/cffi0/test_parsing.py
+++ b/testing/cffi0/test_parsing.py
@@ -176,6 +176,18 @@
m.y
m.z
+def test_line_continuation_in_defines():
+ ffi = FFI(backend=FakeBackend())
+ ffi.cdef("""
+ #define ABC\\
+ 42
+ #define BCD \\
+ 43
+ """)
+ m = ffi.dlopen(lib_m)
+ assert m.ABC == 42
+ assert m.BCD == 43
+
def test_define_not_supported_for_now():
ffi = FFI(backend=FakeBackend())
e = py.test.raises(CDefError, ffi.cdef, '#define FOO "blah"')
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit