Ctrl-C, ignored breakpoints

I have been using wxPython for a while now, but nothing very
sophisticated. I have two questions which have been bothering me for
the longest time.

1) How to change Ctrl-C behaviour? By default, pressing Ctrl-C in the
   terminal from which the wxPython app has been started causes the
   application to exit to shell. Is there any way to get the app to
   generate a KeyboardInterrupt exception instead, like a normal Python
   program? I frequently would like to stop my program in the middle of
   execution (say to find and debug an infinite loop), but can't do it
   because of this behaviour.

2) Is it me or are breakpoints set using 'pdb' simply ignored for
   wxPython apps?? For the life of me I can't find a way to debug
   wxPython programs, other than using inline "print" statements... what
   do I have to do to be able to "break" on a line and step through a
   running program??

···

--
Maciej Kalisiak mac "at" dgp.toronto.edu www.dgp.toronto.edu/~mac

Maciej Kalisiak wrote:

I have been using wxPython for a while now, but nothing very
sophisticated. I have two questions which have been bothering me for
the longest time.

1) How to change Ctrl-C behaviour? By default, pressing Ctrl-C in the
   terminal from which the wxPython app has been started causes the
   application to exit to shell. Is there any way to get the app to
   generate a KeyboardInterrupt exception instead, like a normal Python
   program? I frequently would like to stop my program in the middle of
   execution (say to find and debug an infinite loop), but can't do it
   because of this behaviour.

I've gone back and forth on this and have not found a better solution. The current behaviour is how other X apps behave and so I thought it best to stick with that. The other ways I have tried it resulted in either the KeyboardInterrupt not being raised until after the MainLoop exited, or in a segfault at exit, neigther of which seemed like a good alternative to me.

2) Is it me or are breakpoints set using 'pdb' simply ignored for
   wxPython apps?? For the life of me I can't find a way to debug
   wxPython programs, other than using inline "print" statements... what
   do I have to do to be able to "break" on a line and step through a
   running program??

It works for me, running pdb via the pdb mode in emacs. How do you run pdb and set the breakpoint?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

>1) How to change Ctrl-C behaviour? By default, pressing Ctrl-C in the
> terminal from which the wxPython app has been started causes the
> application to exit to shell. Is there any way to get the app to
> generate a KeyboardInterrupt exception instead, like a normal Python
> program? I frequently would like to stop my program in the middle of
> execution (say to find and debug an infinite loop), but can't do it
> because of this behaviour.

I've gone back and forth on this and have not found a better solution.
The current behaviour is how other X apps behave and so I thought it
best to stick with that. The other ways I have tried it resulted in
either the KeyboardInterrupt not being raised until after the MainLoop
exited, or in a segfault at exit, neigther of which seemed like a good
alternative to me.

Is there some workaround to cause the wxPython app to break at the currently
executing point? Any way to do this whatsoever? Occasionally I find that I
have chosen a menuitem which runs a function that runs for a longer period then
expected, and would like to debug where it is spending its time. Obviously
setting a breakpoint does not help with such intermittent behaviours.

>2) Is it me or are breakpoints set using 'pdb' simply ignored for
> wxPython apps?? For the life of me I can't find a way to debug
> wxPython programs, other than using inline "print" statements... what
> do I have to do to be able to "break" on a line and step through a
> running program??
>

It works for me, running pdb via the pdb mode in emacs. How do you run
pdb and set the breakpoint?

Well, I tried a number of methods, from commandline, ipython, and now emacs too
(although I haven't used pdb from Emacs before). This is for example how I try
to do it from the commandline:

% python -u /usr/lib/python2.3/pdb.py main.py

(main.py is my prog) I then do a few 's' commands to step into the main source
file, and then do "break main". The line number given for the breakpoint is
correct. But when I do "c" after that, the program doesn't stop at the
breakpoint, no matter where it has been set.

···

On Mon, Jan 05, 2004 at 03:17:10PM -0500, Robin Dunn wrote:

--
Maciej Kalisiak mac "at" dgp.toronto.edu www.dgp.toronto.edu/~mac

Maciej Kalisiak wrote:

1) How to change Ctrl-C behaviour? By default, pressing Ctrl-C in the
terminal from which the wxPython app has been started causes the
application to exit to shell. Is there any way to get the app to
generate a KeyboardInterrupt exception instead, like a normal Python
program? I frequently would like to stop my program in the middle of
execution (say to find and debug an infinite loop), but can't do it
because of this behaviour.

I've gone back and forth on this and have not found a better solution. The current behaviour is how other X apps behave and so I thought it best to stick with that. The other ways I have tried it resulted in either the KeyboardInterrupt not being raised until after the MainLoop exited, or in a segfault at exit, neigther of which seemed like a good alternative to me.

Is there some workaround to cause the wxPython app to break at the currently
executing point? Any way to do this whatsoever? Occasionally I find that I
have chosen a menuitem which runs a function that runs for a longer period then
expected, and would like to debug where it is spending its time. Obviously
setting a breakpoint does not help with such intermittent behaviours.

I think the more advanced debuggers (like WingIDE or Boa) can handle breaking into a running app.

2) Is it me or are breakpoints set using 'pdb' simply ignored for
wxPython apps?? For the life of me I can't find a way to debug
wxPython programs, other than using inline "print" statements... what
do I have to do to be able to "break" on a line and step through a
running program??

It works for me, running pdb via the pdb mode in emacs. How do you run pdb and set the breakpoint?

Well, I tried a number of methods, from commandline, ipython, and now emacs too
(although I haven't used pdb from Emacs before). This is for example how I try
to do it from the commandline:

% python -u /usr/lib/python2.3/pdb.py main.py

(main.py is my prog) I then do a few 's' commands to step into the main source
file, and then do "break main". The line number given for the breakpoint is
correct. But when I do "c" after that, the program doesn't stop at the
breakpoint, no matter where it has been set.

That works here:

$ pdb wxButton.py
> <string>(1)?()
(Pdb) s
--Call--
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(2)?()
-> from wxPython.wx import *
(Pdb) n
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(2)?()
-> from wxPython.wx import *
(Pdb)
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(4)?()
-> import images
(Pdb) n
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(8)?()
-> class TestPanel(wxPanel):
(Pdb)
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(57)?()
-> def runTest(frame, nb, log):
(Pdb) b TestPanel.OnClick
Breakpoint 1 at /home/work/projects/wx2.4/wxPython/demo/wxButton.py:49
(Pdb) c
wx.VERSION_STRING = 2.4.2.4
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(49)OnClick()
-> self.log.write("Click! (%d)\n" % event.GetId())
(Pdb) print event
<wxPython.events.wxCommandEventPtr instance; proxy of C++ wxCommandEvent instance at _bfffd6e0_wxCommandEvent_p>
(Pdb) c
09:48:34 AM: Click! (20)

···

On Mon, Jan 05, 2004 at 03:17:10PM -0500, Robin Dunn wrote:

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Well, I tried your procedure on my system and that works fine. I then trimmed
down my program into a (very) minimalistic test case which still exhibits this
behaviour. It is starting to look like a bug (or just my misunderstanding of
some part of Python :)... something to do with scopes is causing pdb to ignore
breakpoints. Here is the test case code:

---8<---
#!/usr/bin/env python

from wxPython.wx import *

class App(wxApp):
    def OnInit(self):
        frame = wxFrame(NULL, -1, "This is a test")
        frame.Show(TRUE)
        self.SetTopWindow(frame)
        return TRUE

def main():
    global foo # THIS SEEMS TO BE THE CULPRIT
                                        # it causes pdb to ignore breakpoints
                                        # in wxPython apps somehow...
                                      
    foo = 1 # not really necessary

    app = App(0)
    app.MainLoop()

if __name__ == '__main__':
    main()

---8<---

Test procedure:
- pdb test.py
- do one or two 's' steps
- 'b main'
- 'c'
- bp ignored

If you comment out "global foo" (and "foo=1" naturally) and repeat the
procedure, the bp will be respected.

···

On Wed, Jan 07, 2004 at 01:42:29PM -0500, Robin Dunn wrote:

That works here:

$ pdb wxButton.py
> <string>(1)?()
(Pdb) s
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(2)?()
-> from wxPython.wx import *
(Pdb) n
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(2)?()
-> from wxPython.wx import *
(Pdb)
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(4)?()
-> import images
(Pdb) n
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(8)?()
-> class TestPanel(wxPanel):
(Pdb)
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(57)?()
-> def runTest(frame, nb, log):
(Pdb) b TestPanel.OnClick
Breakpoint 1 at /home/work/projects/wx2.4/wxPython/demo/wxButton.py:49
(Pdb) c
wx.VERSION_STRING = 2.4.2.4
> /home/work/projects/wx2.4/wxPython/demo/wxButton.py(49)OnClick()
-> self.log.write("Click! (%d)\n" % event.GetId())
(Pdb) print event
<wxPython.events.wxCommandEventPtr instance; proxy of C++ wxCommandEvent
instance at _bfffd6e0_wxCommandEvent_p>
(Pdb) c
09:48:34 AM: Click! (20)

--
Maciej Kalisiak mac "at" dgp.toronto.edu www.dgp.toronto.edu/~mac

Maciej Kalisiak wrote:

Well, I tried your procedure on my system and that works fine. I then trimmed
down my program into a (very) minimalistic test case which still exhibits this
behaviour. It is starting to look like a bug (or just my misunderstanding of
some part of Python :)... something to do with scopes is causing pdb to ignore
breakpoints. Here is the test case code:

---8<---
#!/usr/bin/env python

from wxPython.wx import *

class App(wxApp):
    def OnInit(self):
        frame = wxFrame(NULL, -1, "This is a test")
        frame.Show(TRUE)
        self.SetTopWindow(frame)
        return TRUE

def main():
    global foo # THIS SEEMS TO BE THE CULPRIT
                                        # it causes pdb to ignore breakpoints
                                        # in wxPython apps somehow...

Not just wxPython apps, but ordinary Python apps too. Please report this to the Python team.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!