diff options
author | schwarz <schwarz@zib.de> | 2014-07-27 16:50:45 +0200 |
---|---|---|
committer | schwarz <schwarz@zib.de> | 2014-07-27 16:50:45 +0200 |
commit | 27788d02bdef260ebe66db151ad5ea79061decda (patch) | |
tree | 4662a44dc9c95d13dde3bdca118d95f709bc3058 | |
parent | cc0d84a0b1ad2b402ea889c2160e1056e04b3357 (diff) | |
download | cffi-27788d02bdef260ebe66db151ad5ea79061decda.tar.gz |
add parsing of constant with unary +
-rw-r--r-- | cffi/cparser.py | 6 | ||||
-rw-r--r-- | testing/test_parsing.py | 11 |
2 files changed, 16 insertions, 1 deletions
diff --git a/cffi/cparser.py b/cffi/cparser.py index a53d4c3..d699894 100644 --- a/cffi/cparser.py +++ b/cffi/cparser.py @@ -532,11 +532,15 @@ class Parser(object): def _parse_constant(self, exprnode, partial_length_ok=False): # for now, limited to expressions that are an immediate number - # or negative number + # or positive/negative number if isinstance(exprnode, pycparser.c_ast.Constant): return int(exprnode.value, 0) # if (isinstance(exprnode, pycparser.c_ast.UnaryOp) and + exprnode.op == '+'): + return self._parse_constant(exprnode.expr) + # + if (isinstance(exprnode, pycparser.c_ast.UnaryOp) and exprnode.op == '-'): return -self._parse_constant(exprnode.expr) # load previously defined int constant diff --git a/testing/test_parsing.py b/testing/test_parsing.py index 594796a..97a16f9 100644 --- a/testing/test_parsing.py +++ b/testing/test_parsing.py @@ -288,3 +288,14 @@ def test__is_constant_globalvar(): decl = ast.children()[0][1] node = decl.type assert p._is_constant_globalvar(node) == expected_output + +def test_enum(): + ffi = FFI() + ffi.cdef(""" + enum Enum { POS = +1, TWO = 2, NIL = 0, NEG = -1}; + """) + C = ffi.dlopen(None) + assert C.POS == 1 + assert C.TWO == 2 + assert C.NIL == 0 + assert C.NEG == -1 |