I've a python call from a powershell terminal.
minimalistic example:
Try {
& python "test.py"
}
Catch {
# Place individual error handling here
$ErrorMessage = "$($_.Exception.Message)"
}
echo $ErrorMessage
test.py raises an error, example content can be:
def make_sum():
return a+b
if __name__ == "__main__":
make_sum()
Why does the $ErrorMessage variable contain only the first line of python traceback?
Traceback (most recent call last):
I'd need complete call stack info as python provides:
Traceback (most recent call last):
File "d:/.../test.py", line 10, in <module>
make_sum()
File "d:/.../test.py", line 7, in make_sum
return a+b
NameError: name 'a' is not defined
Any advice how to get a complete traceback into the string variable in powershell?
Thank you,
Honza
Related
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__))
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'
Consider the following Python program:
code = """
def test():
1/0
"""
filename = "<test>"
c = compile(code, filename, 'exec')
exec(c)
import linecache
linecache.cache[filename] = (len(code), None, code.splitlines(keepends=True), filename)
import traceback
print("Traceback from the traceback module:")
print()
try:
test()
except:
traceback.print_exc()
print()
print("Regular traceback:")
print()
test()
I am dynamically defining a function that raises an exception and adding it to the linecache. The output of the code is
Traceback from the traceback module:
Traceback (most recent call last):
File "test.py", line 20, in <module>
test()
File "<test>", line 3, in test
1/0
ZeroDivisionError: division by zero
Regular traceback:
Traceback (most recent call last):
File "test.py", line 28, in <module>
test()
File "<test>", line 3, in test
ZeroDivisionError: division by zero
If I then get a traceback from that function using the traceback module, the line of code from the function is shown (the 1/0 part of the first traceback). But if I just let the code raise an exception and get the regular traceback from the interpreter, it doesn't show the code.
Why doesn't the regular interpreter traceback use the linecache? Is there a way to make the code appear in regular tracebacks?
The default sys.excepthook uses a separate, C-level implementation of traceback printing, not the traceback module. (Perhaps this is so it still works even if the system is too borked to use traceback.py.) The C implementation doesn't try to use linecache. You can see the code it uses to retrieve source lines in _Py_DisplaySourceLine.
If you want tracebacks to use the traceback module's implementation, you could replace sys.excepthook with traceback.print_exception:
import sys
import traceback
sys.excepthook = traceback.print_exception
I'm trying to figure out why this
def scanner(fileName, function):
with open(fileName) as file:
for line in file:
function(line)
def toSmallLetters(line):
print line.lower()
def paramin(fileName):
scanner(fileName, toSmallLetters)
if __name__ == "__main__":
import sys
paramin(sys.argv[1])
throws this error:
Traceback (most recent call last):
File "script.py", line 14, in <module>
paramin(sys.argv[1])
IndexError: list index out of range
What's wrong?
You are getting:
Traceback (most recent call last):
File "script.py", line 14, in <module>
paramin(sys.argv[1])
IndexError: list index out of range
because when you run the script you need to supply a parameter, presumably the file name that you want to scan. If the file is called script.py you might run as:
python script.py name-of-file-to-scan
sys module provides access to any command-line arguments via the sys.argv.
Provide argument while running program.
ex:
python script.py file_name
I'm typing this code directly into Gimp's Python Console:
img=gimp.image_list()[0]
drw = pdb.gimp_image_active_drawable(img)
gimp-drawable-set-pixel(drw,x,y,3,[0xff,0x00,0x00])
and it's yeilding this error after calling the gimp-drawable-set-pixel function:
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'drawable' is not defined
It yields the same error when called with four bytes instead of three bytes as well.
I'm using gimp 2.8.10 on Ubuntu 14.04.01 x86_64
I think it's a typo of gimp_drawable_set_pixel.
pdb.gimp_drawable_set_pixel(drw,x,y,3,[0xff,0x00,0x00])