Hi Pádraig,

On 5/13/24 2:32 PM, Pádraig Brady wrote:
> https://github.com/pixelb/crudini/blob/cbb3b85e/crudini.py#L1134
> Note that also handles a closed stdin (and stdout elsewhere in that util).

I just pushed the attached patch following your code there. Works well
for me. Thanks for the help.

    $ gnulib-tool --create-testdir --dir testdir1
    ^C
    $

Collin
From 43ca9607f03b697df0dfc356ef1a3029551c9897 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Wed, 15 May 2024 20:25:38 -0700
Subject: [PATCH] gnulib-tool.py: Don't print tracebacks when Ctrl-C is
 pressed.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Suggested by Pádraig Brady in
<https://lists.gnu.org/archive/html/bug-gnulib/2024-05/msg00200.html>.

* pygnulib/main.py (cli_exception): New function.
(main_with_exception_handling): Use it.
---
 ChangeLog        |  8 ++++++++
 pygnulib/main.py | 11 +++++++++++
 2 files changed, 19 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index 9a69d7e2aa..7690099cde 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-05-15  Collin Funk  <collin.fu...@gmail.com>
+
+	gnulib-tool.py: Don't print tracebacks when Ctrl-C is pressed.
+	Suggested by Pádraig Brady in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2024-05/msg00200.html>.
+	* pygnulib/main.py (cli_exception): New function.
+	(main_with_exception_handling): Use it.
+
 2024-05-15  Bruno Haible  <br...@clisp.org>
 
 	stdbit-h: Add tests.
diff --git a/pygnulib/main.py b/pygnulib/main.py
index dc0b2f4366..b693e71d7f 100644
--- a/pygnulib/main.py
+++ b/pygnulib/main.py
@@ -1365,7 +1365,18 @@ def main(temp_directory: str) -> None:
                 pass
 
 
+def cli_exception(exc_type, exc_value, exc_traceback) -> None:
+    '''Exception hook that does not print a traceback for KeyboardInterrupts
+    thrown when Ctrl-C is pressed.'''
+    if not issubclass(exc_type, KeyboardInterrupt):
+        sys.__excepthook__(exc_type, exc_value, exc_traceback)
+
+
 def main_with_exception_handling() -> None:
+    # Don't print tracebacks for KeyboardInterrupts when stdin is a tty.
+    if sys.stdin and sys.stdin.isatty():
+        sys.excepthook = cli_exception
+
     try:  # Try to execute
         with tempfile.TemporaryDirectory(prefix='glpy') as temporary_directory:
             main(temporary_directory)
-- 
2.45.0

Reply via email to