def test_pycc_ctypes_lib(self): """ Test creating a C shared library object using pycc. """ unset_macosx_deployment_target() modulename = os.path.join(base_path, 'compile_with_pycc') cdll_modulename = modulename + find_shared_ending() def _cleanup(): if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) _cleanup() self.addCleanup(_cleanup) main(args=[modulename + '.py']) lib = CDLL(cdll_modulename) lib.mult.argtypes = [POINTER(c_double), c_void_p, c_void_p, c_double, c_double] lib.mult.restype = c_int lib.multf.argtypes = [POINTER(c_float), c_void_p, c_void_p, c_float, c_float] lib.multf.restype = c_int res = c_double() lib.mult(byref(res), None, None, 123, 321) self.assertEqual(res.value, 123 * 321) res = c_float() lib.multf(byref(res), None, None, 987, 321) self.assertEqual(res.value, 987 * 321)
def test_pycc_ctypes_lib(self): modulename = os.path.join(base_path, 'compile_with_pycc') cdll_modulename = modulename + find_shared_ending() if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) main(args=[modulename + '.py']) lib = CDLL(cdll_modulename) try: lib.mult.argtypes = [POINTER(c_double), c_double, c_double] lib.mult.restype = c_int lib.multf.argtypes = [POINTER(c_float), c_float, c_float] lib.multf.restype = c_int res = c_double() lib.mult(byref(res), 123, 321) print('lib.mult(123, 321) = %f' % res.value) self.assertEqual(res.value, 123 * 321) res = c_float() lib.multf(byref(res), 987, 321) print('lib.multf(987, 321) = %f' % res.value) self.assertEqual(res.value, 987 * 321) finally: del lib if os.path.exists(cdll_modulename): os.unlink(cdll_modulename)
def test_pycc_ctypes_lib(self): modulename = os.path.join(base_path, "compile_with_pycc") cdll_modulename = modulename + find_shared_ending() if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) main(args=[modulename + ".py"]) lib = CDLL(cdll_modulename) try: lib.mult.argtypes = [POINTER(c_double), c_double, c_double] lib.mult.restype = c_int lib.multf.argtypes = [POINTER(c_float), c_float, c_float] lib.multf.restype = c_int res = c_double() lib.mult(byref(res), 123, 321) print("lib.mult(123, 321) = %f" % res.value) self.assertEqual(res.value, 123 * 321) res = c_float() lib.multf(byref(res), 987, 321) print("lib.multf(987, 321) = %f" % res.value) self.assertEqual(res.value, 987 * 321) finally: del lib if os.path.exists(cdll_modulename): os.unlink(cdll_modulename)
def test_pycc_bitcode(self): """ Test creating a LLVM bitcode file using pycc. """ unset_macosx_deployment_target() modulename = os.path.join(base_path, 'compile_with_pycc') bitcode_modulename = modulename + '.bc' def _cleanup(): if os.path.exists(bitcode_modulename): os.unlink(bitcode_modulename) _cleanup() self.addCleanup(_cleanup) main(args=['--llvm', '-o', bitcode_modulename, modulename + '.py']) # Sanity check bitcode file contents with open(bitcode_modulename, "rb") as f: bc = f.read() bitcode_wrapper_magic = b'\xde\xc0\x17\x0b' bitcode_magic = b'BC\xc0\xde' self.assertTrue(bc.startswith((bitcode_magic, bitcode_wrapper_magic)), bc)
def test_pycc_pymodule(self): """ Test creating a CPython extension module using pycc. """ unset_macosx_deployment_target() modulename = os.path.join(base_path, 'compile_with_pycc') tmpdir = tempfile.gettempdir() out_modulename = (os.path.join(tmpdir, 'compiled_with_pycc') + find_shared_ending()) def _cleanup(): if os.path.exists(out_modulename): os.unlink(out_modulename) _cleanup() self.addCleanup(_cleanup) main(args=['--python', '-o', out_modulename, modulename + '.py']) sys.path.append(tmpdir) try: import compiled_with_pycc as lib try: res = lib.mult(123, 321) assert res == 123 * 321 res = lib.multf(987, 321) assert res == 987 * 321 finally: del lib finally: sys.path.remove(tmpdir)
def test_pycc_ctypes_lib(self): unset_macosx_deployment_target() modulename = os.path.join(base_path, 'compile_with_pycc') cdll_modulename = modulename + find_shared_ending() if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) main(args=[modulename + '.py']) lib = CDLL(cdll_modulename) try: lib.mult.argtypes = [POINTER(c_double), c_void_p, c_double, c_double] lib.mult.restype = c_int lib.multf.argtypes = [POINTER(c_float), c_void_p, c_float, c_float] lib.multf.restype = c_int res = c_double() lib.mult(byref(res), None, 123, 321) print('lib.mult(123, 321) = %f' % res.value) self.assertEqual(res.value, 123 * 321) res = c_float() lib.multf(byref(res), None, 987, 321) print('lib.multf(987, 321) = %f' % res.value) self.assertEqual(res.value, 987 * 321) finally: del lib if os.path.exists(cdll_modulename): os.unlink(cdll_modulename)
def test_pycc_pymodule(self): unset_macosx_deployment_target() modulename = os.path.join(base_path, 'compile_with_pycc') tmpdir = tempfile.gettempdir() print('tmpdir: %s' % tmpdir) out_modulename = (os.path.join(tmpdir, 'compiled_with_pycc') + find_shared_ending()) main(args=['--python', '-o', out_modulename, modulename + '.py']) sys.path.append(tmpdir) try: import compiled_with_pycc as lib try: res = lib.mult(123, 321) print('lib.mult(123, 321) = %f' % res) assert res == 123 * 321 res = lib.multf(987, 321) print('lib.multf(987, 321) = %f' % res) assert res == 987 * 321 finally: del lib finally: if os.path.exists(out_modulename): os.unlink(out_modulename)
def test_pycc_pymodule(self): """ Test creating a CPython extension module using pycc. """ unset_macosx_deployment_target() source = os.path.join(base_path, 'compile_with_pycc.py') modulename = 'pycc_test_pyext' out_modulename = os.path.join(self.tmpdir, modulename + find_pyext_ending()) if os.path.exists(out_modulename): os.unlink(out_modulename) main(args=['--python', '-o', out_modulename, source]) sys.path.append(self.tmpdir) try: lib = __import__(modulename) finally: sys.path.remove(self.tmpdir) try: res = lib.mult(123, 321) assert res == 123 * 321 res = lib.multf(987, 321) assert res == 987 * 321 finally: del lib
def test_pycc_ctypes_lib(self): """ Test creating a C shared library object using pycc. """ source = os.path.join(base_path, 'compile_with_pycc.py') cdll_modulename = 'test_dll_legacy' + find_shared_ending() cdll_path = os.path.join(self.tmpdir, cdll_modulename) if os.path.exists(cdll_path): os.unlink(cdll_path) main(args=['--debug', '-o', cdll_path, source]) lib = CDLL(cdll_path) lib.mult.argtypes = [POINTER(c_double), c_void_p, c_double, c_double] lib.mult.restype = c_int lib.multf.argtypes = [POINTER(c_float), c_void_p, c_float, c_float] lib.multf.restype = c_int res = c_double() lib.mult(byref(res), None, 123, 321) self.assertEqual(res.value, 123 * 321) res = c_float() lib.multf(byref(res), None, 987, 321) self.assertEqual(res.value, 987 * 321)
def test_pycc(): modulename = os.path.join(base_path, 'compile_with_pycc') cdll_modulename = modulename + find_shared_ending() if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) main(args=[modulename + '.py']) lib = CDLL(cdll_modulename) try: lib.mult.argtypes = [c_double, c_double] lib.mult.restype = c_double lib.multf.argtypes = [c_float, c_float] lib.multf.restype = c_float res = lib.mult(123, 321) print('lib.mult(123, 321) = %f', res) assert res == 123 * 321 res = lib.multf(987, 321) print('lib.multf(987, 321) = %f' % res) assert res == 987 * 321 finally: del lib if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) tmpdir = tempfile.gettempdir() print('tmpdir: %s' % tmpdir) out_modulename = (os.path.join(tmpdir, 'compiled_with_pycc') + find_shared_ending()) main(args=['--python', '-o', out_modulename, modulename + '.py']) sys.path.append(tmpdir) try: import compiled_with_pycc as lib try: res = lib.mult(123, 321) print('lib.mult(123, 321) = %f' % res) assert res == 123 * 321 res = lib.multf(987, 321) print('lib.multf(987, 321) = %f' % res) assert res == 987 * 321 finally: del lib finally: if os.path.exists(out_modulename): os.unlink(out_modulename)
def test_pycc(): modulename = os.path.join(base_path, "compile_with_pycc") cdll_modulename = modulename + find_shared_ending() if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) main(args=[modulename + ".py"]) lib = CDLL(cdll_modulename) try: lib.mult.argtypes = [c_double, c_double] lib.mult.restype = c_double lib.multf.argtypes = [c_float, c_float] lib.multf.restype = c_float res = lib.mult(123, 321) print("lib.mult(123, 321) = %f", res) assert res == 123 * 321 res = lib.multf(987, 321) print("lib.multf(987, 321) = %f" % res) assert res == 987 * 321 finally: del lib if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) tmpdir = tempfile.gettempdir() print("tmpdir: %s" % tmpdir) out_modulename = os.path.join(tmpdir, "compiled_with_pycc") + find_shared_ending() main(args=["--python", "-o", out_modulename, modulename + ".py"]) sys.path.append(tmpdir) try: import compiled_with_pycc as lib try: res = lib.mult(123, 321) print("lib.mult(123, 321) = %f" % res) assert res == 123 * 321 res = lib.multf(987, 321) print("lib.multf(987, 321) = %f" % res) assert res == 987 * 321 finally: del lib finally: if os.path.exists(out_modulename): os.unlink(out_modulename)
def test_pycc(): modulename = os.path.join(base_path, 'compile_with_pycc') cdll_modulename = modulename + find_shared_ending() if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) main(args=[modulename + '.py']) lib = CDLL(cdll_modulename) try: lib.mult.argtypes = [c_double, c_double] lib.mult.restype = c_double lib.multf.argtypes = [c_float, c_float] lib.multf.restype = c_float res = lib.mult(123, 321) print('lib.mult(123, 321) = %f', res) assert res == 123 * 321 res = lib.multf(987, 321) print('lib.multf(987, 321) = %f' % res) assert res == 987 * 321 finally: del lib if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) out_modulename = (os.path.join(base_path, 'compiled_with_pycc') + find_shared_ending()) main(args=['--python', '-o', out_modulename, modulename + '.py']) try: import numba.tests.compiled_with_pycc as lib try: res = lib.mult(123, 321) print('lib.mult(123, 321) = %f' % res) assert res == 123 * 321 res = lib.multf(987, 321) print('lib.multf(987, 321) = %f', res) assert res == 987 * 321 finally: del lib finally: if os.path.exists(out_modulename): os.unlink(out_modulename)
def test_pycc_bitcode(self): """ Test creating a LLVM bitcode file using pycc. """ modulename = os.path.join(base_path, "compile_with_pycc") bitcode_modulename = os.path.join(self.tmpdir, "test_bitcode_legacy.bc") if os.path.exists(bitcode_modulename): os.unlink(bitcode_modulename) main(args=["--debug", "--llvm", "-o", bitcode_modulename, modulename + ".py"]) # Sanity check bitcode file contents with open(bitcode_modulename, "rb") as f: bc = f.read() bitcode_wrapper_magic = b"\xde\xc0\x17\x0b" bitcode_magic = b"BC\xc0\xde" self.assertTrue(bc.startswith((bitcode_magic, bitcode_wrapper_magic)), bc)
def test_pycc_bitcode(self): """ Test creating a LLVM bitcode file using pycc. """ modulename = os.path.join(base_path, 'compile_with_pycc') bitcode_modulename = os.path.join(self.tmpdir, 'test_bitcode_legacy.bc') if os.path.exists(bitcode_modulename): os.unlink(bitcode_modulename) main(args=['--debug', '--llvm', '-o', bitcode_modulename, modulename + '.py']) # Sanity check bitcode file contents with open(bitcode_modulename, "rb") as f: bc = f.read() bitcode_wrapper_magic = b'\xde\xc0\x17\x0b' bitcode_magic = b'BC\xc0\xde' self.assertTrue(bc.startswith((bitcode_magic, bitcode_wrapper_magic)), bc)
def test_pycc_pymodule(self): """ Test creating a CPython extension module using pycc. """ self.skipTest("lack of environment can make the extension crash") source = os.path.join(base_path, "compile_with_pycc.py") modulename = "test_pyext_legacy" out_modulename = os.path.join(self.tmpdir, modulename + find_pyext_ending()) if os.path.exists(out_modulename): os.unlink(out_modulename) main(args=["--debug", "--python", "-o", out_modulename, source]) with self.check_c_ext(self.tmpdir, modulename) as lib: res = lib.multi(123, 321) self.assertPreciseEqual(res, 123 * 321) res = lib.multf(987, 321) self.assertPreciseEqual(res, 987.0 * 321.0)
def test_pycc_pymodule(self): """ Test creating a CPython extension module using pycc. """ unset_macosx_deployment_target() source = os.path.join(base_path, 'compile_with_pycc.py') modulename = 'test_pyext_legacy' out_modulename = os.path.join(self.tmpdir, modulename + find_pyext_ending()) if os.path.exists(out_modulename): os.unlink(out_modulename) main(args=['--debug', '--python', '-o', out_modulename, source]) with self.check_c_ext(self.tmpdir, modulename) as lib: res = lib.multi(123, 321) self.assertPreciseEqual(res, 123 * 321) res = lib.multf(987, 321) self.assertPreciseEqual(res, 987.0 * 321.0)
def test_pycc_pymodule(self): """ Test creating a CPython extension module using pycc. """ self.skipTest("lack of environment can make the extension crash") source = os.path.join(base_path, 'compile_with_pycc.py') modulename = 'test_pyext_legacy' out_modulename = os.path.join(self.tmpdir, modulename + find_pyext_ending()) if os.path.exists(out_modulename): os.unlink(out_modulename) main(args=['--debug', '--python', '-o', out_modulename, source]) with self.check_c_ext(self.tmpdir, modulename) as lib: res = lib.multi(123, 321) self.assertPreciseEqual(res, 123 * 321) res = lib.multf(987, 321) self.assertPreciseEqual(res, 987.0 * 321.0)
def test_pycc_bitcode(self): """ Test creating a LLVM bitcode file using pycc. """ modulename = os.path.join(base_path, "compile_with_pycc") bitcode_modulename = os.path.join(self.tmpdir, "test_bitcode_legacy.bc") if os.path.exists(bitcode_modulename): os.unlink(bitcode_modulename) main(args=[ "--debug", "--llvm", "-o", bitcode_modulename, modulename + ".py" ]) # Sanity check bitcode file contents with open(bitcode_modulename, "rb") as f: bc = f.read() bitcode_wrapper_magic = b"\xde\xc0\x17\x0b" bitcode_magic = b"BC\xc0\xde" self.assertTrue(bc.startswith((bitcode_magic, bitcode_wrapper_magic)), bc)
def test_pycc_pymodule(self): modulename = os.path.join(base_path, "compile_with_pycc") tmpdir = tempfile.gettempdir() print("tmpdir: %s" % tmpdir) out_modulename = os.path.join(tmpdir, "compiled_with_pycc") + find_shared_ending() main(args=["--python", "-o", out_modulename, modulename + ".py"]) sys.path.append(tmpdir) try: import compiled_with_pycc as lib try: res = lib.mult(123, 321) print("lib.mult(123, 321) = %f" % res) assert res == 123 * 321 res = lib.multf(987, 321) print("lib.multf(987, 321) = %f" % res) assert res == 987 * 321 finally: del lib finally: if os.path.exists(out_modulename): os.unlink(out_modulename)
from ctypes import * from numba.pycc import find_shared_ending, main is_windows = sys.platform.startswith('win32') if is_windows: raise OSError('Example does not work on Windows platforms yet.') base_path = os.path.dirname(os.path.abspath(__file__)) modulename = os.path.join(base_path, 'compile_with_pycc') cdll_modulename = modulename + find_shared_ending() if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) # Compile python module to library main(args=[modulename + '.py']) lib = CDLL(cdll_modulename) # Load library with ctypes and call mult function try: lib.mult.argtypes = [POINTER(c_double), c_double, c_double] lib.mult.restype = c_int lib.multf.argtypes = [POINTER(c_float), c_float, c_float] lib.multf.restype = c_int res = c_double() lib.mult(byref(res), 123, 321) print('lib.mult(123, 321) = %f' % res.value) res = c_float()
if __name__ == '__main__': import sys from numba.pycc import main sys.exit(main())
is_windows = sys.platform.startswith('win32') if is_windows: raise OSError('Example does not work on Windows platforms yet.') base_path = os.path.dirname(os.path.abspath(__file__)) modulename = os.path.join(base_path, 'compile_with_pycc') cdll_modulename = modulename + find_shared_ending() if os.path.exists(cdll_modulename): os.unlink(cdll_modulename) # Compile python module to library main(args=[modulename + '.py']) lib = CDLL(cdll_modulename) # Load library with ctypes and call mult function try: lib.mult.argtypes = [POINTER(c_double), c_double, c_double] lib.mult.restype = c_int lib.multf.argtypes = [POINTER(c_float), c_float, c_float] lib.multf.restype = c_int res = c_double() lib.mult(byref(res), 123, 321) print('lib.mult(123, 321) = %f' % res.value) res = c_float()