bpo-30639: Lazily compute repr for error by takluyver · Pull Request #2132 · python/cpython (original) (raw)

If the TypeError is caught and handled by calling code, there's no need to compute the object's repr for the error message - the repr may take a long time (real case encountered in IPython & Jedi), or it may throw another error.

If the error results in a traceback, it will still compute the repr and produce the message as before.

I think this defensiveness is worthwhile for inspect functions because, by design, we're calling them with arbitrary objects which might be badly behaved, and expecting them to be useful nonetheless.

An alternative fix would be to use object.__repr__() to always get the plain <foo object at 0x123abc...> representation.

https://bugs.python.org/issue30639