Python system log file - python

I have a script that has been crashing (seemingly) unpredictably. I can't work out how to reproduce it.
Does Python keep a global system log file, so I can go back and look at what exception precipitated the exit? If so, where can it be found on Windows? I'd like to be able to see a traceback too if possible.
edit: I know I could put the whole script inside a try...except block, but I don't know how long I'll be running it before it crashes again. I'd like to be able to at least gain some cursory information about crashes that have already happened, even if that information is just the type of exception that caused the crash. That way I can try to reproduce the bug more reliably.
I suspect the crash been caused by failed communication with external devices (perhaps something as simple a loose cable). These sorts of failure are essentially random and difficult to reproduce, so I'd like to know if it was a communication error or a genuine code bug.

If you want to know the information about exception you can use this code.
try:
#do some stuff
1/0 #stuff that generated the exception
except Exception as ex:
print ex
raw_input()
For debugging it properly there is a tool winpdb. There is a very good tutorial available for it to learn debugging using it debugging tutorial

Can you "catch" the exception? If yes, then you can use traceback to print the stacktrace and pinpoint where the problem occurs.
Then you can debug the module in question (even if it's part of the python lib, it will be in plain readable form) using Eclipse.
import traceback
...
...
try:
<your script>
except Exception as runtime_ex:
print runtime_ex, traceback.format_exc()

Related

Python3.6 logging module: how to log to file the error which cause the application to terminate prematurely?

The logging module does it's job to report things occurring before the application crash. But somehow it missed the most important part: it doesn't log the error that crash the app, which I find surprising (I'm a beginner).
I know I could print these errors using the try except statement. But it's a rather complicate step to merely print the error which crash the app (it requires to modifies the code).
I did spent few hours trying to understand the documentations, and quite a bunch of thread about it, but I did not find the basic answer of this basic question.
Is there a way to log these NameError, SytaxError, and any runtime error on a file.log?

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.

Python Debugging : Find where an exception is raised, go into debug mode?

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.

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

Categories

Resources