Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message #72

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions jnumpy/InitTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import UUIDs

@nospecialize

const Error_Logger = IOBuffer()

function check_if_typython_installed(typython_dir::AbstractString)
VERSION >= v"1.9" && error("Support for Julia 1.9 is coming soon.")
VERSION < v"1.6" && error("TyPython works for Julia >= 1.6!")
Expand All @@ -23,11 +25,16 @@ function _develop_typython(typython_dir::AbstractString)
nothing
end

function setup_environment(typython_dir::AbstractString)
if !check_if_typython_installed(typython_dir)
_develop_typython(typython_dir)
Pkg.resolve()
Pkg.instantiate()
function setup_environment(typython_dir::AbstractString; log_error::Bool=false)
try
if !check_if_typython_installed(typython_dir)
_develop_typython(typython_dir)
Pkg.resolve()
Pkg.instantiate()
end
catch e
log_error && Base.showerror(Error_Logger, e, catch_backtrace())
rethrow()
end
nothing
end
Expand All @@ -37,20 +44,31 @@ end
The precompiled file goes wrong for unknown reason.
Removing and re-adding works.
"""
@noinline function force_resolve(typython_dir::AbstractString)
@noinline function force_resolve(typython_dir::AbstractString; log_error::Bool=false)
try
Pkg.rm("TyPython", io=devnull)
catch
end
Pkg.develop(path=typython_dir, io=devnull)
Pkg.resolve()
Pkg.instantiate()

try
Pkg.develop(path=typython_dir, io=devnull)
Pkg.resolve()
Pkg.instantiate()
catch e
log_error && Base.showerror(Error_Logger, e, catch_backtrace())
rethrow()
end
nothing
end

@noinline function activate_project(project_dir::AbstractString, typython_dir::AbstractString)
@noinline function activate_project(project_dir::AbstractString, typython_dir::AbstractString; log_error::Bool=false)
Pkg.activate(project_dir, io=devnull)
force_resolve(typython_dir)
force_resolve(typython_dir, log_error=log_error)
nothing
end

function show_error_log()
println(stderr, String(take!(Error_Logger)))
nothing
end

Expand Down
4 changes: 2 additions & 2 deletions jnumpy/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ def activate_project_checked(project_dir: str):
exec_julia(
f"InitTools.activate_project({escape_to_julia_rawstr(project_dir)},"
f"{escape_to_julia_rawstr(TyPython_directory)})",
use_gil=False,
use_gil=True,
)
try:
yield
finally:
exec_julia(
f"InitTools.activate_project({escape_to_julia_rawstr(SessionCtx.DEFAULT_PROJECT_DIR)},"
f"{escape_to_julia_rawstr(TyPython_directory)})",
use_gil=False,
use_gil=True,
)


Expand Down
17 changes: 13 additions & 4 deletions jnumpy/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,19 @@ def _eval_jl(x: str, use_gil: bool):
)
except JuliaError:
pass
exec_julia(
f"InitTools.force_resolve({escape_to_julia_rawstr(TyPython_directory)})",
use_gil=False,
)
try:
# log julia's error in a IOBuffer, because we can't redirect error to python in this step
exec_julia(
f"InitTools.force_resolve({escape_to_julia_rawstr(TyPython_directory)}, log_error=true)",
use_gil=False,
)
except JuliaError:
# show error message in IOBuffer
exec_julia(
"InitTools.show_error_log()",
use_gil=False,
)
raise JuliaError("failed to setup julia environment, check the julia error message above.")
try:
exec_julia(
rf"""
Expand Down