当我没有控制台时,如何在未捕获的异常中输入Python的pdb

时间:2015-10-22 19:28:22

标签: python python-2.7 debugging postmortem-debugging

我正在开发商店中运行Python应用程序。它是从另一个GUI应用程序调用的。当它失败时,我想在pdb事后调试中显示一个控制台,这样我就可以走过去看看当我们的用户遇到问题时会发生什么。

我已尝试在程序的顶部设置excepthook:

def pcs_debugger(type, value, tb):
    traceback.print_exception(type, value, tb)
    pdb.pm()

sys.excepthook = pcs_debugger

除非我没有控制台,比如当我使用pythonw启动它或者从其他不是用Python编写的GUI应用程序调用它时,它的效果很好。

有没有办法做到这一点?感谢

更新:我忘了在Windows 7上提到这一切

更新:添加最少的代码示例。请注意,如果我使用python.exe启动它,而不是pythonw.exe,这会按照我想要的方式工作,并且pythonw更类似于我在我的环境中进行的操作,其中我实际上有一个C#GUI加载这个来自dll的Python代码。

import pdb, sys, traceback, wx

def my_debugger(type, value, tb):
    traceback.print_exception(type, value, tb)
    pdb.pm()

sys.excepthook = my_debugger

class MyApp(wx.App):

    def OnInit(self):
        frame = MyFrame("Hello World", (50, 60), (450, 340))
        frame.Show()
        self.SetTopWindow(frame)
        return True

class MyFrame(wx.Frame):

    def __init__(self, title, pos, size):
        wx.Frame.__init__(self, None, -1, title, pos, size)
        menuFile = wx.Menu()
        menuFile.Append(1, "&About...")
        menuFile.AppendSeparator()
        menuFile.Append(2, "E&xit")
        menuFile.Append(3, "&Fail")
        menuBar = wx.MenuBar()
        menuBar.Append(menuFile, "&File")
        self.SetMenuBar(menuBar)
        self.CreateStatusBar()
        self.SetStatusText("Welcome to wxPython!")
        self.Bind(wx.EVT_MENU, self.OnAbout, id=1)
        self.Bind(wx.EVT_MENU, self.OnQuit, id=2)
        self.Bind(wx.EVT_MENU, self.OnFail, id=3)

    def OnQuit(self, event):
        self.Close()

    def OnAbout(self, event):
        wx.MessageBox("This is a wxPython Hello World Sample",
                      "About Hello World", wx.OK | wx.ICON_INFORMATION, self)

    def OnFail(self, event):
        a = 1
        b = 0
        self.SetStatusText('about to divide by 0')
        c = a / b
        print 'here it is {}'.format(c)
        return

if __name__ == '__main__':
    app = MyApp(False)
    app.MainLoop()

0 个答案:

没有答案