[wxPython] Exceptions anyone?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] Exceptions anyone?

Hugues Talbot

        I must be thoroughly dumb, but why doesn't this work:

------------
if __name__ == '__main__':
    # when not imported
    try:
        app = MyApp(0)     # Create an instance of the application class
        app.MainLoop()     # Tell it to start processing events

    except:
        print "Uncaught exception"
------------

        No exception is caught that way. Looks like it should though?

  ___Hugues Talbot, CSIRO Mathematical & Information Sciences___
 /            Down under, Locked Bag 17, North Ryde  NSW  2113  \
|    ,-_|\                Ph: 61 2 9325 3224 Fax: 61 2 9325 3200 |
|   /     \               (Building E6B, Macquarie University)   |
|   \_,-._* <- There      E-mail: [hidden email]    |
|        ,                WWW: http://www.dms.csiro.au/~talbot   |
+- Failure is not an option. It comes bundled with the software -+




_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Exceptions anyone?

Robin Dunn
>
> I must be thoroughly dumb, but why doesn't this work:
>
> ------------
> if __name__ == '__main__':
>     # when not imported
>     try:
> app = MyApp(0)     # Create an instance of the application class
> app.MainLoop()     # Tell it to start processing events
>
>     except:
> print "Uncaught exception"
> ------------
>
> No exception is caught that way. Looks like it should though?
>

I assume there's some more of the program you're not showing us...  ;-)

The main reason is that exceptions don't cause the main loop to exit.  The
secondary reason is that since most of the Python code that is executed is
actually wrapped in C++ code (event handlers and such) and when control
returns to the C++ code then PyErr_Print() is called if there was an
unhandled exception.  PyErr_Print prints the traceback to sys.stderr and
clears the error indicator.  wxWindows just goes on its merry way never
knowing there was a problem.

--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Exceptions anyone?

Hugues Talbot


> I assume there's some more of the program you're not showing us...  ;-)

        Yep, but it's mostly irrelevant here...

        So any idea how I can catch unprocessed exceptions? This
is pretty important for debugging, as my `alpha testers' would like
some feedback as to why my application is crashing or not doing
something it ought to sometimes, and they can't be persuaded to
run the code from a console... (they want to double-click on the
data file and see it appear in the application, that sort of things).

        I've tried to redirect stderr with the following technique:

1) define

class StderrFaker:
    def write(self, message):
        print "Stderr:" + message # or any gui-based stuff

2) and before the MainLoop():

        import sys
        sys.stderr = StderrFaker()

        This is standard fare from the `learning Python' O'Reilly
book, but this is quite a bit harder to manage.



  ___Hugues Talbot, CSIRO Mathematical & Information Sciences __
 /            Down under, Locked Bag 17, North Ryde  NSW  2113  \
|    ,-_|\                Ph: 61 2 9325 3224 Fax: 61 2 9325 3200 |
|   /     \               (Building E6B, Macquarie University)   |
|   \_,-._* <- There      E-mail: [hidden email]    |
|        ,                WWW: http://www.dms.csiro.au/~talbot   |
+- Failure is not an option. It comes bundled with the software -+


















_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Exceptions anyone?

Robin Dunn
> So any idea how I can catch unprocessed exceptions? This
> is pretty important for debugging, as my `alpha testers' would like
> some feedback as to why my application is crashing or not doing
> something it ought to sometimes, and they can't be persuaded to
> run the code from a console... (they want to double-click on the
> data file and see it appear in the application, that sort of things).
>
> I've tried to redirect stderr with the following technique:
>
> 1) define
>
> class StderrFaker:
>     def write(self, message):
> print "Stderr:" + message # or any gui-based stuff
>
> 2) and before the MainLoop():
>
> import sys
> sys.stderr = StderrFaker()
>
> This is standard fare from the `learning Python' O'Reilly
> book, but this is quite a bit harder to manage.
>

Actually this is very similar to what I would have suggested.  Your
StderrFaker could create a frame when something begins to write to it, with
the written text going to a wxTextCtrl within the frame.  Also displayed in
the frame is a message that says someting like "Please copy this text into
an email message describing what went wrong and mail it to [hidden email]"
When the window is closed, StderrFaker can catch that with an OnCloseWindow
and then reset itself and wait for something else to be written to stderr.
If you're real adventureous you could have the StderrFaker actually send
email to you after the user enters a description of what they were doing at
the time and clicks a button.  (Using smtplib in the python library.)

If you create something generally useful for this I think it would be a good
cantidate for submission to the wxPython library.

--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users