-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy path__init__.py
150 lines (129 loc) · 4.99 KB
/
__init__.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# This module gets modified by PythonCall when it is loaded, e.g. to include Core, Base
# and Main modules.
__version__ = '0.9.0'
_newmodule = None
def newmodule(name):
"A new module with the given name."
global _newmodule
if _newmodule is None:
_newmodule = Main.seval("name -> (n1=Symbol(name); n2=gensym(n1); Main.@eval(module $n2; module $n1; end; end); Main.@eval $n2.$n1)")
return _newmodule(name)
_convert = None
def convert(T, x):
"Convert x to a Julia T."
global _convert
if _convert is None:
_convert = PythonCall.seval("pyjlcallback((T,x)->pyjl(pyconvert(pyjlvalue(T)::Type,x)))")
return _convert(T, x)
class JuliaError(Exception):
"An error arising in Julia code."
def __init__(self, exception, backtrace=None):
super().__init__(exception, backtrace)
def __str__(self):
e = self.exception
b = self.backtrace
if b is None:
return Base.sprint(Base.showerror, e)
else:
return Base.sprint(Base.showerror, e, b)
@property
def exception(self):
return self.args[0]
@property
def backtrace(self):
return self.args[1]
CONFIG = {'inited': False}
def init():
import os
import ctypes as c
import sys
import subprocess
def option(name, default=None):
"""Get an option.
Options can be set as command line arguments '-X juliacall_{name}={value}' or as
environment variables 'PYTHON_JULIACALL_{NAME}={value}'.
"""
k = 'juliacall_'+name.lower()
v = sys._xoptions.get(k)
if v is not None:
return v
k = 'PYTHON_JULIACALL_'+name.upper()
v = os.getenv(k)
if v is not None:
return v
return default
def choice(name, choices, default=None):
k = option(name)
if k is None:
return default
if k in choices:
if isinstance(k, dict):
return choices[k]
else:
return k
raise ValueError(f'invalid value for option: JULIACALL_{name.upper()}={k}, expecting one of {", ".join(choices)}')
def path_option(name, default=None):
path = option(name)
if path is not None:
return os.path.abspath(path)
return default
# Determine if we should skip initialising.
CONFIG['init'] = choice('init', ['yes', 'no'], default='yes') == 'yes'
if not CONFIG['init']:
return
# Parse some more options
CONFIG['opt_bindir'] = path_option('bindir') # TODO
CONFIG['opt_check_bounds'] = choice('check_bounds', ['yes', 'no']) # TODO
CONFIG['opt_compile'] = choice('compile', ['yes', 'no', 'all', 'min']) # TODO
CONFIG['opt_compiled_modules'] = choice('compiled_modules', ['yes', 'no']) # TODO
CONFIG['opt_depwarn'] = choice('depwarn', ['yes', 'no', 'error']) # TODO
CONFIG['opt_inline'] = choice('inline', ['yes', 'no']) # TODO
CONFIG['opt_optimize'] = choice('optimize', ['0', '1', '2', '3']) # TODO
CONFIG['opt_sysimage'] = path_option('sysimage') # TODO
CONFIG['opt_warn_overwrite'] = choice('warn_overwrite', ['yes', 'no']) # TODO
# Stop if we already initialised
if CONFIG['inited']:
return
# we don't import this at the top level because it is not required when juliacall is
# loaded by PythonCall and won't be available
import juliapkg
# Find the Julia executable and project
CONFIG['exepath'] = exepath = juliapkg.executable()
CONFIG['project'] = project = juliapkg.project()
# Find the Julia library
cmd = [exepath, '--project='+project, '--startup-file=no', '-O0', '--compile=min', '-e', 'import Libdl; print(abspath(Libdl.dlpath("libjulia")))']
CONFIG['libpath'] = libpath = subprocess.run(cmd, check=True, capture_output=True, encoding='utf8').stdout
assert os.path.exists(libpath)
# Initialise Julia
d = os.getcwd()
try:
# Open the library
os.chdir(os.path.dirname(libpath))
CONFIG['lib'] = lib = c.CDLL(libpath)
lib.jl_init__threading.argtypes = []
lib.jl_init__threading.restype = None
lib.jl_init__threading()
lib.jl_eval_string.argtypes = [c.c_char_p]
lib.jl_eval_string.restype = c.c_void_p
os.environ['JULIA_PYTHONCALL_LIBPTR'] = str(c.pythonapi._handle)
os.environ['JULIA_PYTHONCALL_EXE'] = sys.executable or ''
os.environ['JULIA_PYTHONCALL_PROJECT'] = project
script = '''
try
import Pkg
Pkg.activate(ENV["JULIA_PYTHONCALL_PROJECT"], io=devnull)
import PythonCall
catch err
print(stderr, "ERROR: ")
showerror(stderr, err, catch_backtrace())
flush(stderr)
rethrow()
end
'''
res = lib.jl_eval_string(script.encode('utf8'))
if res is None:
raise Exception('PythonCall.jl did not start properly')
finally:
os.chdir(d)
CONFIG['inited'] = True
init()