Skip to content

Commit

Permalink
general clean up, @test_throws support
Browse files Browse the repository at this point in the history
  • Loading branch information
samoconnor committed Apr 18, 2016
1 parent 706fe28 commit 28e3e88
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion base/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ end

function eval_user_input(ast::ANY, backend::REPLBackend)
iserr, lasterr, bt = false, (), nothing
Base.enable_catch_fatal()
while true
Base.set_fatal_eh()
try
if iserr
put!(backend.response_channel, (lasterr, bt))
Expand Down
12 changes: 8 additions & 4 deletions base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,20 @@ macro assert(ex, msgs...)
:($(esc(ex)) ? $(nothing) : throw(Main.Base.AssertionError($msg)))
end

## fatal errors ##

isfatal(error) = false
if isdefined(Main, :Base)
isfatal(::StackOverflowError) = true
isfatal(::OutOfMemoryError) = true
isfatal(::UndefVarError) = true
end

set_fatal_eh() = ccall(:jl_set_fatal_eh, Void, ())
rethrow_fatal() = ccall(:jl_rethrow_fatal, Void, ())
enable_catch_fatal() = ccall(:jl_enable_catch_fatal, Void, ())
disable_catch_fatal() = ccall(:jl_disable_catch_fatal, Void, ())
if isdefined(Main, :Base)
rethrow_if_fatal(error) = isfatal(error) && ccall(:jl_rethrow_fatal, Void, ())
else
rethrow_if_fatal(error) = nothing
end


"""
Expand Down
11 changes: 8 additions & 3 deletions base/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,14 @@ macro test_throws(extype, ex)
orig_ex = Expr(:quote,ex)
result = quote
try
Returned($(esc(ex)))
catch _e
Threw(_e, nothing)
Base.enable_catch_fatal()
try
Returned($(esc(ex)))
catch _e
Threw(_e, nothing)
end
finally
Base.disable_catch_fatal()
end
end
Base.remove_linenums!(result)
Expand Down
1 change: 1 addition & 0 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ JL_DLLEXPORT void jl_enter_handler(jl_handler_t *eh)
JL_SIGATOMIC_BEGIN();
eh->prev = jl_current_task->eh;
eh->gcstack = jl_pgcstack;
eh->catch_fatal = 0;
#ifdef JULIA_ENABLE_THREADING
eh->gc_state = jl_gc_state();
eh->locks_len = jl_current_task->locks.len;
Expand Down
9 changes: 3 additions & 6 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -3078,9 +3078,8 @@ f(x) = yt(x)
;; (enter L) - push handler with catch block at label L
;; (leave n) - pop N exception handlers
((trycatch)
(let ((catch (make-label))
(notfatal (make-label))
(endl (make-label)))
(let ((catch (make-label))
(endl (make-label)))
(emit `(enter ,catch))
(set! handler-level (+ handler-level 1))
(let* ((v1 (compile (cadr e)
Expand All @@ -3096,9 +3095,7 @@ f(x) = yt(x)
(set! handler-level (- handler-level 1))
(mark-label catch)
(emit `(leave 1))
(emit `(gotoifnot ,`(call (top isfatal) ,'(the_exception)) ,notfatal))
(emit `(call (top rethrow_fatal)))
(mark-label notfatal)
(emit `(call (top rethrow_if_fatal) ,'(the_exception)))
(let ((v2 (compile (caddr e) break-labels value tail)))
(if val (emit `(= ,val ,v2)))
(if endl (mark-label endl))
Expand Down
6 changes: 3 additions & 3 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ typedef struct _jl_handler_t {
jl_gcframe_t *gcstack;
struct _jl_handler_t *prev;
int8_t gc_state;
int8_t catch_fatal;
#ifdef JULIA_ENABLE_THREADING
size_t locks_len;
#endif
Expand All @@ -1404,8 +1405,6 @@ typedef struct _jl_task_t {

// current exception handler
jl_handler_t *eh;
// fatal exception handler
jl_handler_t *fatal_eh;
// saved gc stack top for context switches
jl_gcframe_t *gcstack;
// current module, or NULL if this task has not set one
Expand Down Expand Up @@ -1436,7 +1435,8 @@ JL_DLLEXPORT jl_value_t *jl_switchto(jl_task_t *t, jl_value_t *arg);
JL_DLLEXPORT void JL_NORETURN jl_throw(jl_value_t *e);
JL_DLLEXPORT void JL_NORETURN jl_rethrow(void);
JL_DLLEXPORT void JL_NORETURN jl_rethrow_other(jl_value_t *e);
JL_DLLEXPORT void jl_set_fatal_eh(void);
JL_DLLEXPORT void jl_enable_catch_fatal(void);
JL_DLLEXPORT void jl_disable_catch_fatal(void);
JL_DLLEXPORT void jl_rethrow_fatal(void);

#ifdef JULIA_ENABLE_THREADING
Expand Down
28 changes: 18 additions & 10 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,21 +526,31 @@ JL_DLLEXPORT void jl_rethrow(void)
throw_internal(jl_exception_in_transit);
}

JL_DLLEXPORT void jl_set_fatal_eh(void)
JL_DLLEXPORT void jl_rethrow_other(jl_value_t *e)
{
jl_current_task->fatal_eh = jl_current_task->eh;
throw_internal(e);
}

JL_DLLEXPORT void jl_rethrow_fatal(void)
JL_DLLEXPORT void jl_enable_catch_fatal(void)
{
if (jl_current_task->eh != jl_current_task->fatal_eh) {
jl_rethrow();
}
assert(jl_current_task->eh);
jl_current_task->eh->catch_fatal = 1;
}

JL_DLLEXPORT void jl_rethrow_other(jl_value_t *e)
JL_DLLEXPORT void jl_disable_catch_fatal(void)
{
throw_internal(e);
assert(jl_current_task->eh);
jl_current_task->eh->catch_fatal = 0;
}

JL_DLLEXPORT void jl_rethrow_fatal(void)
{
if (jl_current_task
&& jl_current_task->eh
&& jl_current_task->eh->catch_fatal) {
return;
}
jl_rethrow();
}

JL_DLLEXPORT jl_task_t *jl_new_task(jl_function_t *start, size_t ssize)
Expand All @@ -566,7 +576,6 @@ JL_DLLEXPORT jl_task_t *jl_new_task(jl_function_t *start, size_t ssize)
t->backtrace = jl_nothing;
// there is no active exception handler available on this stack yet
t->eh = NULL;
t->fatal_eh = NULL;
t->gcstack = NULL;
t->stkbuf = NULL;
t->tid = 0;
Expand Down Expand Up @@ -674,7 +683,6 @@ void jl_init_root_task(void *stack, size_t ssize)
jl_current_task->exception = jl_nothing;
jl_current_task->backtrace = jl_nothing;
jl_current_task->eh = NULL;
jl_current_task->fatal_eh = NULL;
jl_current_task->gcstack = NULL;
jl_current_task->tid = ti_tid;
#ifdef JULIA_ENABLE_THREADING
Expand Down
4 changes: 4 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1103,12 +1103,14 @@ type I2619{T}
end
bad2619 = false
function i2619()
Base.enable_catch_fatal()
global e2619 = try
I2619{Float64}(0.0f)
global bad2619 = true
catch _e
_e
end
Base.disable_catch_fatal()
end
i2619()
@test !bad2619
Expand Down Expand Up @@ -2274,11 +2276,13 @@ OLD_STDOUT = STDOUT
file = open(tempname(), "w")
redirect_stdout(file)
versioninfo()
Base.enable_catch_fatal()
try
type Foo{T}
val::Bar{T}
end
end
Base.disable_catch_fatal()
gc()
redirect_stdout(OLD_STDOUT)
close(file)
Expand Down
2 changes: 2 additions & 0 deletions test/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ macro except_str(expr, err_type)
return quote
let
local err
Base.enable_catch_fatal()
try
$(esc(expr))
catch err
end
Base.disable_catch_fatal()
@test typeof(err) === $(esc(err_type))
buff = IOBuffer()
showerror(buff, err)
Expand Down
8 changes: 1 addition & 7 deletions test/simdloop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ let j=4

# Index that is local to loop
@simd for simd_loop_local=1:0 end
simd_loop_local_present = true
try
simd_loop_local += 1
catch
simd_loop_local_present = false
end
@test !simd_loop_local_present
@test_throws UndefVarError simd_loop_local += 1
end

import Base.SimdLoop.SimdError
Expand Down
2 changes: 1 addition & 1 deletion test/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ let ct = current_task()
yieldto(@task yieldto(ct))
@test catch_backtrace() == StackFrame[]

@noinline bad_function() = throw(UndefVarError(:nonexistent))
@noinline bad_function() = error("error")
@noinline function try_stacktrace()
try
bad_function()
Expand Down

0 comments on commit 28e3e88

Please sign in to comment.