Mailing List Archive

getting source code line of error?
I am trying to get the source code line of the last error.
I know traceback.format_exc() but this contains much more information, e.g.:

Traceback (most recent call last):
File "./error.py", line 18, in main
x=1/0
ZeroDivisionError: division by zero

I could extract the source code line with re.search(), but is there an
easier way?


I have:

exc_type,exc_str,exc_tb = sys.exc_info()
fname = exc_tb.tb_frame.f_code.co_filename
line = exc_tb.tb_lineno
print('%s in %s line %d' % (exc_str,fname,line))

But I also want to output the line itself, not only its number.

--
Ullrich Horlacher Server und Virtualisierung
Rechenzentrum TIK
Universitaet Stuttgart E-Mail: horlacher@tik.uni-stuttgart.de
Allmandring 30a Tel: ++49-711-68565868
70569 Stuttgart (Germany) WWW: http://www.tik.uni-stuttgart.de/
--
https://mail.python.org/mailman/listinfo/python-list
Re: getting source code line of error? [ In reply to ]
Have you tried the logger module and the format options?

On Fri, 19 Nov 2021 at 19:09, Ulli Horlacher
<framstag@rus.uni-stuttgart.de> wrote:
>
> I am trying to get the source code line of the last error.
> I know traceback.format_exc() but this contains much more information, e.g.:
>
> Traceback (most recent call last):
> File "./error.py", line 18, in main
> x=1/0
> ZeroDivisionError: division by zero
>
> I could extract the source code line with re.search(), but is there an
> easier way?
>
>
> I have:
>
> exc_type,exc_str,exc_tb = sys.exc_info()
> fname = exc_tb.tb_frame.f_code.co_filename
> line = exc_tb.tb_lineno
> print('%s in %s line %d' % (exc_str,fname,line))
>
> But I also want to output the line itself, not only its number.
>
> --
> Ullrich Horlacher Server und Virtualisierung
> Rechenzentrum TIK
> Universitaet Stuttgart E-Mail: horlacher@tik.uni-stuttgart.de
> Allmandring 30a Tel: ++49-711-68565868
> 70569 Stuttgart (Germany) WWW: http://www.tik.uni-stuttgart.de/
> --
> https://mail.python.org/mailman/listinfo/python-list
--
https://mail.python.org/mailman/listinfo/python-list
Re: getting source code line of error? [ In reply to ]
Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
> >except Exception as inst:
> > print( traceback.format_exc() )
>
> More to the point of getting the line number:

As I wrote in my initial posting:
I already have the line number. I am looking for the source code line!

So far I use:

m = re.search(r'\n\s*(.+)\n.*\n$',traceback.format_exc())
if m: print('%s %s' % (prefix,m.group(1)))

--
Ullrich Horlacher Server und Virtualisierung
Rechenzentrum TIK
Universitaet Stuttgart E-Mail: horlacher@tik.uni-stuttgart.de
Allmandring 30a Tel: ++49-711-68565868
70569 Stuttgart (Germany) WWW: http://www.tik.uni-stuttgart.de/
--
https://mail.python.org/mailman/listinfo/python-list
Re: getting source code line of error? [ In reply to ]
Am 20.11.21 um 20:15 schrieb Ulli Horlacher:
> Stefan Ram <ram@zedat.fu-berlin.de> wrote:
>> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>> except Exception as inst:
>>> print( traceback.format_exc() )
>>
>> More to the point of getting the line number:
>
> As I wrote in my initial posting:
> I already have the line number. I am looking for the source code line!
>
> So far I use:
>
> m = re.search(r'\n\s*(.+)\n.*\n$',traceback.format_exc())
> if m: print('%s %s' % (prefix,m.group(1)))
>
Stefan Ram's solution missed only the line content. Here it is.


import sys
import traceback

try:
1/0
except ZeroDivisionError as exception:
tr = traceback.TracebackException.from_exception( exception )
x = tr.stack[0]
print("Exception %s in line %s: %s" % (exception, x.lineno, x.line))


The traceback object does not only contain the lineno but also the
content of the offending line.

--------------
Paolo
--
https://mail.python.org/mailman/listinfo/python-list