def main(args=None):
    '''Main function.'''
    import logging
    import sys

    parser, args = _parse_args(args=args)
    if not args.i:
        parser.print_usage()
        return 0

    logging.basicConfig(format='%(filename)s: %(message)s')
    if args.v > 0:
        logging.getLogger().setLevel(logging.INFO)

    if args.ccargs and args.ccargs[0] == '--':
        clang_args = args.ccargs[1:]
    else:
        clang_args = args.ccargs

    choose_cindex_impl(args.cindex)
    from cbind.cindex import Diagnostic
    from cbind.codegen import CodeGen
    from cbind.ctypes_binding import CtypesBindingGenerator
    from cbind.macro import MacroGenerator
    from cbind.source import SyntaxTree

    CodeGen.ENABLE_CPP = args.enable_cpp
    CodeGen.ASSERT_LAYOUT = args.assert_layout
    SyntaxTree.SEVERITY = getattr(Diagnostic, args.severity.capitalize())

    cbgen = CtypesBindingGenerator()
    if args.config:
        try:
            import yaml
        except ImportError:
            parser.error('could not load Python package yaml')
        cbgen.config(yaml.load(args.config))

    for c_src in args.i:
        cbgen.parse(c_src, args=clang_args)
    if args.enable_macro:
        mcgen = MacroGenerator(macro_int=args.macro_int)
        for c_src in args.i:
            mcgen.parse(c_src, args=clang_args)

    if args.o == '-':
        output = sys.stdout
    else:
        output = open(args.o, 'w')
    with output:
        cbgen.generate_preamble(parser.prog, args.l, output)
        cbgen.generate(output)
        if args.enable_macro:
            mcgen.generate(output)

    return 0
def main(args=None):
    """Main function."""
    import logging
    import sys

    parser, args = _parse_args(args=args)
    if not args.i:
        parser.print_usage()
        return 0

    logging.basicConfig(format="%(filename)s: %(message)s")
    if args.v > 0:
        logging.getLogger().setLevel(logging.INFO)

    if args.ccargs and args.ccargs[0] == "--":
        clang_args = args.ccargs[1:]
    else:
        clang_args = args.ccargs

    choose_cindex_impl(args.cindex)
    from cbind.cindex import Diagnostic
    from cbind.codegen import CodeGen
    from cbind.ctypes_binding import CtypesBindingGenerator
    from cbind.macro import MacroGenerator
    from cbind.source import SyntaxTree

    CodeGen.ENABLE_CPP = args.enable_cpp
    CodeGen.ASSERT_LAYOUT = args.assert_layout
    SyntaxTree.SEVERITY = getattr(Diagnostic, args.severity.capitalize())

    cbgen = CtypesBindingGenerator()
    if args.config:
        try:
            import yaml
        except ImportError:
            parser.error("could not load Python package yaml")
        cbgen.config(yaml.load(args.config))

    for c_src in args.i:
        cbgen.parse(c_src, args=clang_args)
    if args.enable_macro:
        mcgen = MacroGenerator(macro_int=args.macro_int)
        for c_src in args.i:
            mcgen.parse(c_src, args=clang_args)

    if args.o == "-":
        output = sys.stdout
    else:
        output = open(args.o, "w")
    with output:
        cbgen.generate_preamble(parser.prog, args.l, output)
        cbgen.generate(output)
        if args.enable_macro:
            mcgen.generate(output)

    return 0
 def run_test(self, cpp_code, symbols, args=None):
     '''Test mangler.'''
     cbgen = CtypesBindingGenerator()
     cpp_src = StringIO(cpp_code)
     cbgen.parse('input.cpp', contents=cpp_src, args=args)
     root = cbgen.syntax_tree_forest[0]
     symbol_table = self._make_symbol_table(symbols, root)
     errmsg = StringIO()
     errmsg.write('In comparing mangled names:\n')
     for blob in symbol_table:
         symbol = blob['symbol']
         mangled_name = blob['mangled_name']
         output_name = blob['output_name']
         if mangled_name != output_name:
             errmsg.write(
                 'Mangling symbol %s: %s != %s\n' %
                 (repr(symbol), repr(mangled_name), repr(output_name)))
     format_ast([root.translation_unit], errmsg)
     errmsg = errmsg.getvalue()
     for blob in symbol_table:
         mangled_name = blob['mangled_name']
         output_name = blob['output_name']
         self.assertEqual(mangled_name, output_name, errmsg)
 def run_test(self, cpp_code, symbols, args=None):
     '''Test mangler.'''
     cbgen = CtypesBindingGenerator()
     cpp_src = StringIO(cpp_code)
     cbgen.parse('input.cpp', contents=cpp_src, args=args)
     root = cbgen.syntax_tree_forest[0]
     symbol_table = self._make_symbol_table(symbols, root)
     errmsg = StringIO()
     errmsg.write('In comparing mangled names:\n')
     for blob in symbol_table:
         symbol = blob['symbol']
         mangled_name = blob['mangled_name']
         output_name = blob['output_name']
         if mangled_name != output_name:
             errmsg.write('Mangling symbol %s: %s != %s\n' %
                          (repr(symbol),
                           repr(mangled_name),
                           repr(output_name)))
     format_ast([root.translation_unit], errmsg)
     errmsg = errmsg.getvalue()
     for blob in symbol_table:
         mangled_name = blob['mangled_name']
         output_name = blob['output_name']
         self.assertEqual(mangled_name, output_name, errmsg)
Example #5
0
 def test_file(self):
     c_file = os.path.join(os.path.dirname(__file__), 'file.c')
     cbgen = CtypesBindingGenerator()
     cbgen.parse(c_file)
    def run_test(self, c_code, python_code,
                 filename='input.c', args=None, config=None,
                 enable_cpp=False,
                 assert_layout=False):
        '''Generate Python code from C code and compare it to the answer.'''
        CodeGen.ENABLE_CPP = enable_cpp
        CodeGen.ASSERT_LAYOUT = assert_layout
        cbgen = CtypesBindingGenerator()
        if config is not None:
            import yaml
            config = yaml.load(config)
            cbgen.config(config)
        if isinstance(c_code, str):
            c_src = StringIO(c_code)
            cbgen.parse(filename, contents=c_src, args=args)
        else:
            for filename, code in c_code:
                c_src = StringIO(code)
                cbgen.parse(filename, contents=c_src, args=args)
        output = StringIO()
        if config and 'preamble' in config:
            cbgen.generate_preamble('cbind', None, output)
        cbgen.generate(output)
        gen_code = output.getvalue()

        error_message = prepare_error_message(
            python_code, gen_code, tunits=cbgen.get_translation_units())
        self.assertTrue(compare_codes(gen_code, python_code), error_message)
        code = compile(gen_code, 'output.py', 'exec')
        if assert_layout:
            # Test if layout assertions are true
            exec(code, self.get_env())  # pylint: disable=W0122
    def run_test(self,
                 c_code,
                 python_code,
                 filename='input.c',
                 args=None,
                 config=None,
                 enable_cpp=False,
                 assert_layout=False):
        '''Generate Python code from C code and compare it to the answer.'''
        CodeGen.ENABLE_CPP = enable_cpp
        CodeGen.ASSERT_LAYOUT = assert_layout
        cbgen = CtypesBindingGenerator()
        if config is not None:
            import yaml
            config = yaml.load(config)
            cbgen.config(config)
        if isinstance(c_code, str):
            c_src = StringIO(c_code)
            cbgen.parse(filename, contents=c_src, args=args)
        else:
            for filename, code in c_code:
                c_src = StringIO(code)
                cbgen.parse(filename, contents=c_src, args=args)
        output = StringIO()
        if config and 'preamble' in config:
            cbgen.generate_preamble('cbind', None, output)
        cbgen.generate(output)
        gen_code = output.getvalue()

        error_message = prepare_error_message(
            python_code, gen_code, tunits=cbgen.get_translation_units())
        self.assertTrue(compare_codes(gen_code, python_code), error_message)
        code = compile(gen_code, 'output.py', 'exec')
        if assert_layout:
            # Test if layout assertions are true
            exec(code, self.get_env())  # pylint: disable=W0122
 def test_file(self):
     c_file = os.path.join(os.path.dirname(__file__), 'file.c')
     cbgen = CtypesBindingGenerator()
     cbgen.parse(c_file)