Beispiel #1
0
 def test_arg_input_stdin(self):
     """run echo | clang2py - """
     from ctypeslib import clang2py
     with patch('sys.stdin', StringIO('int i = 0;')) as stdin, patch('sys.stdout', new=StringIO()) as fake_out:
         clang2py.main(['-'])
         self.assertIn("__all__ =", fake_out.getvalue())
         self.assertIn("# TARGET arch is: []", fake_out.getvalue())
Beispiel #2
0
 def test_arg_clang_args(self):
     """run clang2py test/data/test-basic-types.c --clang-args="-DDEBUG=2" """
     from ctypeslib import clang2py
     with patch('sys.stdin', StringIO('int i = DEBUG;')) as stdin, patch('sys.stdout', new=StringIO()) as fake_out:
         clang2py.main(['', '--clang-args="-DDEBUG=2"', '-'])
         self.assertIn("# TARGET arch is: []", fake_out.getvalue())
         self.assertIn("i = 2", fake_out.getvalue())
Beispiel #3
0
 def test_version(self):
     """run clang2py -v"""
     from ctypeslib import clang2py
     with patch('sys.stdout', new=StringIO()) as fake_out:
         with self.assertRaises(SystemExit):
             clang2py.main(['--version'])
         self.assertIn(str(ctypeslib.__version__), fake_out.getvalue())
Beispiel #4
0
 def test(self):
     clang2py.main(["clang2py",
                    "-c",
                    "-o", "_stdio_gen.xml",
                    "stdio.h"
                    ])
     import _stdio_gen
Beispiel #5
0
 def test_arg_debug(self):
     """run clang2py --debug test/data/test-basic-types.c"""
     from ctypeslib import clang2py
     with patch('sys.stdout', new=StringIO()) as fake_out, patch('sys.stderr', new=StringIO()) as fake_err:
         clang2py.main(['--debug', 'test/data/test-basic-types.c'])
         self.assertIn("_int = ctypes.c_int", fake_out.getvalue())
         self.assertIn("DEBUG:clangparser:ARCH sizes:", fake_err.getvalue())
         self.assertNotIn("ERROR", fake_err.getvalue())
Beispiel #6
0
 def test_windows(self):
     clang2py.main(["clang2py",
                    "-c",
                    "-w",
                    "-m", "ctypes.wintypes",
                    "-o", "_winapi_gen.py",
                    "windows.h"
                    ])
     import _winapi_gen
Beispiel #7
0
def ctypes_gen(includes=None, use_fake_libs=False, debug=False):
    """
    Generates ctypes types for trezor_crypto
    :return:
    """
    from ctypeslib import clang2py
    from pipes import quote

    if includes is None:
        includes = []

    types_fname = get_ctypes_types_fname()
    clang_args = get_compile_args() + ['-Isrc/']

    for inc in includes:
        clang_args.append('-I%s' % quote(inc))
    if includes is None and os.path.exists('/usr/include'):
        clang_args.append('-I%s' % quote('/usr/include'))
    # detect default includes: gcc -xc -E -v /dev/null

    base_dir = 'src'
    h_files = glob.glob(os.path.join(base_dir, "*.h")) \
               + ['src/ed25519-donna/ed25519-donna.h', ] \
               + ['src/ed25519-donna/curve25519-donna-32bit.h', ] \
               + ['src/ed25519-donna/modm-donna-32bit.h', ] \
               + glob.glob(os.path.join(os.path.join(base_dir, 'aes'), "*.h")) \
               + glob.glob(os.path.join(os.path.join(base_dir, 'chacha20poly1305'), "*.h")) \
               + glob.glob(os.path.join(os.path.join(base_dir, 'monero'), "*.h"))

    args = [
        'clang2py',
        '--clang-args=%s' % quote(' '.join(clang_args)),
        '-o%s' % quote(types_fname),
    ]
    for cf in h_files:
        args.append(quote(cf))

    logger.info('Clang args: %s' % (' '.join(args)))

    _back = sys.argv
    sys.argv = args
    clang2py.main()
    sys.argv = _back

    # post edit
    with open(types_fname) as fh:
        types_data = fh.read()

    types_data = re.sub(r'\]\s*$', ', \'POINTER_T\']\n\n', types_data)
    with open(types_fname, 'w') as fh:
        fh.write(types_data)

    ctypes_functions()
Beispiel #8
0
    def test_arg_target(self):
        """run clang2py --target x86_64-Linux test/data/test-basic-types.c """
        from ctypeslib import clang2py
        with patch('sys.stdout', new=StringIO()) as fake_out:
            clang2py.main(['--target', 'x86_64-Linux', 'test/data/test-basic-types.c'])
            self.assertIn("# TARGET arch is: ['-target', 'x86_64-Linux']", fake_out.getvalue())
            self.assertIn("_int = ctypes.c_int", fake_out.getvalue())
            self.assertIn("_long = ctypes.c_int64", fake_out.getvalue())

            clang2py.main(['--target', 'i586-Linux', 'test/data/test-basic-types.c'])
            self.assertIn("# TARGET arch is: ['-target', 'i586-Linux']", fake_out.getvalue())
            self.assertIn("_int = ctypes.c_int", fake_out.getvalue())
            self.assertIn("_long = ctypes.c_int32", fake_out.getvalue())
Beispiel #9
0
 def test_arg_file(self):
     """run clang2py test/data/test-basic-types.c"""
     from ctypeslib import clang2py
     with patch('sys.stdout', new=StringIO()) as fake_out:
         clang2py.main(['test/data/test-basic-types.c'])
         self.assertIn("_int = ctypes.c_int", fake_out.getvalue())
Beispiel #10
0
 def test_windows(self):
     clang2py.main([
         "clang2py", "-c", "-w", "-m", "ctypes.wintypes", "-o",
         "_winapi_gen.py", "windows.h"
     ])
     import _winapi_gen
Beispiel #11
0
 def test(self):
     clang2py.main(["clang2py", "-c", "-o", "_stdio_gen.xml", "stdio.h"])
     import _stdio_gen