How to pretty print or format stack trace from python? - python

I receive python stack traces from logs and they sometimes come in very weird format, e.g. no new lines or incorrectly formated. Here is an syntactic example:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in bar File "<stdin>", line 2, in foo ValueError
Unfortunately, there is no way I can change the format of the logs, as I don't have an access to that system.
Is there an automatic way of formatting those exceptions to standard python format, so that it would look something like this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in bar
File "<stdin>", line 2, in foo
ValueError
I've quickly went through traceback module's documentation, but it seems it's only for runtime exceptions.

Related

Retrieve python traceback format from string

I have a string that is a python traceback collapsed into a single line. I want to retrieve the multiline format of the traceback for the sake of readability. What is the best way to achieve it?
I see that traceback module has similar functionality, but I don't see how to pass a string as an argument there:
https://docs.python.org/3/library/traceback.html
Example input:
'Traceback (most recent call last): File "<stdin>", line 1, in <module> Exception'
Example output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception

How to catch python exception and save traceback text as string

I'm trying to write a nice error handler for my code, so that when it fails, the logs, traceback and other relevant info get emailed to me.
I can't figure out how to take an exception object and extract the traceback.
I find the traceback module pretty confusing, mostly because it doesn't deal with exceptions at all. It just fetches some global variables from somewhere, assuming that I want the most recent exception. But what if I don't? What if I want to ignore some exception in my error handler? (e.g. if I fail to send me email and want to retry.)
What I want
import traceback as tb
# some function that will fail after a few recursions
def myfunc(x):
assert x > 0, "oh no"
return myfunc(x-1)
try:
myfunc(3)
except Exception as e:
traceback_str = tb.something(e)
Note that tb.something takes e as an argument.
There's lots of questions on Stack Overflow about using the traceback module to get a traceback string. The unique thing about this question is how to get it from the caught exception, instead of global variables.
Result:
traceback_str contains the string:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in myfunc
File "<stdin>", line 3, in myfunc
File "<stdin>", line 3, in myfunc
File "<stdin>", line 2, in myfunc
AssertionError: oh no
Note that it contains not just the most recent function call, but the whole stack, and includes the "AssertionError" at the end
The correct function for tb.something(e) is
''.join(tb.format_exception(None, e, e.__traceback__))

Is there a package that makes traceback and error handling easier?

I come from a python background, and error traceback back there is so easy to follow and allows for quick exception identification.
I was wondering if there was a package or a plugin (VSCode) that formats or makes error tracing easier in Flutter.
For non Python user, here are two examples:
Traceback (most recent call last):
File "/path/to/example.py", line 4, in <module>
greet('Chad')
File "/path/to/example.py", line 2, in greet
print('Hello, ' + someon)
NameError: name 'someon' is not defined
$ python example.py
Traceback (most recent call last):
File "/path/to/greetings.py", line 19, in <module>
greet('Chad', greting='Yo')
TypeError: greet() got an unexpected keyword argument 'greting'

Cannot play ogg file in Python

So I tried to convert my midi file to ogg and I still cant get it to work... hope some of you can help me?
This is my code:
showTextScreen('Tetromino')
while True: # game loop
if random.randint(0, 1) == 0:
pygame.mixer.music.load('tetrisb.mid')
else:
pygame.mixer.music.load('tetrisc.mid')
pygame.mixer.music.play(-1, 0,0)
runGame()
pygame.mixer.music.stop()
showTextScreen('Game Over')
It gives the error that it cant open tetrisb.mid
I tried renaming it and it still says the same...
If i can fix this I can fix the other file probably
Here is the error:
Traceback (most recent call last):
File "H:\Programmering\Python\TETRIS!.py", line 487, in <module>
main()
File "H:\Programmering\Python\TETRIS!.py", line 166, in main
pygame.mixer.music.load('tetrisc.mid')
pygame.error: Couldn't open 'tetrisc.mid'
I'm using windows and python 3.8 and i dont mind if im using midi or ogg...
I changed my midi file into ogg, hoping it would fix the problem but it didnt.
My file is ogg atm
It looks like you need to supply the correct path to your midi file. This question about how to check for file existence might also be helpful.
The error messages from pygame.mixer.music.load are different if the file format is not supported or non-existent:
>>> pygame.mixer.music.load("nonexistent.file")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Couldn't open 'nonexistent.file'
>>> pygame.mixer.music.load(r"c:\tmp\empty.file")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Couldn't read from RWops
>>> pygame.mixer.music.load(r"c:\tmp\robots.txt")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pygame.error: Module format not recognized
>>> pygame.mixer.music.load(r"C:\Windows\media\flourish.mid")
>>> pygame.mixer.music.play(-1, 0)
>>> pygame.mixer.music.stop()
Note that when specifying a file path a \ is an escape character which is processed differently. So you can use \\ instead, or put an r at the start of the string, before the quotation mark as I've shown above.

when does bz2.decompress(rawdata) got an error of "couldn't find end of stream"?

I have checked that the file in my disk is the same as https://github.com/scipy/scipy/blob/master/scipy/misc/face.dat
but when I run this statement ,I have got this error. why ?
scipy.misc.face()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\scipy\misc\common.py", line 567, in face
data = bz2.decompress(rawdata)
ValueError: couldn't find end of stream
It's a bug (on Windows), see here
https://github.com/scipy/scipy/commit/7f0210fd28a2b3be79806d5cae462be52c1601e9

Categories

Resources