Source code for ase2sprkkr.common.debug

import sys


[docs] def add_debug_hook(only_original=False): """ Breaks when an unhandled exception occurs, if the terminal is present https://stackoverflow.com/questions/1237379/how-do-i-set-sys-excepthook-to-invoke-pdb-globally-in-python """ def info(type, value, tb): if ( # hasattr(sys, "ps1") or not sys.stderr.isatty() or not sys.stdin.isatty() ): # stdin or stderr is redirected, just do the normal thing original_hook(type, value, tb) else: # a terminal is attached and stderr is not redirected, debug import traceback import pdb traceback.print_exception(type, value, tb) pdb.pm() original_hook = sys.excepthook if not only_original or sys.excepthook == sys.__excepthook__: # if someone already patched excepthook, let them win sys.excepthook = info
[docs] def backtrace_pyparsing(term, fn, _visited=None): if _visited is None: _visited = set() identity = id(term) if identity in _visited: return _visited.add(identity) fn(term) if hasattr(term, "exprs"): for child in term.exprs: backtrace_pyparsing(child, fn, _visited) elif hasattr(term, "expr") and term.expr is not None: backtrace_pyparsing(term.expr, fn, _visited)
[docs] def debug_pyparsing(term): backtrace_pyparsing(term, lambda term: term.set_debug(True))
[docs] def check_whitespaces(term, chars=frozenset({"\t", " ", "\r"})): def check(item): if item.whiteChars != chars: raise ValueError("Bad white chars") backtrace_pyparsing(term, check)