Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors in display functions more gracefully and isolate full html pages #285

Merged
merged 5 commits into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 42 additions & 10 deletions R/execution.r
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ execute = function(request) {

err <<- list()
nframe <- NULL # find out stack depth in notebook cell

tryCatch(evaluate(
'stop()',
stop_on_error = 1L,
Expand All @@ -174,25 +174,57 @@ execute = function(request) {
handle_message <- identity
handle_warning <- identity
} else {
handle_display_error <- function(e){
# This is used with withCallingHandler and only has two additional
# calls at the end instead of the 3 for tryCatch... (-2 at the end)
# we also remove the tryCatch and mime2repr stuff at the head of the callstack (+7)
calls <- head(sys.calls()[-seq_len(nframe + 7L)], -2)
stack_info <- format_stack(calls)
msg <- sprintf('ERROR while rich displaying an object: %s\nTraceback:\n%s\n',
toString(e),
paste(stack_info, collapse='\n'))
log_debug(msg)
if (!silent) {
send_response('stream', request, 'iopub', list(
name = 'stderr',
text = msg))
}
}
handle_value <- function(obj) {
data <- namedlist()
metadata <- namedlist()

data[['text/plain']] <- repr_text(obj)

# Only send a response when there is regular console output
if (nchar(data[['text/plain']]) > 0) {
if (getOption('jupyter.rich_display')) {
tryCatch({
for (mime in getOption('jupyter.display_mimetypes')) {
for (mime in getOption('jupyter.display_mimetypes')) {
# Use withCallingHandlers as that shows the inner stacktrace:
# https://stackoverflow.com/questions/15282471/get-stack-trace-on-trycatched-error-in-r
# the tryCatch is still needed to prevent the error from showing
# up outside further up the stack :-/
tryCatch(withCallingHandlers({
r <- mime2repr[[mime]](obj)
if (!is.null(r)) data[[mime]] <- r
}
}, error = handle_error)
if (!is.null(r)) {
data[[mime]] <- r
# Isolating full html pages (putting them in an iframe)
if (identical(mime, 'text/html')) {
if (grepl("<html.*>", r, ignore.case = TRUE)){
jupyter_debug("Found full html page: %s", strtrim(r, 100))
metadata[[mime]] <- list(isolated = TRUE)
}
}
}
}, error = handle_display_error),
error = function(x) {})
}
}

send_response('display_data', request, 'iopub', list(
data = data,
metadata = namedlist() ))
}
log_debug("Sending display_data: %s", paste(capture.output(str(data)), collapse = "\n"))
send_response('display_data', request, 'iopub', list(
data = data,
metadata = metadata))
}

stream <- function(output, streamname) {
Expand Down
10 changes: 10 additions & 0 deletions test_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class InstallspecTests(jkt.KernelTests):

code_hello_world = 'print("hello, world")'

class DisplaySystem(jkt.KernelTests):
kernel_name = 'ir'

language_name = 'R'

code_display_data = [
{'code': 'options(jupyter.rich_display = FALSE);cat("a")', 'mime': {'text/plain':'a'}},
{'code': '"a"', 'mime': {'text/plain':'"a"'}},
{'code': '1:3', 'mime': {'text/plain':'[1] 1 2 3'}},
]

if __name__ == '__main__':
unittest.main()