Exception catching with wx.pyshell - python

I have an app written in Python and using wxpython for the GUI. Embedded in the app there is a pyShell. I have an editor next to it, where python code can be written, and executed in the shell. Related to this I have two questions due to a lack of documentation and examples:
1) To execute code being written in the editor (i.e. can be multi-lined code with for-loops e.t.c.), should I use run or Execute? I am using Execute now, since it seems to execute all of the code at "once" instead of "line by line", but I'm not sure about this.
2) If an error occured (including SyntaxErrors), how can I catch this? I tried to put a try/except around my call to shell.Execute(<editor-code>), but the exception is not caught by me (it's being printed into the shell). It seems like nothing is returned from the shell.Execute either. I would prefer a solution where you don't have to pre-parse the code if possible. If you cant do this I guess pre-parsing the code is the only option.

Related

Error handling in Kivy

I just started using Kivy and was wondering how I am supposed to handle exceptions. For example, in a simple Python script that runs in the command line, I would normally print the error first then use sys.exit(1) to exit the script. However, I noticed sys.exit(1) causes my Kivy application to completely close.
What is the proper way to handle this?
Thanks!
In order to handle errors, you should look at the ExceptionHandler class.
After following the discussion on the Kivy GitHub page, it seems the most graceful way to handle errors is to just handle errors with Python as you normally would, but you could use the ExceptionHandler class.

Keep simpleserver active even on syntax errors

Is there a way to configure the simple-server that Flask uses to not exit on every single syntax error?
app = Flask(__name__)
app.run(host='0.0.0.0', debug=True, use_debugger=True, passthrough_errors=False);
Currently I'm using this setup for the simple-server.
Setting passthrough_errors to False means most of the errors actually keeps the process alive so that I can use the interactive debugger, syntax errors still exits the program though. I've tried different configuration values but I have not found anything that works. Thanks!
I just posted a Flask-Failsafe extension to solve this exact issue.
I hit this all the time and ran across your post earlier looking for a solution. After a bit of experimenting I hacked up a decorator you can use to wrap your initialization code so that if it fails the reloader will keep working. Check it out and let me know what you think.
According to Python documentation there are two types or errors:
Syntax Errors
Exceptions
Syntax errors are produced during parse time (at that moment your code doesn't actually executes, so you have no possibility to catch errors, since parse time is not a runtime, when your code actually executes).
The only way you can catch syntax errors is when they happen inside a piece of code given as an argument to exec function (executes string of python code):
>>> try:
... exec('x===6')
... except SyntaxError:
... print('Hello!')
...
Hello!
But you must remember to use exec() only when you really know what you do. It's not recommended to use exec() at all especially when it depends on user input.

Where to find python-interpreter error codes?

I have a python application that communicates with a PHP application.
Right now, I have defined error codes on both sides so they match (i.e. we can detect the python-side failures in PHP and react accordingly).
My problem comes from python being an interpreted language, here is why :
If the code has a syntax problem, then the interpreter will return error codes.
But these interpreter error codes will be indistinguishable from the application errors.
I am not in charge of the python code, but I was asked to be able to detect eventual python interpreter failures so I can notify the end user using PHP.
Because of that, I need to find a list of the interpreter return codes when malfunctionning.
I failed to find this information on both google, python doc, and man page.
Does anybody have/know where to find this information ?
(also, if you have any other ideas on how to get around this problem, I'd be happy to hear them)
Thanks in advance !
The best solution would be setting an exception hook that always exits with a certain code.
import sys
def excepthook(type, value, traceback):
sys.exit(X) # X is your exit code
sys.excepthook = excepthook
Syntax errors should be irrelevant - why would you ever put a script that contains syntax errors in production?
But anyway, Python always exits with a non-zero code (apparently always 1) in case of an uncaught exception and 0 if everything went fine.
I believe this might be what you need. It will map the error codes to their respective string messages.
http://docs.python.org/library/errno.html
Specifically:
http://docs.python.org/library/os.html#os.strerror

How to stop a code running due to an input error without causing the GUI from crashing

I had a program developed in Python (2.7 & 3.2) that reads three files and generates some code based on those files. In the code, I had several input file checks to capture any input errors by the user. If the program catches an input error, I used os.sys.exit() command to stop processing and issue an error message. I was primarily using IDLE for the process and this worked fine.
Now I have developed a GUI for the program for deployment using PYQT4. The user uses the GUI to input all the necessary input files and conditions and then the GUI calls the earlier code I generated with the necessary arguments.
However, I am finding that if the user makes an error in the input files, when the earlier code catches those errors and the os.sys.exit() is executed, the GUI itself is shutdown completely; which is not good.
I introduced the same checks on the input files into the GUI, so if those are caught, they are treated within the GUI and not by the code. But there are certain processing checks that happen inside the code that the GUI does not have access to them.
The Question: Is there a way to make the called code stop from running, print an error message (to a log file for example; which I already use) without causing the GUI to quit altogether?
Thanks,
note: The code is too large at this point for me to integrate it into the GUI as a class.
I assume you can not or prefer not to change your CLI programs and instead wish to catch the exception raised by sys.exit instead in the GUI. Here is how:
import os
try:
os.sys.exit()
except SystemExit as err:
print('Caught ya')
Have you tried handling exceptions in python.
try:
#some code here
except Exception:
print 'Something bad happened'
Better try catching specific exceptions.
List of built-in exceptions http://docs.python.org/library/exceptions.html#bltin-exceptions

Where will Python be logging errors for me (moving from PHP)

I am a PHPer new to Python (2.7 on Win32) and I would like to know where Python is shoving any errors it finds?
Do I need to turn something on, if so where do I do that?
Or, is the idea that you develop using a shell and watch errors spat out via that?
Please share any other good Python debugging/sanity-saving mechanisms you wish you'd known about earlier - or if you have switched from PHP perhaps you can tell me what the Python equivalents of :
ini_set('error_reporting', 1);
display_errors();
trigger_error();
var_dump();
Try and Exceptions looks fairly similar.
I will probably stumble across these answers myself in time, but in the meantime this issue is bugging me (no pun intended).
Thanks a lot.
Python development is normally done in a shell, and you get a full traceback printed out on any uncaught exception.
If you want to log errors to file, have a look at the logging module. You can either catch exceptions directly, or override the sys.excepthook function which is called for an uncaught error. If you're using a framework for e.g. web development, it may have mechanisms to do this sort of thing already.

Categories

Resources