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
Related
I am currently working on a python project - windows application / software that has GUI where you can put patient information and so on and all that is going to a local mysql database / table. all that works but my IDE VS code gives 4 error messages and one of the errors is related to "len()" function. I will provide Github link of the entire code plus the error messages so if someone can help, please do so! Thank you!
https://github.com/ethicalduty/Hospital_Management_Software_Windows/blob/main/main_Hospital_Management_Software_Windows_3.11.0.py
I am new to programming so I cannot do much besides trying to find similar solution on google. I have not found anything already resolved so far, thus asking for help here!
The "errors" you are seeing are type checking issues, not real Python errors that are occurring when you run your code. They're warnings that there might be real errors in the code, but you should be able to run it regardless and the code may work fine. Whether there's a real issue may depend on the data the code is processing (it might work for some kinds of data but not others), or there might be inaccuracies in how the type checker is interpreting the code (and so there is no real issue in the code at all).
The issue that has to do with len seems to be saying that your rows = my_cursor.fetchall() statement may assign None to rows, which would cause an error below where you do len(rows). I don't know MySQL's Python bindings, so I'm not sure if the type checker's assumption is correct that a query could cause fetchall to return None (rather than an empty list), but if that's the case, you can easily fix the type checking issue (and the possibly small chance of a real error when you run the code) by checking that rows is not None before you check its length:
rows = my_cursor.fetchall()
if rows is not None and len(rows) != 0:
...
I have written someGDB python scripts to analyze data containers, but depending on core dump backtrace, some of my class initialization actions fails due to following exception:
gdb> my_command Exception: my_cmd_class.__init__(some_init_data) No symbol "A_VALID_ENUM_NAME" in current context.
The variable name is definitely there, but in case it's not in scope of current backtrace, gdb is unable to find it for some reason. In case I'll print a listing of the file like:
gdb> list file.c:1
, and then repeat a command, the exception is not thrown.
The above workaround is not good though, as it reguires manual intervention (or a good backtrace, but often I don't have such a luxury), so any ideas / suggestions to get around this issue?
It sounds like it could be a gdb bug (specifically related to lack of symtab expansion in some case); but you haven't really provided enough details to answer this definitively.
Is there a command in the Python debugger (pdb) that says something like "run until the next exception is raised?"
Seems an obvious requirement but can't seem to find it.
Update : To be clear, my problem is an exception which is being caught and turned into an inadequate message in a log file. And I can't find where the exception is raised.
I figured that if I could go into trace mode and say "run until an exception is thrown" that would be the most straightforward way of finding it. I don't think post-mortem will work here.
Execute your script as follows:
python -m pdb myscript.py
Press c and Enter.
When an uncaught exception is raised you program will stop running and fall back to the pdb debug prompt.
Can you search the code in the failing script for the text of the message that is logged (I realize that this may be difficult if the string is generated in a complex way). If you can find the point where the message is generated/logged then you can set an appropriate break point to troubleshoot the problem.
Unfortunately, AFAIK Python pdb debugging does not offer the capability that is present in some other languages to say, for example, break when Exception is raised.
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.
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.