]> Savannah Git Hosting - gnulib.git/commitdiff
gnulib-tool.py: Don't print tracebacks when Ctrl-C is pressed.
authorCollin Funk <collin.funk1@gmail.com>
Thu, 16 May 2024 03:25:38 +0000 (20:25 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Thu, 16 May 2024 03:25:38 +0000 (20:25 -0700)
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
pygnulib/main.py

index 9a69d7e2aa47b64f69ed8d2fe51c5d7ead38afc5..7690099cde540d9a72c413b3137f8f7b30b50153 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-05-15  Collin Funk  <collin.funk1@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  <bruno@clisp.org>
 
        stdbit-h: Add tests.
index dc0b2f436686c1479fce76ee5f975fa61ab74aee..b693e71d7fc306147f553f26b99a8e256c4bc404 100644 (file)
@@ -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)