Source code for tracmass.seagrid.debug

"""Debugging utilities for tracmass.seagrid."""

import functools
import linecache
import sys


[docs] def trace_lines(func): """Decorator that prints each source line of *func* as it executes. Sub-function calls are not traced — only the lines of the decorated function itself are printed. Examples -------- >>> @trace_lines ... def open_dataset(...): ... ... """ @functools.wraps(func) def wrapper(*args, **kwargs): def local_trace(frame, event, arg): if event == "line": src = linecache.getline( frame.f_code.co_filename, frame.f_lineno ).rstrip() print(f" {frame.f_lineno}: {src}") return local_trace if event != "call" else None def global_trace(frame, event, arg): if event == "call" and frame.f_code is func.__code__: return local_trace return None old = sys.gettrace() sys.settrace(global_trace) try: return func(*args, **kwargs) finally: sys.settrace(old) return wrapper