saferepr: Avoid indirect function calls

The DRY savings they provide are rather small, while they make it harder
to type-check, and IMO harder to understand.
This commit is contained in:
Ran Benita 2019-07-14 22:24:12 +03:00
parent c7aacc96bb
commit 129600d698
3 changed files with 42 additions and 23 deletions

View File

@ -0,0 +1 @@
Simplified internal ``SafeRepr`` class and removed some dead code.

View File

@ -2,19 +2,23 @@ import pprint
import reprlib
def _call_and_format_exception(call, x, *args):
def _format_repr_exception(exc, obj):
exc_name = type(exc).__name__
try:
# Try the vanilla repr and make sure that the result is a string
return call(x, *args)
except Exception as exc:
exc_name = type(exc).__name__
try:
exc_info = str(exc)
except Exception:
exc_info = "unknown"
return '<[{}("{}") raised in repr()] {} object at 0x{:x}>'.format(
exc_name, exc_info, x.__class__.__name__, id(x)
)
exc_info = str(exc)
except Exception:
exc_info = "unknown"
return '<[{}("{}") raised in repr()] {} object at 0x{:x}>'.format(
exc_name, exc_info, obj.__class__.__name__, id(obj)
)
def _ellipsize(s, maxsize):
if len(s) > maxsize:
i = max(0, (maxsize - 3) // 2)
j = max(0, maxsize - 3 - i)
return s[:i] + "..." + s[len(s) - j :]
return s
class SafeRepr(reprlib.Repr):
@ -28,18 +32,18 @@ class SafeRepr(reprlib.Repr):
self.maxsize = maxsize
def repr(self, x):
return self._callhelper(reprlib.Repr.repr, self, x)
try:
s = super().repr(x)
except Exception as exc:
s = _format_repr_exception(exc, x)
return _ellipsize(s, self.maxsize)
def repr_instance(self, x, level):
return self._callhelper(repr, x)
def _callhelper(self, call, x, *args):
s = _call_and_format_exception(call, x, *args)
if len(s) > self.maxsize:
i = max(0, (self.maxsize - 3) // 2)
j = max(0, self.maxsize - 3 - i)
s = s[:i] + "..." + s[len(s) - j :]
return s
try:
s = repr(x)
except Exception as exc:
s = _format_repr_exception(exc, x)
return _ellipsize(s, self.maxsize)
def safeformat(obj):
@ -47,7 +51,10 @@ def safeformat(obj):
Failing __repr__ functions of user instances will be represented
with a short exception info.
"""
return _call_and_format_exception(pprint.pformat, obj)
try:
return pprint.pformat(obj)
except Exception as exc:
return _format_repr_exception(exc, obj)
def saferepr(obj, maxsize=240):

View File

@ -45,6 +45,17 @@ def test_exceptions():
assert "unknown" in s2
def test_buggy_builtin_repr():
# Simulate a case where a repr for a builtin raises.
# reprlib dispatches by type name, so use "int".
class int:
def __repr__(self):
raise ValueError("Buggy repr!")
assert "Buggy" in saferepr(int())
def test_big_repr():
from _pytest._io.saferepr import SafeRepr