Remove reprlib because it's not reliable for simple cases

This commit is contained in:
Ram Rachum 2019-05-09 09:15:59 +03:00
parent 0057d91c46
commit 0482853d96
2 changed files with 20 additions and 35 deletions

View file

@ -6,13 +6,7 @@ import abc
from .pycompat import ABC
from .third_party import six
try:
import reprlib
import builtins
except ImportError:
import repr as reprlib
import __builtin__ as builtins
MAX_VARIABLE_LENGTH = 100
def _check_methods(C, *methods):
mro = C.__mro__
@ -54,36 +48,27 @@ def shitcode(s):
)
class Repr(reprlib.Repr, object): # reprlib.Repr is old-style in Python 2
def __init__(self):
super(Repr, self).__init__()
self.maxother = 100
def repr(self, x):
try:
return super(Repr, self).repr(x)
except Exception as e:
return '<{} instance at {:#x} (__repr__ raised {})>'.format(
x.__class__.__name__, id(x), e.__class__.__name__)
def repr_instance(self, x, level):
s = builtins.repr(x)
if len(s) > self.maxother:
i = max(0, (self.maxother - 3) // 2)
j = max(0, self.maxother - 3 - i)
s = s[:i] + '...' + s[len(s) - j:]
return s
repr_instance = Repr()
def get_shortish_repr(item):
r = repr_instance.repr(item)
try:
r = repr(item)
except Exception:
r = 'REPR FAILED'
r = r.replace('\r', '').replace('\n', '')
if len(r) > MAX_VARIABLE_LENGTH:
r = '{truncated_r}...'.format(truncated_r=r[:MAX_VARIABLE_LENGTH])
return r
def ensure_tuple(x):
if isinstance(x, six.string_types):
x = (x,)

View file

@ -280,10 +280,10 @@ def test_long_variable():
(
CallEntry('def my_function():'),
LineEntry('foo = list(range(1000))'),
VariableEntry('foo', '[0, 1, 2, 3, 4, 5, ...]'),
VariableEntry('foo', value_regex=r'''^\[0, 1, 2, .*\.\.\.$'''),
LineEntry(),
ReturnEntry(),
ReturnValueEntry('[0, 1, 2, 3, 4, 5, ...]')
ReturnValueEntry(value_regex=r'''^\[0, 1, 2, .*\.\.\.$''')
)
)
@ -308,7 +308,7 @@ def test_repr_exception():
VariableEntry('Bad'),
CallEntry('def my_function():'),
LineEntry('bad = Bad()'),
VariableEntry('bad', value_regex=r'<Bad instance at 0x\w+ \(__repr__ raised ZeroDivisionError\)>'),
VariableEntry('bad', value='REPR FAILED'),
ReturnEntry(),
ReturnValueEntry('None')
)