Ronu
Ronu

Reputation: 563

Python traceback to print stack at the end of class

I do have many python classes and which dynamically imports and runs several test scripts (Its like a test automation tool). The logging is also very clear that it reports some error in the correct test script. However, the error is occurred from one of the libraries used by the test script. The error is very common pythonic error - - need more than 0 values to unpack

The library code is too large and the logging doesn't provide any information on which line/statement this error occurred in the library.

I am not able to figure out the buggy line. Where should I print traceback stack?

Is there any way that I can print entire traceback befor termination of the program?

UPDATES :

after adding many debugging statements, the script fails in this function -

def _print_pretty_results(self,diff):
    count = 1
    str = ''
    #Calculate Screen Buffer Size
    rows, columns = os.popen('stty size', 'r').read().split()
    for err in diff:
        err_dict = diff[err]
        for key in err_dict:
            exp_val = err_dict[key]["Exp"]
            act_val = err_dict[key]["Act"]
            str += "_"*int(columns)
            str += "\n\nMISMATCH NO. %d\n" % count
            str += "_"*int(columns)
            str += "\n\nAttribute : %s" % key
            str += "\nExpected  : %s" % exp_val
            str += "\nActual    : %s\n" % act_val
            count += 1


    str += "_"*int(columns)+"\n"
    return str

Upvotes: 0

Views: 618

Answers (2)

Ross
Ross

Reputation: 1033

How about first using "import traceback;" then pdb.set_trace() to debug interactively;

def _print_pretty_results(self,diff):
    count = 1
    str = ''

    try:
        #Calculate Screen Buffer Size
        rows, columns = os.popen('stty size', 'r').read().split()
        for err in diff:
            err_dict = diff[err]
            for key in err_dict:
                exp_val = err_dict[key]["Exp"]
                act_val = err_dict[key]["Act"]
                str += "_"*int(columns)
                str += "\n\nMISMATCH NO. %d\n" % count
                str += "_"*int(columns)
                str += "\n\nAttribute : %s" % key
                str += "\nExpected  : %s" % exp_val
                str += "\nActual    : %s\n" % act_val
                count += 1


        str += "_"*int(columns)+"\n"
    catch Exception, e:
        traceback.print_exc()
        import pdb; pdb.set_trace()
    return str

Upvotes: 1

Abhishek Kulkarni
Abhishek Kulkarni

Reputation: 3838

I guessed it correct - Python TypeError : need more than 0 values to unpack is because of the statement

rows, columns = os.popen('stty size', 'r').read().split()

The issue here is, the above statement will calculate the buffer size and you are not allowing the script to access the screen buffer by redirecting your output to some file and that too running in background using nohup command.

Here is the sample code I have tried --

import os
rows, columns = os.popen('stty size', 'r').read().split()
print rows
print columns

There are four different way I ran this script -

Output1

[root@localhost]# python test.py
44
168

Output2

[root@localhost]# python test.py > /tmp/out.log
[root@localhost]#

[root@localhost]# cat /tmp/out.log
44
168

Output3

[root@localhost]# python test.py > /tmp/out.log &
[root@localhost]#

[root@localhost]# cat /tmp/out.log
44
168

Output4

[root@localhost]# nohup python test.py > /tmp/out.log &
[root@localhost]#

[root@localhost]# cat /tmp/out.log
stty: standard input: Inappropriate ioctl for device
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    rows, columns = os.popen('stty size', 'r').read().split()
ValueError: need more than 0 values to unpack

Upvotes: 2

Related Questions