Skip to content

Instantly share code, notes, and snippets.

@tkf
Created September 11, 2018 01:59
Show Gist options
  • Save tkf/91be62b2d38b7aeb2c15d157893bca86 to your computer and use it in GitHub Desktop.
Save tkf/91be62b2d38b7aeb2c15d157893bca86 to your computer and use it in GitHub Desktop.
from ctypes import c_char_p, c_void_p
import ctypes
import os
import subprocess
def juliainfo():
out = subprocess.check_output(
["julia", "-e", """
using Libdl
println(Base.Sys.BINDIR)
println(Libdl.dlpath(string("lib", splitext(Base.julia_exename())[1])))
println(unsafe_string(Base.JLOptions().image_file))
"""],
universal_newlines=True)
return out.split("\n")[:3]
def demo():
try:
input("Please hit Ctrl-C\n")
except KeyboardInterrupt:
print("Got a KeyboardInterrupt")
bindir, libjulia_path, image_file = juliainfo()
print("Base.Sys.BINDIR =", bindir)
print("libjulia_path =", libjulia_path)
if not os.path.exists(libjulia_path):
raise RuntimeError('Julia library ("libjulia") not found! {}'
.format(libjulia_path))
# Handle to libjulia without GIL:
dll = ctypes.CDLL(libjulia_path, ctypes.RTLD_GLOBAL)
try:
jl_init_with_image = dll.jl_init_with_image
except AttributeError:
try:
jl_init_with_image = dll.jl_init_with_image__threading
except AttributeError:
raise RuntimeError(
"No libjulia entrypoint found! (tried jl_init_with_image"
" and jl_init_with_image__threading)")
jl_init_with_image.argtypes = [c_char_p, c_char_p]
jl_init_with_image = jl_init_with_image
dll.jl_eval_string.argtypes = [c_char_p]
dll.jl_eval_string.restype = c_void_p
print("calling jl_init_with_image({}, {})".format(bindir, image_file))
jl_init_with_image(bindir.encode("utf-8"), image_file.encode("utf-8"))
print("seems to work...")
dll.jl_eval_string(b"""
try
println("Please hit Ctrl-C")
readline(stdin)
catch err
@show typeof(err)
display(stacktrace(catch_backtrace()))
end
""")
print()
print()
try:
input("Please hit Ctrl-C\n")
except KeyboardInterrupt:
print("Got a KeyboardInterrupt")
if __name__ == "__main__":
demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment