Source code for mmf.utils.mdb

r"""Some debugging utilities"""
import sys

# Global variable to allow passing debug frame back to the user.
_LOCALS = [None]

try:
    import ipdb as pdb
    from ipdb import set_trace
except ImportError:
    import pdb
    def set_trace(frame=None):
        if frame is None:
            frame = sys._getframe().f_back
        pdb.Pdb().set_trace(frame=frame)
        
[docs]class MDBBreak(Exception): """MDB Breakout: Local variables in mmf.utils.mdb._LOCALS[0]: import mmf.utils.mdb; locals().update(mmf.utils.mdb._LOCALS[0]) """
[docs] def __init__(self): Exception.__init__(self, self.__doc__)
[docs]def mdb(trace=True): r"""Use in place of `import pdb;pdb.set_trace()`. When using the debugger, errors can crash the debugging session making it hard to continue. This version stores the local variables in :attr:`_LOCALS` so that you work with these in the interpreter by doing something like import mmf.utils.mdb; locals().update(mmf.utils.mdb._LOCALS[0]) """ frame = sys._getframe() locals_ = frame.f_locals _LOCALS[0] = locals_ if trace: set_trace(frame=frame.f_back) else: raise MDBBreak()
[docs]def breakout(): mdb(trace=False)