Repository navigation
Expand file tree
/
Copy pathpycc_example.py
More file actions
74 lines (57 loc) · 1.93 KB
/
Copy pathpycc_example.py
File metadata and controls
74 lines (57 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import tempfile
import sys
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()
lib.multf(byref(res), 987, 321)
print('lib.multf(987, 321) = %f' % res.value)
finally:
del lib
if os.path.exists(cdll_modulename):
os.unlink(cdll_modulename)
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())
# Compile python module to python extension
main(args=['--python', '-o', out_modulename, modulename + '.py'])
# Load compiled extension and call mult function
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)