#!/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
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
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.