[Python-ideas] Have REPL print less by default

Steven D'Aprano steve at pearwood.info
Wed Apr 20 21:03:07 EDT 2016


On Wed, Apr 20, 2016 at 03:01:48PM -0400, Random832 wrote:

> You know what would make complicated infinite recursion easier to debug?
> The arguments.

Check out the cgitb module, which installs an except hook which (among 
many other things) prints the arguments to the functions. Run this 
sample code:


import cgitb
cgitb.enable(format='text')

def spam(arg):
    if arg == 0:
        raise ValueError('+++ out of cheese error, redo from start +++')
    return spam(arg - 1)

def eggs(x):
    return spam(x) + 1

def cheese(a, b, c):
    return a or b or eggs(c)

cheese(0, None, 1)


and you will see output something like the following. (For brevity I have 
compressed some of the output.)



ValueError
Python 3.3.0rc3: /usr/local/bin/python3.3
Thu Apr 21 10:58:22 2016

A problem occurred in a Python script.  Here is the sequence of
function calls leading up to the error, in the order they occurred.

 /home/steve/python/<stdin> in <module>()
 /home/steve/python/<stdin> in cheese(a=0, b=None, c=2)
 /home/steve/python/<stdin> in eggs(x=2)
 /home/steve/python/<stdin> in spam(arg=2)
 /home/steve/python/<stdin> in spam(arg=1)
 /home/steve/python/<stdin> in spam(arg=0)

ValueError: +++ out of cheese error, redo from start +++
    __cause__ = None
    __class__ = <class 'ValueError'>
    [...]
    __traceback__ = <traceback object>
    args = ('+++ out of cheese error, redo from start +++',)
    with_traceback = <built-in method with_traceback of ValueError object>

The above is a description of an error in a Python program.  Here is
the original traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in cheese
  File "<stdin>", line 2, in eggs
  File "<stdin>", line 4, in spam
  File "<stdin>", line 4, in spam
  File "<stdin>", line 3, in spam
ValueError: +++ out of cheese error, redo from start +++




-- 
Steve


More information about the Python-ideas mailing list