From fa7a2b41ded46afd4e93b4eb648ebdbd58cd9b97 Mon Sep 17 00:00:00 2001 From: songjhaha Date: Thu, 29 Sep 2022 13:53:20 +0800 Subject: [PATCH 1/2] improve error message --- jnumpy/InitTools.jl | 40 +++++++++++++++++++++++++++++----------- jnumpy/apis.py | 4 ++-- jnumpy/init.py | 17 +++++++++++++---- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/jnumpy/InitTools.jl b/jnumpy/InitTools.jl index 63d557a..65926df 100644 --- a/jnumpy/InitTools.jl +++ b/jnumpy/InitTools.jl @@ -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!") @@ -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(e) end nothing end @@ -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(e) + 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 diff --git a/jnumpy/apis.py b/jnumpy/apis.py index c720dca..9c95624 100644 --- a/jnumpy/apis.py +++ b/jnumpy/apis.py @@ -44,7 +44,7 @@ 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 @@ -52,7 +52,7 @@ def activate_project_checked(project_dir: str): 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, ) diff --git a/jnumpy/init.py b/jnumpy/init.py index 148a763..d7c32dd 100644 --- a/jnumpy/init.py +++ b/jnumpy/init.py @@ -206,10 +206,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""" From 111c89281322d0507f650768737f6520a3e38b5c Mon Sep 17 00:00:00 2001 From: songjhaha Date: Fri, 30 Sep 2022 13:48:58 +0800 Subject: [PATCH 2/2] fix rethrow --- jnumpy/InitTools.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jnumpy/InitTools.jl b/jnumpy/InitTools.jl index 65926df..017516a 100644 --- a/jnumpy/InitTools.jl +++ b/jnumpy/InitTools.jl @@ -34,7 +34,7 @@ function setup_environment(typython_dir::AbstractString; log_error::Bool=false) end catch e log_error && Base.showerror(Error_Logger, e, catch_backtrace()) - rethrow(e) + rethrow() end nothing end @@ -56,7 +56,7 @@ Removing and re-adding works. Pkg.instantiate() catch e log_error && Base.showerror(Error_Logger, e, catch_backtrace()) - rethrow(e) + rethrow() end nothing end