Why aren't breakpoints working on the Swing Event Dispatch Thread in PyDev?

折月煮酒 提交于 2019-12-12 09:26:41

问题


I'm using Jython, Swing, and PyDev (Eclipse).

Breakpoints are not being hit on any code that runs on the EDT (aka AWT Event Queue?).

This includes:

  • Functions that are invoked from a Swing event (eg JButton click)
  • Functions that, via a decorator, are run through SwingUtilities.invokeLater() (See the last example here.
  • Functions that registered as hooks to a Java package (socket class), that I'm using.

Swing event code to reproduce:

from javax.swing import JFrame, JButton

def TestFunc(event):
    #breakpoints in this function don't work
    print "Hey"

if __name__ == '__main__':
    mainWindow = JFrame('Test', 
                        defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                        size = (1024, 600))
    mainWindow.add(JButton("Hey", actionPerformed = TestFunc))
    mainWindow.visible = True

invokeLater() code to reproduce:

from java.lang import Runnable
from javax.swing import SwingUtilities
import threading

class foo(Runnable):
    def __init__(self, bar):
        self.bar = bar

    def run(self):
        #breakpoints in this function don't work
        print threading.currentThread()
        print self.bar

if __name__ == '__main__':
    myFoo = foo(5)
    SwingUtilities.invokeLater(myFoo)

回答1:


It's actually a Jython issue.

I.e.: in the code below, when TestFunc is called, the print from the trace_dispatch should be called, but it's not.

So, the Jython tracing implementation is not calling the tracing function as it should in that situation. You can 'help' the PyDev debugger by calling import pydevd;pydevd.settrace(suspend=False) so that the debugger discovers about that frame (i.e.: in the start of TestFunc add that line of code).

Note that if you don't pass the suspend=False, it'll act as a breakpoint in the code and will stop the execution at that line.

import sys
import threading
def trace_dispatch(frame, event, arg):
    print frame.f_code.co_filename, frame.f_code.co_name
sys.settrace(trace_dispatch)
threading.settrace(trace_dispatch)

from javax.swing import JFrame, JButton

def TestFunc(event):
    print "Hey"

if __name__ == '__main__':
    mainWindow = JFrame('Test', 
                        defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
                        size = (1024, 600))
    mainWindow.add(JButton("Hey", actionPerformed = TestFunc))
    mainWindow.visible = True


来源:https://stackoverflow.com/questions/7252070/why-arent-breakpoints-working-on-the-swing-event-dispatch-thread-in-pydev

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!