#!/usr/bin/python2 # encoding: utf-8 import cmd import readline class _DummyQtCore: @staticmethod def pyqtRemoveInputHook(): print '@v@ Qt input hook removed @v@' @staticmethod def pyqtRestoreInputHook(): print '@^@ Qt input hook restored @^@' QtCore = _DummyQtCore() import sys, pdb, pprint class QPdb(pdb.Pdb): def __init__(self, *args, **kwargs): pdb.Pdb.__init__(self, *args, **kwargs) self._ih_removed = False def cmdloop(self): if self._ih_removed: return pdb.Pdb.cmdloop(self) QtCore.pyqtRemoveInputHook() self._ih_removed = True pdb.Pdb.cmdloop(self) QtCore.pyqtRestoreInputHook() self._ih_removed = False def _set_current_frame(self, up=2): frame = sys._getframe() for n in range(up): frame = frame.f_back self.stack, self.curindex = self.get_stack(frame, None) self.curframe = self.stack[self.curindex][0] def _set_bottom_frame(self): frame = sys._getframe() self.reset() while frame: frame.f_trace = self.trace_dispatch self.botframe = frame frame = frame.f_back def func_break(self, func): if not hasattr(func, 'im_func'): raise ValueError(func) func = func.im_func code = func.func_code #use co_name to identify the bkpt (function names #could be aliased, but co_name is invariant) funcname = code.co_name lineno = code.co_firstlineno filename = code.co_filename self._set_current_frame() line = self.checkline(filename, lineno) if not line: return RuntimeError("Could not find breakpoint line for %r" % (func)) err = self.set_break(filename, line, 0, None, funcname) if err: print >>self.stdout, '***', err else: bp = self.get_breaks(filename, line)[-1] print >>self.stdout, "Breakpoint %d at %s:%d" % ( bp.number, bp.file, bp.line ) self._set_bottom_frame() self.set_continue() sys.settrace(self.trace_dispatch) class Example(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) self.qpdb = QPdb() self.qpdb.reset() self.qpdb.func_break(self.do_foo) def do_foo(self, arg): print 'foo' def do_bar(self, arg): print 'bar' def do_EOF(self, arg): return 1 do_q = do_quit = do_EOF def do_break(self, arg): try: self.qpdb.do_break(arg) except Exception: print_exc() print 'arg was %r' % (arg,) def do_set_trace(self, arg): self.qpdb.set_trace() do_s = do_set_trace if __name__ == '__main__': example = Example() example.cmdloop("Example program with debugging")