1

I've got the following:

try:
    package_info = __import__('app') #app.py
except:
    print traceback.extract_tb(sys.exc_info()[-1])
    print traceback.tb_lineno(sys.exc_info()[-1])

And what i get from this is:

[('test.py', 18, '<module>', 'package_info = __import__(\'app\')')]
18

Now this is almost what i want, this is where the actual error begins but i need to follow this through and get the actual infection, that is app.py containing an ä on row 17 not 18 for instance.

Here's my actual error message if untreated:

Non-ASCII character '\xc3' in file C:\app.py on line 17, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details", ('C:\app.py', 17, 0, None)), )

I've found some examples but all of them show the point of impact and not the actual cause to the problem, how to go about this (pref Python2 and Python3 cross-support but Python2 is more important in this scenario) to get the filename, row and cause of the problem in a similar manner to the tuple above?

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • Have you checked [this](http://stackoverflow.com/questions/1278705/python-when-i-catch-an-exception-how-do-i-get-the-type-file-and-line-number) out? – Argiri Kotsaris Aug 22 '13 at 16:07
  • Yes, it only gives the running module and not the actual cause either, i thought the traceback would catch these things too. – Torxed Aug 22 '13 at 16:39

3 Answers3

4

Catch the specific exception and see what information it has. The message is formatted from the exception object's parameters so its a good bet that its there. In this case, SyntaxError includes a filename attribute.

try:
    package_info = __import__('app') #app.py
except SyntaxError, e:
    print traceback.extract_tb(sys.exc_info()[-1])
    print traceback.tb_lineno(sys.exc_info()[-1])
    print e.filename
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Because it does exactly the same thing as the code i have already, it shows that `test.py` had an error on `__import__` which is not what i want (not me who downvoted this time, but that's probably why) – Torxed Aug 22 '13 at 16:42
  • Actually, e.filename works o0 wtf.. `e.filename`gives `app.py` which is correct, can i get the line-number and causing function from e as well perhaps? add that and you got the solve-tick! – Torxed Aug 22 '13 at 16:44
  • @Torxed - Probably. Run this in the python shell and `e` will still be available after the exception processes. You can `dir(e)` to get the attributes and poke around to see what's there. – tdelaney Aug 22 '13 at 16:50
  • `e.lineno` was the closest i got, can't find a neat way of showing the actual function-line causing the issue unless i open up the file manually and split each line and print the rows[e.lineno] but it's close enough. Thank you for reminding me that some things are simple. – Torxed Aug 22 '13 at 16:53
0

For me

except Exception as e:
    print e.__unicode__()

works with returning exactly same message

profuel
  • 128
  • 5
0

To be python 2.5 and 3.x compatible at the same time (2.5 does not support except Exception as e), use

try:
    package_info = __import__('app') #app.py

except SyntaxError:
    exc_type, exc, tb = sys.exc_info()
    print(exc)
    print('\n\n'.join(traceback.format_tb(tb, limit=5)))

prints

Non-ASCII character '\xc3' in file /foo/app.py on line 2, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (app.py, line 2)
  File "foo.py", line 6, in <module>
    package_info = __import__('app') #app.py