codekingpro/portable-devtools
115k
1import sys2 3 4# Some cruft to deal with the Pythonwin GUI booting up from a non GUI app.5def _MakeDebuggerGUI():6 app.InitInstance()7 8 9isInprocApp = -110 11 12def _CheckNeedGUI():13 global isInprocApp14 if isInprocApp == -1:15 import win32ui16 17 isInprocApp = win32ui.GetApp().IsInproc()18 if isInprocApp:19 # MAY Need it - may already have one20 need = "pywin.framework.app" not in sys.modules21 else:22 need = 023 if need:24 import pywin.framework.app25 26 from . import dbgpyapp27 28 pywin.framework.app.CreateDefaultGUI(dbgpyapp.DebuggerPythonApp)29 30 else:31 # Check we have the appropriate editor32 # No longer necessary!33 pass34 return need35 36 37# Inject some methods in the top level name-space.38currentDebugger = None # Wipe out any old one on reload.39 40 41def _GetCurrentDebugger():42 global currentDebugger43 if currentDebugger is None:44 _CheckNeedGUI()45 from . import debugger46 47 currentDebugger = debugger.Debugger()48 return currentDebugger49 50 51def GetDebugger():52 # An error here is not nice - as we are probably trying to53 # break into the debugger on a Python error, any54 # error raised by this is usually silent, and causes55 # big problems later!56 try:57 rc = _GetCurrentDebugger()58 rc.GUICheckInit()59 return rc60 except:61 print("Could not create the debugger!")62 import traceback63 64 traceback.print_exc()65 return None66 67 68def close():69 if currentDebugger is not None:70 currentDebugger.close()71 72 73def run(cmd, globals=None, locals=None, start_stepping=1):74 _GetCurrentDebugger().run(cmd, globals, locals, start_stepping)75 76 77def runeval(expression, globals=None, locals=None):78 return _GetCurrentDebugger().runeval(expression, globals, locals)79 80 81def runcall(*args):82 return _GetCurrentDebugger().runcall(*args)83 84 85def set_trace():86 import sys87 88 d = _GetCurrentDebugger()89 90 if d.frameShutdown:91 return # App closing92 93 if d.stopframe != d.botframe:94 # If im not "running"95 return96 97 sys.settrace(None) # May be hooked98 d.reset()99 d.set_trace()100 101 102# "brk" is an alias for "set_trace" ("break" is a reserved word :-(103brk = set_trace104 105# Post-Mortem interface106 107 108def post_mortem(t=None):109 if t is None:110 t = sys.exc_info()[2] # Will be valid if we are called from an except handler.111 if t is None:112 try:113 t = sys.last_traceback114 except AttributeError:115 print(116 "No traceback can be found from which to perform post-mortem debugging!"117 )118 print("No debugging can continue")119 return120 p = _GetCurrentDebugger()121 if p.frameShutdown:122 return # App closing123 # No idea why I need to settrace to None - it should have been reset by now?124 sys.settrace(None)125 p.reset()126 while t.tb_next != None:127 t = t.tb_next128 p.bAtPostMortem = 1129 p.prep_run(None)130 try:131 p.interaction(t.tb_frame, t)132 finally:133 t = None134 p.bAtPostMortem = 0135 p.done_run()136 137 138def pm(t=None):139 post_mortem(t)140 