From 3970f4ac405516ad3d1aba5079c0754ec1dc5da1 Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Mon, 7 Dec 2020 11:55:29 +0100 Subject: [PATCH] Add new variable to selectively suppress backtraces in batch mode. * src/eval.c (syms_of_eval): Define new variable 'backtrace-on-error-noninteractive' to selectively enable backtrace printing in batch mode. (signal_or_quit): Use it. * etc/NEWS: Document new variable. * test/src/eval-tests.el (eval-tests/backtrace-in-batch-mode/inhibit): New unit test. --- etc/NEWS | 4 +++- src/eval.c | 9 ++++++++- test/src/eval-tests.el | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 8390c82c98b..a30355f20db 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -193,7 +193,9 @@ These functions return the connection local value of the respective variables. This can be used for remote hosts. ** Emacs now prints a backtrace when signaling an error in batch mode. This -makes debugging Emacs Lisp scripts run in batch mode easier. +makes debugging Emacs Lisp scripts run in batch mode easier. If you +want to disable this behavior, set 'backtrace-on-error-noninteractive' +to nil. * Editing Changes in Emacs 28.1 diff --git a/src/eval.c b/src/eval.c index 18df484aac0..1351d283c58 100644 --- a/src/eval.c +++ b/src/eval.c @@ -1735,7 +1735,8 @@ signal_or_quit (Lisp_Object error_symbol, Lisp_Object data, bool keyboard_quit) debugging. Make sure to use `debug' unconditionally to not interfere with ERT or other packages that install custom debuggers. */ if (!debugger_called && !NILP (error_symbol) - && (NILP (clause) || EQ (h->tag_or_ch, Qerror)) && noninteractive) + && (NILP (clause) || EQ (h->tag_or_ch, Qerror)) && noninteractive + && backtrace_on_error_noninteractive) { ptrdiff_t count = SPECPDL_INDEX (); specbind (Vdebugger, Qdebug); @@ -4264,6 +4265,12 @@ Note that `debug-on-error', `debug-on-quit' and friends still determine whether to handle the particular condition. */); Vdebug_on_signal = Qnil; + DEFVAR_BOOL ("backtrace-on-error-noninteractive", + backtrace_on_error_noninteractive, + doc: /* If non-nil and Emacs is running noninteractively, +print a backtrace on encountering an unhandled error. */); + backtrace_on_error_noninteractive = true; + /* The value of num_nonmacro_input_events as of the last time we started to enter the debugger. If we decide to enter the debugger again when this is still equal to num_nonmacro_input_events, then we diff --git a/test/src/eval-tests.el b/test/src/eval-tests.el index 4125573dc6a..297db81f5ab 100644 --- a/test/src/eval-tests.el +++ b/test/src/eval-tests.el @@ -195,6 +195,23 @@ expressions works for identifiers starting with period." (search-forward " foo()") (search-forward " normal-top-level()"))))) +(ert-deftest eval-tests/backtrace-in-batch-mode/inhibit () + (let ((emacs (expand-file-name invocation-name invocation-directory))) + (skip-unless (file-executable-p emacs)) + (with-temp-buffer + (let ((status (call-process + emacs nil t nil + "--quick" "--batch" + (concat "--eval=" + (prin1-to-string + '(progn + (defun foo () (error "Boo")) + (let ((backtrace-on-error-noninteractive nil)) + (foo)))))))) + (should (natnump status)) + (should-not (eql status 0))) + (should (equal (string-trim (buffer-string)) "Boo"))))) + (ert-deftest eval-tests/backtrace-in-batch-mode/demoted-errors () (let ((emacs (expand-file-name invocation-name invocation-directory))) (skip-unless (file-executable-p emacs)) -- 2.39.2