Cause the custom exception handler to also log unhandled exceptions'
[python_utils.git] / bootstrap.py
index 1316dabc9433b7e468a0af60a4311d1b65db5cc8..a4923b4a9ce3424b80475c801dde8f93ed49b48f 100644 (file)
@@ -1,10 +1,14 @@
 #!/usr/bin/env python3
 
+# © Copyright 2021-2022, Scott Gasch
+
 """This is a module for wrapping around python programs and doing some
 minor setup and tear down work for them.  With it, you can break into
 pdb on unhandled top level exceptions, profile your code by passing a
 commandline argument in, audit module import events, examine where
-memory is being used in your program, and so on."""
+memory is being used in your program, and so on.
+
+"""
 
 import functools
 import importlib
@@ -94,9 +98,15 @@ def handle_uncaught_exception(exc_type, exc_value, exc_tb):
             ORIGINAL_EXCEPTION_HOOK(exc_type, exc_value, exc_tb)
         else:
             # a terminal is attached and stderr is not redirected, maybe debug.
+            import io
             import traceback
 
-            traceback.print_exception(exc_type, exc_value, exc_tb)
+            tb_output = io.StringIO()
+            traceback.print_tb(exc_tb, None, tb_output)
+            print(tb_output.getvalue(), file=sys.stderr)
+            logger.error(tb_output.getvalue())
+            tb_output.close()
+
             if config.config['debug_unhandled_exceptions']:
                 import pdb
 
@@ -269,6 +279,10 @@ def initialize(entry_point):
         logger.debug('Python interpreter version: %s', sys.version)
         logger.debug('Python implementation: %s', sys.implementation)
         logger.debug('Python C API version: %s', sys.api_version)
+        if __debug__:
+            logger.debug('Python interpreter running in __debug__ mode.')
+        else:
+            logger.debug('Python interpreter running in optimized mode.')
         logger.debug('Python path: %s', sys.path)
 
         # Log something about the site_config, many things use it.