[Python-ideas] Shrink recursion error tracebacks (was: Have REPL print less by default)

Chris Angelico rosuav at gmail.com
Thu Apr 21 01:26:52 EDT 2016


On Thu, Apr 21, 2016 at 2:55 PM, Émanuel Barry <vgr255 at live.ca> wrote:
> The core of my proposal is to enhance ability to read and debug large
> tracebacks, which most of the times will be because of recursive calls.
> We're shrinking tracebacks for recursive calls, and if that happens to
> shrink other large tracebacks as well, that's a useful side effect and
> should be considered, but that's not the core of my idea.

No problem with that. In my experience, most RecursionErrors come from
*accidental* recursion, which is straight-forwardly infinite and
usually involves a single function. Consider this idiom:

>>> class id(int):
...     _orig_id = id
...     def __new__(cls, obj):
...         return super().__new__(cls, cls._orig_id(obj))
...     def __repr__(self):
...         return hex(self)
...
>>> obj = object()
>>> obj
<object object at 0x7ffb12290110>
>>> id(obj)
0x7ffb12290110

If I muck something up and accidentally call id() inside the
definition of __new__ (instead of cls._orig_id), it'll end up
infinitely recursing. A traceback shortener that recognizes only the
very simplest forms of repetition would work fine for this case. It
doesn't need a huge amount of intelligence - the traceback would have
a bunch of exactly identical lines, and it _would_ end with one of the
identical ones.

ChrisA


More information about the Python-ideas mailing list