0

I have a source code of 500Mb having more than 5K of files written in Python. Sometimes I get exception messages but no idea about the line number and file name of exception. Even sometimes exceptions are not seen on terminal unlit I specifically use pdb.

Is there any convenient way to get to know about the exception location ?

Thanks.

Ravi
  • 173
  • 1
  • 10
  • where are exception messages from? or show your messages here – kaitian521 Jun 06 '16 at 14:14
  • this is what I wanted to know. I run my project in cmd line for debugging as ravi$ python -OO -m core.main main file imports all the python code. my code shows just exception message not the location eg *** TypeError: int() can't convert non-string with explicit base – Ravi Jun 06 '16 at 14:27

1 Answers1

1

Try the traceback module: https://docs.python.org/3/library/traceback.html

It contains the tb_lineno function which takes the traceback as a parameter, returning the line number of the exception.

EDIT:

import sys, traceback

def lumberjack():
    bright_side_of_death()

def bright_side_of_death():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print("*** print_tb:")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print("*** print_exception:")
    traceback.print_exception(exc_type, exc_value, exc_traceback,
                          limit=2, file=sys.stdout)
    print("*** print_exc:")
    traceback.print_exc()
    print("*** format_exc, first and last line:")
    formatted_lines = traceback.format_exc().splitlines()
    print(formatted_lines[0])
    print(formatted_lines[-1])
    print("*** format_exception:")
    print(repr(traceback.format_exception(exc_type, exc_value,
                                      exc_traceback)))
    print("*** extract_tb:")
    print(repr(traceback.extract_tb(exc_traceback)))
    print("*** format_tb:")
    print(repr(traceback.format_tb(exc_traceback)))
    print("*** tb_lineno:", exc_traceback.tb_lineno)
Rome_Leader
  • 2,518
  • 9
  • 42
  • 73
  • where am I supposed to put line "tb_lineno" ?? I run my project in cmd line for debug as ravi$ python -OO -m core.main main file imports all the python code. thanks – Ravi Jun 06 '16 at 14:23
  • Check out the example at the link. I'll edit the answer to include it pasted from that source. It should be in your except block after trying some operation. – Rome_Leader Jun 06 '16 at 14:32
  • I even do not know about the try block where the exception took place. as i said earlier I have about 5000 files and countless try and except block in my project. do you mean to say i need to put tb_lineno on each catch/except block of the project. well this is not possible for me as I am just working on a maintenance project. thanks – Ravi Jun 06 '16 at 14:39
  • The problem might be in your structure, then. That sounds somewhat unreasonable, and I'm not sure how to address it without a refactoring. – Rome_Leader Jun 06 '16 at 14:41