Skip to content

Commit 1e42a47

Browse files
committed
Merge pull request #12742 from phobon/init_exceptions
Follow up changes for InitError
2 parents b6a0241 + fe0337e commit 1e42a47

File tree

9 files changed

+171
-116
lines changed

9 files changed

+171
-116
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")
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+
"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(ex,
39+
"WARNING: Error during initialization of module MPFR")
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")
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")
137+
end
138+
end
132139

133140

134141
## make_seed()

base/replutil.jl

+11
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ 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_printf, UInt, (Ptr{Void},Cstring), stderr_stream, ":\n")
200+
ccall(:jl_static_show, UInt, (Ptr{Void},Ptr{Void}), stderr_stream,
201+
pointer_from_objref(err))
202+
ccall(:jl_printf, UInt, (Ptr{Void},Cstring), stderr_stream, "\n")
203+
end
204+
194205
const UNSHOWN_METHODS = ObjectIdDict(
195206
which(call, Tuple{Type, Vararg{Any}}) => true
196207
)

base/sparse/cholmod.jl

+88-76
Original file line numberDiff line numberDiff line change
@@ -63,94 +63,106 @@ end
6363
const version = VersionNumber(version_array...)
6464

6565
function __init__()
66-
### Check if the linked library is compatible with the Julia code
67-
if Libdl.dlsym(Libdl.dlopen("libcholmod"), :cholmod_version) == C_NULL
68-
hasversion = false
69-
warn("""
70-
71-
CHOLMOD version incompatibility
72-
73-
Julia was compiled with CHOLMOD version $version. It is
74-
currently linked with a version older than 2.1.0. This
75-
might cause Julia to terminate when working with sparse
76-
matrix factorizations, e.g. solving systems of equations
77-
with \\.
78-
79-
It is recommended that you use Julia with a recent version
80-
of CHOLMOD, or download the OS X or generic Linux binaries
81-
from www.julialang.org, which ship with the correct
82-
versions of all dependencies.
83-
""")
84-
else
85-
hasversion = true
86-
tmp = Array(Cint, 3)
87-
ccall((:cholmod_version, :libcholmod), Cint, (Ptr{Cint},), version_array)
88-
ccall((:jl_cholmod_version, :libsuitesparse_wrapper), Cint, (Ptr{Cint},), tmp)
89-
if tmp != version_array
66+
try
67+
### Check if the linked library is compatible with the Julia code
68+
if Libdl.dlsym(Libdl.dlopen("libcholmod"), :cholmod_version) == C_NULL
69+
hasversion = false
9070
warn("""
9171
92-
CHOLMOD version incompatibility
72+
CHOLMOD version incompatibility
9373
94-
Julia was compiled with CHOLMOD version $version. It
95-
is currently linked with a version older than
96-
$(VersionNumber(tmp...)). This might cause Julia to
97-
terminate when working with sparse matrix
98-
factorizations, e.g. solving systems of equations
99-
with \\.
74+
Julia was compiled with CHOLMOD version $version. It is
75+
currently linked with a version older than 2.1.0. This
76+
might cause Julia to terminate when working with sparse
77+
matrix factorizations, e.g. solving systems of equations
78+
with \\.
10079
101-
It is recommended that you use Julia with a recent
102-
version of CHOLMOD, or download the OS X or generic
103-
Linux binary from www.julialang.org, which ship with
104-
the correct versions of all dependencies.
80+
It is recommended that you use Julia with a recent version
81+
of CHOLMOD, or download the OS X or generic Linux binaries
82+
from www.julialang.org, which ship with the correct
83+
versions of all dependencies.
10584
""")
85+
else
86+
hasversion = true
87+
tmp = Array(Cint, 3)
88+
ccall((:cholmod_version, :libcholmod), Cint, (Ptr{Cint},), version_array)
89+
ccall((:jl_cholmod_version, :libsuitesparse_wrapper), Cint, (Ptr{Cint},), tmp)
90+
if tmp != version_array
91+
warn("""
92+
93+
CHOLMOD version incompatibility
94+
95+
Julia was compiled with CHOLMOD version $version. It
96+
is currently linked with a version older than
97+
$(VersionNumber(tmp...)). This might cause Julia to
98+
terminate when working with sparse matrix
99+
factorizations, e.g. solving systems of equations
100+
with \\.
101+
102+
It is recommended that you use Julia with a recent
103+
version of CHOLMOD, or download the OS X or generic
104+
Linux binary from www.julialang.org, which ship with
105+
the correct versions of all dependencies.
106+
""")
107+
end
106108
end
107-
end
108109

109-
intsize = Int(ccall((:jl_cholmod_sizeof_long,:libsuitesparse_wrapper),Csize_t,()))
110-
if intsize != 4length(IndexTypes)
111-
warn("""
110+
intsize = Int(ccall((:jl_cholmod_sizeof_long,:libsuitesparse_wrapper),Csize_t,()))
111+
if intsize != 4length(IndexTypes)
112+
warn("""
112113
113-
CHOLMOD integer size incompatibility
114+
CHOLMOD integer size incompatibility
114115
115-
Julia was compiled with a version of CHOLMOD that
116-
supported $(32length(IndexTypes)) bit integers. It is
117-
currently linked with version that supports $(8intsize)
118-
integers. This might cause Julia to terminate when
119-
working with sparse matrix factorizations, e.g. solving
120-
systems of equations with \\.
116+
Julia was compiled with a version of CHOLMOD that
117+
supported $(32length(IndexTypes)) bit integers. It is
118+
currently linked with version that supports $(8intsize)
119+
integers. This might cause Julia to terminate when
120+
working with sparse matrix factorizations, e.g. solving
121+
systems of equations with \\.
121122
122-
This problem can be fixed by modifying the Julia build
123-
configuration or by downloading the OS X or generic
124-
Linux binary from www.julialang.org, which include
125-
the correct versions of all dependencies.
126-
""")
127-
end
123+
This problem can be fixed by modifying the Julia build
124+
configuration or by downloading the OS X or generic
125+
Linux binary from www.julialang.org, which include
126+
the correct versions of all dependencies.
127+
""")
128+
end
128129

129-
### Initiate CHOLMOD
130-
### The common struct. Controls the type of factorization and keeps pointers
131-
### to temporary memory.
132-
global const commonStruct = fill(0xff, common_size)
133-
134-
global const common_supernodal = convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[4] + 1))
135-
global const common_final_ll = convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[7] + 1))
136-
global const common_print = convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[13] + 1))
137-
global const common_itype = convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[18] + 1))
138-
global const common_dtype = convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[19] + 1))
139-
global const common_nmethods = convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[15] + 1))
140-
global const common_postorder = convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[17] + 1))
141-
142-
start(commonStruct) # initializes CHOLMOD
143-
set_print_level(commonStruct, 0) # no printing from CHOLMOD by default
144-
145-
# Register gc tracked allocator if CHOLMOD is new enough
146-
if hasversion && version >= v"3.0.0"
147-
cnfg = cglobal((:SuiteSparse_config, :libsuitesparseconfig), Ptr{Void})
148-
unsafe_store!(cnfg, cglobal(:jl_malloc, Ptr{Void}), 1)
149-
unsafe_store!(cnfg, cglobal(:jl_calloc, Ptr{Void}), 2)
150-
unsafe_store!(cnfg, cglobal(:jl_realloc, Ptr{Void}), 3)
151-
unsafe_store!(cnfg, cglobal(:jl_free, Ptr{Void}), 4)
152-
end
130+
### Initiate CHOLMOD
131+
### The common struct. Controls the type of factorization and keeps pointers
132+
### to temporary memory.
133+
global const commonStruct = fill(0xff, common_size)
134+
135+
global const common_supernodal =
136+
convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[4] + 1))
137+
global const common_final_ll =
138+
convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[7] + 1))
139+
global const common_print =
140+
convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[13] + 1))
141+
global const common_itype =
142+
convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[18] + 1))
143+
global const common_dtype =
144+
convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[19] + 1))
145+
global const common_nmethods =
146+
convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[15] + 1))
147+
global const common_postorder =
148+
convert(Ptr{Cint}, pointer(commonStruct, cholmod_com_offsets[17] + 1))
149+
150+
start(commonStruct) # initializes CHOLMOD
151+
set_print_level(commonStruct, 0) # no printing from CHOLMOD by default
152+
153+
# Register gc tracked allocator if CHOLMOD is new enough
154+
if hasversion && version >= v"3.0.0"
155+
cnfg = cglobal((:SuiteSparse_config, :libsuitesparseconfig), Ptr{Void})
156+
unsafe_store!(cnfg, cglobal(:jl_malloc, Ptr{Void}), 1)
157+
unsafe_store!(cnfg, cglobal(:jl_calloc, Ptr{Void}), 2)
158+
unsafe_store!(cnfg, cglobal(:jl_realloc, Ptr{Void}), 3)
159+
unsafe_store!(cnfg, cglobal(:jl_free, Ptr{Void}), 4)
160+
end
153161

162+
catch ex
163+
Base.showerror_nostdio(ex,
164+
"WARNING: Error during initialization of module CHOLMOD")
165+
end
154166
end
155167

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

src/toplevel.c

+2-7
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,8 @@ jl_value_t *jl_parse_eval_all(const char *fname, size_t len)
587587
jl_rethrow();
588588
}
589589
else {
590-
if(jl_typeis(jl_exception_in_transit, jl_initerror_type)) {
591-
jl_rethrow();
592-
}
593-
else {
594-
jl_rethrow_other(jl_new_struct(jl_loaderror_type, fn, ln,
595-
jl_exception_in_transit));
596-
}
590+
jl_rethrow_other(jl_new_struct(jl_loaderror_type, fn, ln,
591+
jl_exception_in_transit));
597592
}
598593
}
599594
jl_stop_parsing();

test/core.jl

+15-5
Original file line numberDiff line numberDiff line change
@@ -692,12 +692,22 @@ end
692692
@test names(Module(:anonymous, false), true, true) == [:anonymous]
693693

694694
# exception from __init__()
695-
@test_throws InitError include_string(
696-
"""
697-
module TestInitError
698-
__init__() = error()
695+
let didthrow =
696+
try
697+
include_string(
698+
"""
699+
module TestInitError
700+
__init__() = error()
701+
end
702+
""")
703+
false
704+
catch ex
705+
@test isa(ex, LoadError)
706+
@test isa(ex.error, InitError)
707+
true
699708
end
700-
""")
709+
@test didthrow
710+
end
701711

702712
# issue #7307
703713
function test7307(a, ret)

0 commit comments

Comments
 (0)