Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import cffi
import cffi.api
import cffi.cparser
import pycparser
import pycparser.c_ast
__all__ = [
"BetterParser",
"FFIWithBetterParser",
]
class BetterParser(cffi.cparser.Parser):
def __init__(self, ffi):
super().__init__()
self._ffi = ffi
def _declare(self, name, obj, included=False, quals=0):
if name in self._declarations:
prevobj, prevquals = self._declarations[name]
if prevobj == obj and prevquals == quals:
return
if not self._options.get('override'):
raise cffi.api.FFIError(
"multiple declarations of %s (for interactive usage, "
"try cdef(xx, override=True))" % (name,))
assert '__dotdotdot__' not in name.split()
self._declarations[name] = (obj, quals)
if included:
# Save in the context, so that other entries can refere to
# it
context[enum.name] = value
# Rewrite using a constant
enum.value = pycparser.c_ast.Constant("int", str(value))
value += 1
self.enums[self._name] = mapping
# Clear the scope
self._name = None
with open(all_c_preprocessed) as f:
ast, _, _ = cffi.cparser.Parser()._parse(f.read())
visitor = EnumVisitor()
visitor.visit(ast)
with open(all_c_preprocessed, "w") as f:
generator = pycparser.c_generator.CGenerator()
f.write(generator.visit(ast).replace("__dotdotdot__", "foo"))
return visitor.enums
def _parse_cdecl(declstr):
parser = cffi.cparser.Parser()
parser.parse(declstr)
functions = []
# Get all the functions
for k in parser._declarations:
typ, val = k.split()
if typ == 'function':
functions.append(val)
return functions