Skip to content

Commit

Permalink
Wrap __init__ functions of Base submodules in try/catch
Browse files Browse the repository at this point in the history
Prevents them from throwing any InitError exceptions that will not get
handled
  • Loading branch information
jdlangs committed Aug 22, 2015
1 parent 478be64 commit 419539d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 28 deletions.
29 changes: 17 additions & 12 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,24 @@ _gmp_clear_func = C_NULL
_mpfr_clear_func = C_NULL

function __init__()
if gmp_version().major != GMP_VERSION.major || gmp_bits_per_limb() != GMP_BITS_PER_LIMB
error(string("The dynamically loaded GMP library (version $(gmp_version()) with __gmp_bits_per_limb == $(gmp_bits_per_limb()))\n",
"does not correspond to the compile time version (version $GMP_VERSION with __gmp_bits_per_limb == $GMP_BITS_PER_LIMB).\n",
"Please rebuild Julia."))
end
try
if gmp_version().major != GMP_VERSION.major || gmp_bits_per_limb() != GMP_BITS_PER_LIMB
error(string("The dynamically loaded GMP library (version $(gmp_version()) with __gmp_bits_per_limb == $(gmp_bits_per_limb()))\n",
"does not correspond to the compile time version (version $GMP_VERSION with __gmp_bits_per_limb == $GMP_BITS_PER_LIMB).\n",
"Please rebuild Julia."))
end

global _gmp_clear_func = cglobal((:__gmpz_clear, :libgmp))
global _mpfr_clear_func = cglobal((:mpfr_clear, :libmpfr))
ccall((:__gmp_set_memory_functions, :libgmp), Void,
(Ptr{Void},Ptr{Void},Ptr{Void}),
cglobal(:jl_gc_counted_malloc),
cglobal(:jl_gc_counted_realloc_with_old_size),
cglobal(:jl_gc_counted_free))
global _gmp_clear_func = cglobal((:__gmpz_clear, :libgmp))
global _mpfr_clear_func = cglobal((:mpfr_clear, :libmpfr))
ccall((:__gmp_set_memory_functions, :libgmp), Void,
(Ptr{Void},Ptr{Void},Ptr{Void}),
cglobal(:jl_gc_counted_malloc),
cglobal(:jl_gc_counted_realloc_with_old_size),
cglobal(:jl_gc_counted_free))
catch ex
Base.showerror_nostdio(ex,
"WARNING: Error during initialization of module GMP:\n")
end
end

widen(::Type{Int128}) = BigInt
Expand Down
11 changes: 8 additions & 3 deletions base/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,14 @@ include("linalg/arpack.jl")
include("linalg/arnoldi.jl")

function __init__()
Base.check_blas()
if Base.blas_vendor() == :mkl
ccall((:MKL_Set_Interface_Layer, Base.libblas_name), Void, (Cint,), USE_BLAS64 ? 1 : 0)
try
Base.check_blas()
if Base.blas_vendor() == :mkl
ccall((:MKL_Set_Interface_Layer, Base.libblas_name), Void, (Cint,), USE_BLAS64 ? 1 : 0)
end
catch ex
Base.showerror_nostdio(ex,
"WARNING: Error during initialization of module LinAlg")
end
end

Expand Down
11 changes: 8 additions & 3 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ import Base.GMP: ClongMax, CulongMax, CdoubleMax
import Base.Math.lgamma_r

function __init__()
# set exponent to full range by default
set_emin!(get_emin_min())
set_emax!(get_emax_max())
try
# set exponent to full range by default
set_emin!(get_emin_min())
set_emax!(get_emax_max())
catch ex
Base.showerror_nostdio(ex,
"WARNING: Error during initialization of module MPFR:\n")
end
end

const ROUNDING_MODE = Cint[0]
Expand Down
23 changes: 14 additions & 9 deletions base/pcre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ global JIT_STACK = C_NULL
global MATCH_CONTEXT = C_NULL

function __init__()
JIT_STACK_START_SIZE = 32768
JIT_STACK_MAX_SIZE = 1048576
global JIT_STACK = ccall((:pcre2_jit_stack_create_8, PCRE_LIB), Ptr{Void},
(Cint, Cint, Ptr{Void}),
JIT_STACK_START_SIZE, JIT_STACK_MAX_SIZE, C_NULL)
global MATCH_CONTEXT = ccall((:pcre2_match_context_create_8, PCRE_LIB),
Ptr{Void}, (Ptr{Void},), C_NULL)
ccall((:pcre2_jit_stack_assign_8, PCRE_LIB), Void,
(Ptr{Void}, Ptr{Void}, Ptr{Void}), MATCH_CONTEXT, C_NULL, JIT_STACK)
try
JIT_STACK_START_SIZE = 32768
JIT_STACK_MAX_SIZE = 1048576
global JIT_STACK = ccall((:pcre2_jit_stack_create_8, PCRE_LIB), Ptr{Void},
(Cint, Cint, Ptr{Void}),
JIT_STACK_START_SIZE, JIT_STACK_MAX_SIZE, C_NULL)
global MATCH_CONTEXT = ccall((:pcre2_match_context_create_8, PCRE_LIB),
Ptr{Void}, (Ptr{Void},), C_NULL)
ccall((:pcre2_jit_stack_assign_8, PCRE_LIB), Void,
(Ptr{Void}, Ptr{Void}, Ptr{Void}), MATCH_CONTEXT, C_NULL, JIT_STACK)
catch ex
Base.showerror_nostdio(ex,
"WARNING: Error during initialization of module PCRE:\n")
end
end

# supported options for different use cases
Expand Down
9 changes: 8 additions & 1 deletion base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ randjump(r::MersenneTwister, jumps::Integer) = randjump(r, jumps, dSFMT.JPOLY1e2

## initialization

__init__() = srand()
function __init__()
try
srand()
catch ex
Base.showerror_nostdio(ex,
"WARNING: Error during initialization of module Random:\n")
end
end


## make_seed()
Expand Down
10 changes: 10 additions & 0 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ function showerror(io::IO, ex::MethodError)
show_method_candidates(io, ex)
end

#Show an error by directly calling jl_printf.
#Useful in Base submodule __init__ functions where STDERR isn't defined yet.
function showerror_nostdio(err, msg::AbstractString)
stderr_stream = ccall(:jl_stderr_stream, Ptr{Void}, ())
ccall(:jl_printf, UInt, (Ptr{Void},Cstring), stderr_stream, msg)
ccall(:jl_static_show, UInt, (Ptr{Void},Ptr{Void}), stderr_stream,
pointer_from_objref(err))
ccall(:jl_printf, UInt, (Ptr{Void},Cstring), stderr_stream, "\n")
end

const UNSHOWN_METHODS = ObjectIdDict(
which(call, Tuple{Type, Vararg{Any}}) => true
)
Expand Down
5 changes: 5 additions & 0 deletions base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ end
const version = VersionNumber(version_array...)

function __init__()
try
### Check if the linked library is compatible with the Julia code
if Libdl.dlsym(Libdl.dlopen("libcholmod"), :cholmod_version) == C_NULL
hasversion = false
Expand Down Expand Up @@ -151,6 +152,10 @@ function __init__()
unsafe_store!(cnfg, cglobal(:jl_free, Ptr{Void}), 4)
end

catch ex
Base.showerror_nostdio(ex,
"WARNING: Error during initialization of module CHOLMOD:\n")
end
end

function set_print_level(cm::Array{UInt8}, lev::Integer)
Expand Down

0 comments on commit 419539d

Please sign in to comment.