From 9580b0311e2575da8bad99843625fc0f51ad19af Mon Sep 17 00:00:00 2001 From: Tomasz Kalinowski Date: Mon, 29 Jan 2024 09:09:23 -0500 Subject: [PATCH] knitr eng: format captured exceptions Closes #1520 --- R/knitr-engine.R | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/R/knitr-engine.R b/R/knitr-engine.R index 3e9951fa9..b0f5c59f1 100644 --- a/R/knitr-engine.R +++ b/R/knitr-engine.R @@ -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) @@ -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 - } + } }