How to use the cffi.cparser.Parser function in cffi

To help you get started, we’ve selected a few cffi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dgelessus / pythonista-c-utils / cffipp / cffi_patches.py View on Github external
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:
github revng / llvmcpy / llvmcpy / llvm.py View on Github external
# 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
github flypy / flypy / flypy / extern_support.py View on Github external
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