Skip to content

Commit 5cf44cc

Browse files
committed
JuliaPy#184, fix JuliaPy#190: support julia options 'bindir' and 'sysimage'
1 parent 567c01b commit 5cf44cc

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

pysrc/juliacall/__init__.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ def backtrace(self):
4141

4242
CONFIG = {'inited': False}
4343

44+
julia_info_query = r"""
45+
import Libdl
46+
println(Base.Sys.BINDIR)
47+
println(abspath(Libdl.dlpath("libjulia")))
48+
println(unsafe_string(Base.JLOptions().image_file))
49+
"""
50+
4451
def init():
4552
import os
4653
import ctypes as c
@@ -86,14 +93,14 @@ def path_option(name, default=None):
8693
return
8794

8895
# Parse some more options
89-
CONFIG['opt_bindir'] = path_option('bindir') # TODO
96+
CONFIG['opt_bindir'] = path_option('bindir')
9097
CONFIG['opt_check_bounds'] = choice('check_bounds', ['yes', 'no']) # TODO
9198
CONFIG['opt_compile'] = choice('compile', ['yes', 'no', 'all', 'min']) # TODO
9299
CONFIG['opt_compiled_modules'] = choice('compiled_modules', ['yes', 'no']) # TODO
93100
CONFIG['opt_depwarn'] = choice('depwarn', ['yes', 'no', 'error']) # TODO
94101
CONFIG['opt_inline'] = choice('inline', ['yes', 'no']) # TODO
95102
CONFIG['opt_optimize'] = choice('optimize', ['0', '1', '2', '3']) # TODO
96-
CONFIG['opt_sysimage'] = path_option('sysimage') # TODO
103+
CONFIG['opt_sysimage'] = path_option('sysimage')
97104
CONFIG['opt_warn_overwrite'] = choice('warn_overwrite', ['yes', 'no']) # TODO
98105

99106
# Stop if we already initialised
@@ -109,19 +116,35 @@ def path_option(name, default=None):
109116
CONFIG['project'] = project = juliapkg.project()
110117

111118
# Find the Julia library
112-
cmd = [exepath, '--project='+project, '--startup-file=no', '-O0', '--compile=min', '-e', 'import Libdl; print(abspath(Libdl.dlpath("libjulia")))']
113-
CONFIG['libpath'] = libpath = subprocess.run(cmd, check=True, capture_output=True, encoding='utf8').stdout
119+
cmd = [exepath, '--project='+project, '--startup-file=no', '-O0', '--compile=min', '-e', julia_info_query]
120+
121+
default_bindir, default_libpath, default_sysimage = subprocess.run(cmd, check=True, capture_output=True, encoding='utf8').stdout.splitlines()
122+
CONFIG['libpath'] = libpath = default_libpath
114123
assert os.path.exists(libpath)
115124

125+
if not CONFIG.get('opt_bindir'):
126+
CONFIG['opt_bindir'] = default_bindir
127+
if not CONFIG.get("opt_sysimage"):
128+
CONFIG['opt_sysimage'] = default_sysimage
129+
116130
# Initialise Julia
117131
d = os.getcwd()
118132
try:
119133
# Open the library
120134
os.chdir(os.path.dirname(libpath))
121135
CONFIG['lib'] = lib = c.CDLL(libpath, mode=c.RTLD_GLOBAL)
122-
lib.jl_init__threading.argtypes = []
123-
lib.jl_init__threading.restype = None
124-
lib.jl_init__threading()
136+
try:
137+
init_func = lib.jl_init_with_image
138+
except AttributeError:
139+
init_func = lib.jl_init_with_image__threading
140+
141+
init_func.argtypes = [c.c_char_p, c.c_char_p]
142+
init_func.restype = None
143+
init_func(
144+
str(CONFIG['opt_bindir']).encode('utf-8'),
145+
str(CONFIG['opt_sysimage']).encode('utf-8')
146+
)
147+
125148
lib.jl_eval_string.argtypes = [c.c_char_p]
126149
lib.jl_eval_string.restype = c.c_void_p
127150
os.environ['JULIA_PYTHONCALL_LIBPTR'] = str(c.pythonapi._handle)

0 commit comments

Comments
 (0)