Skip to content

Commit

Permalink
knitr eng: format captured exceptions
Browse files Browse the repository at this point in the history
Closes #1520
  • Loading branch information
t-kalinowski committed Jan 29, 2024
1 parent 0e5676e commit 9580b03
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions R/knitr-engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,25 @@ eng_python <- function(options) {
compile_mode <- if (suppress) "exec" else "single"

# run code and capture output
captured_stdout <- if (capture_errors)
tryCatch(py_compile_eval(snippet, compile_mode), error = identity)
captured_stdout <- if (capture_errors) {
tryCatch(
py_compile_eval(snippet, compile_mode),
error = function(e) {

# if the chunk option is error = FALSE (the default).
# we'll need to bail and not evaluate to the next python expression.
if (identical(options$error, FALSE))
had_error <- TRUE

# format the exception object
etype <- py_get_attr(e, "__class__")
traceback <- import("traceback")
paste0(traceback$format_exception_only(etype, e),
collapse = "")
}
)

}
else
py_compile_eval(snippet, compile_mode)

Expand Down Expand Up @@ -304,10 +321,9 @@ eng_python <- function(options) {
pending_source_index <- range[2] + 1

# bail if we had an error with 'error=FALSE'
if (identical(options$error, FALSE) && inherits(captured, "error")) {
had_error <- TRUE
if (had_error && identical(options$error, FALSE))
break
}


}
}
Expand Down

0 comments on commit 9580b03

Please sign in to comment.