Cracking The Python Traceback Secret

Hello Geeks! I hope all are doing great. So as the title of this article suggests, today, we are going to discuss python traceback. Yes, all those who know python programming a bit often encounter the word traceback or traceback error.

In this article, we will discuss that. We will see what traceback is and how it helps us diagnose errors in our code and then fix them. So, let’s get started.

What is a Traceback?

So, understanding errors and finding reasons for an error is one of the very major aspects of programming. To fix bugs in the system, we need to discover why the bug appeared? Traceback helps us in doing that. How?! So, what traceback does is that it contains the report of function calls made in our code and store it in a stack.

So, when the system encounters an exception, traceback prints the most recently used function call, which results in hinting at the place from where an error may occur. Sometimes it is also the case where the reason for error may not be in the most recent function call.

In that case, we have to go through the list of tracebacks that appeared in the console.

I know it sounds a bit confusing, but we can understand it better by looking through the examples. But before that, we have to keep some points in mind, i.e., we always take a bottom-up approach to read and understand the errors. It is a very efficient way to understand the error.

You will get to know what information do a console error and traceback represent in a few seconds. To do that, let’s see a console error.

console error python traceback

So, in the above image, we can see how a console error or traceback gives us complete information about the error, such as error type, error location, and description of the error.

This bunch of information helped us a lot in diagnosing the error so that we can apply the fix on them. Now, once we have learned how to understand the error, let’s move on to error types.

Types of Traceback Error

Although traceback gives us a lot of information about errors, it is essential for us to understand the actual error and in which condition those errors occur. When we can understand all of them, fixing them is not a difficult task for us.

Attribute Error

Whenever we try to access an attribute that is not possessed by that object, we get an attribute error. For example- We know that to make the string uppercase, we use the upper(). 

a=5
a.upper()
Output:

AttributeError: 'int' object has no attribute 'upper' 

Learn Demystifying Python Attribute Error With Examples

Index Error

IndexError occurs when there is an invalid index passed to access the elements of the object. In python, the index starts from 0 and ranges till the length-1 of the list. Whenever the element is accessed from the list using the square brackets, the __getitem__ method is called. This method first checks if the index is valid. The index cannot be float or greater than, or equal to the length of the list.

So, whenever you try to access the element which is not acceptable, IndexError is thrown.

color = ['red', 'blue', 'green', 'pink']
print(color[len(color)])
Output:

Traceback (most recent call last):
    File "<string>", line 2, in <module>
Index Error: List index out of range

Python List Index Out Of Range: Error and Resolution

Import Error

Import error occurs when a file is unable to load the module. The reason may be that the module is not installed in the system, or may the module’s location is not correctly defined, or maybe the name of the class is incorrect.

# name of the class is incorrect i.e. testmod rather tha testmode
from doctest import testmode
Output:

Traceback (most recent call last):                                                                                        File "<stdin>", line 1, in <module>                                                                                   ImportError: cannot import name 'testmode' from 'doctest'

Name Error

This error occurs when we want to access any variable or function that is not defined or invalid.

a = 10
b = 20

d = a+b+c
print(d)
Output:

Traceback (most recent call last):                                                                                            File "<stdin>", line 1, in <module>                                                                                   NameError: name 'c' is not defined  

Syntax Error

A syntax error occurred when we missed following the syntax. The other reason is maybe that we made some typos in our code. We should be careful about the syntax error, and sometimes while dealing with large modules, we miss some minor syntax errors and spend much time finding them.

def function()
     a = 10
     print(a)
SyntaxError: invalid syntax

Type Error

The type error occurs when we try to operate any function or operation on any datatype which is not supported by that datatype. For Example:- using len() function on int object which does not support len() function.

a = 10 
b = 20
print(len(a))
Output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()

Value Error

The value error is raised when we assign an inappropriate value to the object. It may be that we put the value out of range or some incompatible value.

a = [10,20,30,40,50]
x,y,z = a 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack (expected 3)

How to Print Stack Trace in Python

Although the system raises the error when something is wrong with our code, we can also print the exception on our console. It works the same way as to try and except block with the block of code to print the exception or error that occurred. Let’s see this.

import traceback    # importing traceback module
x = 5 
try:
  y = x.upper()
except:
  traceback.print_exc()    # printing stack trace
print("end of the program")      # Line of code to demonstrate that all the above lines of code executed
Output:

end of the program
Traceback (most recent call last):
  File "<ipython-input-9-c9c9a4a1af58>", line 4, in <module>
    y = x.upper()
AttributeError: 'int' object has no attribute 'upper'

Although we haven’t passed any argument in the print_exc() method in the above example, we can customize the given function with the following arguments.

Syntax:
traceback.print_exc(limit=<value>, file=<value>, chain=<value>)
  • limit : It limits the printing of trace stack on th console. If set to null, prints all the entries.
  • file : If it is set to, None, the output goes to sys.stderr; otherwise, it should be an open file or file-like object to receive the output.
  • chain : If it is true then, interpreter itself hanndle when the printing is to be done.

How to exit from Python without Traceback

You can do that by following the given code,

import traceback,sys    # importing traceback and sys module
x = 1
try:
  y = x.upper()
except:
  traceback.print_exc(file=sys.stdout)    # printing stack trace
  sys.exit(0)

print("end of the program")
Output:
Traceback (most recent call last):
  File "<ipython-input-42-9b3a7065b099>", line 4, in <module>
    y = x.upper()
AttributeError: 'int' object has no attribute 'upper'
An exception has occurred, use %tb to see the full traceback.

SystemExit: 0

In the above snippet of output, you can see that when the error occurs, we get the error message in the output console despite handling it in the except block.

How to Parse Traceback as String

import traceback   # importing traceback
x = 1
try:
  y = x.upper()
except:
   error = traceback.format_exc()
print(error)         # Printing error message
print(type(error))   # Checking datetype of the error
print("end of the program")
Output:
Traceback (most recent call last):
  File "<ipython-input-44-9b46a18aa464>", line 4, in <module>
    y = x.upper()
AttributeError: 'int' object has no attribute 'upper'

<class 'str'>
end of the program

How to get log traceback while using logging in Python

To get log traceback while logging in python, you can use logging.exception from within the except: handler/block to log the current exception along with the trace information. To do that, use the following block of code.

import traceback  
import logging
LOG_FILENAME = '/tmp/logging_example.out'
logging.basicConfig(filename=LOG_FILENAME, level=logging.DEBUG)
logger = logging.getLogger(__name__)
x = 1
try:
  y = x.upper()
except:
   error = traceback.format_exc()
   logger.exception("Got Exception")

Getting a traceback form multiprocessing Process

To get the traceback from the multiprocessing library, we need tblib.

import tblib.pickling_support
tblib.pickling_support.install()

from multiprocessing import Pool
import sys


class ExceptionWrapper(object):

    def __init__(self, ee):
        self.ee = ee
        __, __, self.tb = sys.exc_info()

    def re_raise(self):
        raise self.ee.with_traceback(self.tb)
        # for Python 2 replace the previous line by:
        # raise self.ee, None, self.tb


# example of how to use ExceptionWrapper

def inverse(i):
    """ will fail for i == 0 """
    try:
        return 1.0 / i
    except Exception as e:
        return ExceptionWrapper(e)


def main():
    p = Pool(1)
    results = p.map(inverse, [0, 1, 2, 3])
    for result in results:
        if isinstance(result, ExceptionWrapper):
            result.re_raise()


if __name__ == "__main__":
    main()

So, if you catch an exception in your remote process, wrap it with ExceptionWrapper and then pass it back. Calling re_raise () the main process will do the work.

FAQs

How to access a python traceback from C API?

To do that, use the following libraries and codes.

#include
#include
PyTracebackObject* traceback = get_the_traceback();
int line = traceback->tb_lineno;
const char* filename = PyString_AsString(traceback->tb_frame->f_code->co_filename);

How to limit python traceback to specific files?

To print your stack trace, you need to handle all unhandled exceptions yourself; this is how the sys.excepthook becomes handy.
The signature for this function is sys.excepthook(type, value, traceback) and its job is:
This function prints out a given traceback and exception to sys.stderr.

How can I use awk or grep to capture an entire python traceback in a log file?

Use the following command in your terminal to get the entire traceback – grep -v '[0-9] INFO ' *.log

Conclusion

So today, in this article, we learned about tracebacks and how it helps us find the errors and then apply fixes to them. We have also seen how a console error gives us complete information about the error. Moreover, in the end, we have discussed the different types of errors that occur in our program and what they represent.

I hope this article has helped you. Thank You!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments