Skip to content

Commit 13dacd3

Browse files
committed
Wrap __init__ functions of Base submodules in try/catch
Prevents them from throwing any InitError exceptions that will not get handled
1 parent e5f3829 commit 13dacd3

File tree

7 files changed

+70
-28
lines changed

7 files changed

+70
-28
lines changed

base/gmp.jl

+17-12
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,24 @@ _gmp_clear_func = C_NULL
5353
_mpfr_clear_func = C_NULL
5454

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

62-
global _gmp_clear_func = cglobal((:__gmpz_clear, :libgmp))
63-
global _mpfr_clear_func = cglobal((:mpfr_clear, :libmpfr))
64-
ccall((:__gmp_set_memory_functions, :libgmp), Void,
65-
(Ptr{Void},Ptr{Void},Ptr{Void}),
66-
cglobal(:jl_gc_counted_malloc),
67-
cglobal(:jl_gc_counted_realloc_with_old_size),
68-
cglobal(:jl_gc_counted_free))
63+
global _gmp_clear_func = cglobal((:__gmpz_clear, :libgmp))
64+
global _mpfr_clear_func = cglobal((:mpfr_clear, :libmpfr))
65+
ccall((:__gmp_set_memory_functions, :libgmp), Void,
66+
(Ptr{Void},Ptr{Void},Ptr{Void}),
67+
cglobal(:jl_gc_counted_malloc),
68+
cglobal(:jl_gc_counted_realloc_with_old_size),
69+
cglobal(:jl_gc_counted_free))
70+
catch ex
71+
Base.showerror_nostdio(ex,
72+
"WARNING: Error during initialization of module GMP:\n")
73+
end
6974
end
7075

7176
widen(::Type{Int128}) = BigInt

base/linalg.jl

+8-3
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,14 @@ include("linalg/arpack.jl")
236236
include("linalg/arnoldi.jl")
237237

238238
function __init__()
239-
Base.check_blas()
240-
if Base.blas_vendor() == :mkl
241-
ccall((:MKL_Set_Interface_Layer, Base.libblas_name), Void, (Cint,), USE_BLAS64 ? 1 : 0)
239+
try
240+
Base.check_blas()
241+
if Base.blas_vendor() == :mkl
242+
ccall((:MKL_Set_Interface_Layer, Base.libblas_name), Void, (Cint,), USE_BLAS64 ? 1 : 0)
243+
end
244+
catch ex
245+
#Base.showerror_nostdio(ex,
246+
warn( "WARNING: Error during initialization of module LinAlg")
242247
end
243248
end
244249

base/mpfr.jl

+8-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ import Base.GMP: ClongMax, CulongMax, CdoubleMax
3030
import Base.Math.lgamma_r
3131

3232
function __init__()
33-
# set exponent to full range by default
34-
set_emin!(get_emin_min())
35-
set_emax!(get_emax_max())
33+
try
34+
# set exponent to full range by default
35+
set_emin!(get_emin_min())
36+
set_emax!(get_emax_max())
37+
catch ex
38+
Base.showerror_nostdio(
39+
"WARNING: Error during initialization of module MPFR:\n", ex)
40+
end
3641
end
3742

3843
const ROUNDING_MODE = Cint[0]

base/pcre.jl

+14-9
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ global JIT_STACK = C_NULL
1212
global MATCH_CONTEXT = C_NULL
1313

1414
function __init__()
15-
JIT_STACK_START_SIZE = 32768
16-
JIT_STACK_MAX_SIZE = 1048576
17-
global JIT_STACK = ccall((:pcre2_jit_stack_create_8, PCRE_LIB), Ptr{Void},
18-
(Cint, Cint, Ptr{Void}),
19-
JIT_STACK_START_SIZE, JIT_STACK_MAX_SIZE, C_NULL)
20-
global MATCH_CONTEXT = ccall((:pcre2_match_context_create_8, PCRE_LIB),
21-
Ptr{Void}, (Ptr{Void},), C_NULL)
22-
ccall((:pcre2_jit_stack_assign_8, PCRE_LIB), Void,
23-
(Ptr{Void}, Ptr{Void}, Ptr{Void}), MATCH_CONTEXT, C_NULL, JIT_STACK)
15+
try
16+
JIT_STACK_START_SIZE = 32768
17+
JIT_STACK_MAX_SIZE = 1048576
18+
global JIT_STACK = ccall((:pcre2_jit_stack_create_8, PCRE_LIB), Ptr{Void},
19+
(Cint, Cint, Ptr{Void}),
20+
JIT_STACK_START_SIZE, JIT_STACK_MAX_SIZE, C_NULL)
21+
global MATCH_CONTEXT = ccall((:pcre2_match_context_create_8, PCRE_LIB),
22+
Ptr{Void}, (Ptr{Void},), C_NULL)
23+
ccall((:pcre2_jit_stack_assign_8, PCRE_LIB), Void,
24+
(Ptr{Void}, Ptr{Void}, Ptr{Void}), MATCH_CONTEXT, C_NULL, JIT_STACK)
25+
catch ex
26+
Base.showerror_nostdio(ex,
27+
"WARNING: Error during initialization of module PCRE:\n")
28+
end
2429
end
2530

2631
# supported options for different use cases

base/random.jl

+8-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,14 @@ randjump(r::MersenneTwister, jumps::Integer) = randjump(r, jumps, dSFMT.JPOLY1e2
128128

129129
## initialization
130130

131-
__init__() = srand()
131+
function __init__()
132+
try
133+
srand()
134+
catch ex
135+
Base.showerror_nostdio(ex,
136+
"WARNING: Error during initialization of module Random:\n")
137+
end
138+
end
132139

133140

134141
## make_seed()

base/replutil.jl

+10
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ function showerror(io::IO, ex::MethodError)
191191
show_method_candidates(io, ex)
192192
end
193193

194+
#Show an error by directly calling jl_printf.
195+
#Useful in Base submodule __init__ functions where STDERR isn't defined yet.
196+
function showerror_nostdio(err, msg::AbstractString)
197+
stderr_stream = ccall(:jl_stderr_stream, Ptr{Void}, ())
198+
ccall(:jl_printf, UInt, (Ptr{Void},Cstring), stderr_stream, msg)
199+
ccall(:jl_static_show, UInt, (Ptr{Void},Ptr{Void}), stderr_stream,
200+
pointer_from_objref(err))
201+
ccall(:jl_printf, UInt, (Ptr{Void},Cstring), stderr_stream, "\n")
202+
end
203+
194204
const UNSHOWN_METHODS = ObjectIdDict(
195205
which(call, Tuple{Type, Vararg{Any}}) => true
196206
)

base/sparse/cholmod.jl

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ end
6363
const version = VersionNumber(version_array...)
6464

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

155+
catch ex
156+
Base.showerror_nostdio(ex,
157+
"WARNING: Error during initialization of module CHOLMOD:\n")
158+
end
154159
end
155160

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

0 commit comments

Comments
 (0)