Handling errors in Python in the console - python

I am currently working with a program and doing a lot of testing on it. I am using the Python logging library and write all my errors to the library. However, the errors also print directly to the console and show the entire stack trace which I do not want. Instead I would rather it just simply say error in the console and print out to the error logs.
I found this similar question here (How to make python gracefully fail?) but all answers talked about using try catches. Is there anyway to set up errors being handled this way throughout the entire program without using try-catches? I foresee them being difficult to use, for example when I quit using Control + C, which I could do at any time and may not be able to catch in a try-catch block. I would love to be able to tell my program to handle errors a certain way and have that reflected throughout the entire program.

Related

Is it possible in Python to run some code upon any exception, without losing scope?

One of my favorite debug tools is a short piece of code which starts up a console. I sometimes debug by means of (roughly)
try:
#my code
...
except:
launch_console()
However, this requires anticipating where the error will be. If I enclose the entire script in a try/except, I will not have access to any variables within the function I am debugging.
It would be magnificently convenient, if I could run the program and just automatically have launch_console() run in case of an error, without losing scope. I think spyder is supposed to have this functionality, but it does not work.
Wondering if there is some nice way to do this!

Suppress undefined name warning in Spyder?

Being in the process of switching from Matlab to Python/IPython/Spyder I ran into the following issue.
In a Matlab script (and I actually did not notice this until I switched to Python) there is no warning if you are using a variable which you did not define in that script.
What I kept doing all the times in Matlab was to have, say, 2 (or more) scripts which I would then execute one after the other. The second script would typically use variables defined in the first, but would not give me any warning.
In Spyder I noticed that the situation is different. In my hypothetical second script, all the variables which are not defined in the second script itself give me a warning (undefined name '...'), which is not nice too see...
Another typical example, would be the following: I have a main script but there is something in this script I wanna look at better. So, without touching the script, I would create another file where I would copy paste a few lines from the script to play a little with them.
In this new file I would be using variables which are in the console already but that are techically unknown to the file itself, so would give me a warning.
So I guess I am simply asking if there a way to suppress this kind of warnings.
Or there might be a deeper question of whether there s a more pythonic way of working, but there I am really not sure about...

How to programmatically execute/step through Python code line by line

I am trying to find a way that I can have a program step through Python code line by line and do something with the results of each line. In effect a debugger that could be controlled programmatically rather than manually. pdb would be exactly what I am looking for if it returned its output after each step as a string and I could then call pdb again to pickup where I left off. However, instead it outputs to stdout and I have to manually input "step" via the keyboard.
Things I have tried:
I am able to redirect pdb's stdout. I could redirect it to a second
Python program which would then process it. However, I cannot
figure out how to have the second Python program tell pdb to
step.
Related to the previous one, if I could get pdb to step all the way
through to the end (perhaps I could figure out something to spoof a
keyboard repeatedly entering "step"?) and redirect the output to a
file, I could then write another program that acted like it was
stepping through the program when it was actually just reading the
file line by line.
I could use exec to manually run lines of Python code. However,
since I would be looking at one line at a time, I would need to
manually detect and handle things like conditionals, loops, and
function calls which quickly gets very complicated.
I read some posts that say that pdb is implemented using
sys.settrace. If nothing else works I should be able to recreate
the behavior I need using this.
Is there any established/straight forward way to implement the behavior that I am looking for?
sys.settrace() is the fundamental building block for stepping through Python code. pdb is implemented entirely in Python, so you can just look at the module to see how it does things. It also has various public functions/methods for stepping under program control, read the library reference for your version of Python for details.
I read some posts that say that pdb is implemented using sys.settrace.
If nothing else works I should be able to recreate the behavior I need
using this.
Don't view this as a last resort. I think it's the best approach for what you want to accomplish.

Errors don't stop execution

I'm coding using Python and Pyglet, and, as I just have starting the script, it is full of errors, which is normal.
What is less normal is that those errors don't stop execution of the script, even if they are 'basic python' errors. It isn't very bothersome, but a little, since it makes me outsee some errors.
For example, I got :
AttributeError: 'Win32EventLoop' object has no attribute '_next_idle_time'
(by the way, I don't know what this one means. It isn't the main subject of my thread but I'd gladly get answers for this one)
or :
IndexError: list index out of range
(A stupid bug I solved, but it should have stopped the script).
I use Eclipse + pydev, and never experienced that before. It may be pyglet related because it happened with my first pyglet attemps (before, I used pygame).
Thanks for answers,
Fred
Your GUI application is probably running in some kind of loop. This event loop is catching exceptions for You and printing them out.
I recommend writing Your code the way You can unit-test small parts of your program e.g. classes or functions outside of the framework and then glue them together as GUI application. This work-flow will guarantee that you haven't overseen simple bugs just because mindlessly throwing new code into more-or-less working pile of existing code ;)
Read about TDD ;)

How to automate python logging

I need to add logging to a milter that I wrote a few months back. It is occasionally rejecting some messages, but I'm not sure why. I know how to add logging to a Python script from the HowTo, but is it necessary for me to add log output commands at every point in my script, or is there a way Python automatically handles that?
Basically, I don't know where in the script it fails and don't want to add the overhead of 60 logging lines. I'm looking for the simplest method of doing this.
If you have no idea where it fails you could run a debugging session with input that you know causes the error, and step through the code if that is an option.
Another pretty obvious option is to log all exceptions at the entrance of your script and then drill down from there, but I honestly don't think that there is a way that will find the right places to log for you - if this would be the case that program could just as well track the bug down on itself.

Categories