### pystacksnapshot Library API Reference Source: https://github.com/qfcy/pystacksnapshot/blob/main/README.md Reference documentation for the core functions provided by the `pystacksnapshot` library, detailing their purpose, parameters, and behavior for stack capturing and exception management. ```APIDOC stack_snapshot(start=0) Returns a list of the captured current stack (thread-safe). start: The stack depth (integer). hack_exc(exc) Enables automatic stack capturing for an exception class. exc: An exception class (e.g., ValueError). hack_all_exc(ignored=IGNORED) Enables automatic stack capturing for all exception classes (including standard library exceptions and user-defined exceptions that inherit from standard library exceptions). ignored: A list or tuple indicating which classes to ignore, defaulting to (BaseException,). trace_stack(err, file=None, brief_global_var=True, maxlength=150) Outputs the stack information of a specific exception. err: An exception object (e.g., from 'except Exception as err'). file: Where to output (similar to a file object), defaults to sys.stderr. brief_global_var: Whether to simplify the output of global variables (i.e., not outputting variables from functions, classes, and imported modules, and disabling most double underscore names like __var__). maxlength: The maximum length of variable values to avoid excessively long outputs (e.g., for arrays). trace_error(file=None, brief_global_var=True, maxlength=150) Outputs both the traceback and stack capturing information for an exception, without needing to provide the err parameter. file: Where to output (similar to a file object), defaults to sys.stderr. brief_global_var: Whether to simplify the output of global variables. maxlength: The maximum length of variable values. hook_sys_exception(brief_global_var=True, maxlength=150) Modifies sys.excepthook, allowing automatic output of the stack when an unhandled exception occurs. brief_global_var: Whether to simplify the output of global variables. maxlength: The maximum length of variable values. reset_sys_excepthook() Restores the original sys.excepthook. enable_snapshot() Enables automatic stack capturing when an exception occurs (thread-safe). disable_snapshot() Disables automatic stack capturing when an exception occurs (thread-safe). is_snapshot_enabled() Checks if automatic stack capturing is enabled (thread-safe). init(ignored=IGNORED, brief_global_var=True, maxlength=150) Enables automatic stack capturing for all exceptions and for stack output. (Recommended) ignored: A list or tuple of classes to ignore. brief_global_var: Whether to simplify the output of global variables. maxlength: The maximum length of variable values. ``` -------------------------------- ### Python `hack_exc` Function Implementation Source: https://github.com/qfcy/pystacksnapshot/blob/main/README.md Illustrates the internal working principle of the `hack_exc` function, which modifies an exception class's `__new__` method to inject stack snapshot capturing. It temporarily alters the object's type flags to allow modification of built-in attributes. ```Python _hacked_excs = weakref.WeakSet() def hack_exc(exc): # Prevent repeated modifications if exc in _hacked_excs: return _hacked_excs.add(exc) flag = get_type_flag(exc) pre_flag = flag flag |= Py_TPFLAGS_HEAPTYPE flag &= ~Py_TPFLAGS_IMMUTABLETYPE # Remove Py_TPFLAGS_IMMUTABLETYPE set_type_flag(exc, flag) # Temporarily modify the underlying flag of the object (the properties of built-in objects like ValueError.__new__ are originally unmodifiable) def __new__(cls, *args, **kw): new_func = BaseException.__new__ # Underlying __new__ method result = new_func(cls, *args, **kw) if not getattr(result, "stack_snapshot", None): # Prevent repeated capturing # Capture the current stack result.stack_snapshot = stack_snapshot(start=2) # start=2: skip this function and two layers of __new__ return result exc.__new__ = __new__ # Replace the exception type's __new__ pre_flag &= ~Py_TPFLAGS_IMMUTABLETYPE set_type_flag(exc, pre_flag) # Restore the original flag ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.