mirror of
https://github.com/cool-RR/PySnooper.git
synced 2026-01-23 10:15:11 +00:00
More informative repr for exceptions. Forgot to use repr_instance
This commit is contained in:
parent
4ec976bd14
commit
0322982875
2 changed files with 51 additions and 3 deletions
|
|
@ -15,12 +15,33 @@ from .third_party import six
|
|||
|
||||
ipython_filename_pattern = re.compile('^<ipython-input-([0-9]+)-.*>$')
|
||||
|
||||
repr_instance = reprlib.Repr()
|
||||
repr_instance.maxother = 100
|
||||
|
||||
class MyRepr(reprlib.Repr):
|
||||
def __init__(self):
|
||||
super(MyRepr, self).__init__()
|
||||
self.maxother = 100
|
||||
|
||||
def repr(self, x):
|
||||
try:
|
||||
return super(MyRepr, 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 = reprlib.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 = MyRepr()
|
||||
|
||||
|
||||
def get_shortish_repr(item):
|
||||
r = reprlib.repr(item)
|
||||
r = repr_instance.repr(item)
|
||||
r = r.replace('\r', '').replace('\n', '')
|
||||
return r
|
||||
|
||||
|
|
|
|||
|
|
@ -158,6 +158,33 @@ def test_long_variable():
|
|||
)
|
||||
|
||||
|
||||
def test_repr_exception():
|
||||
class Bad(object):
|
||||
def __repr__(self):
|
||||
1 / 0
|
||||
|
||||
@pysnooper.snoop()
|
||||
def my_function():
|
||||
bad = Bad()
|
||||
|
||||
with sys_tools.OutputCapturer(stdout=False,
|
||||
stderr=True) as output_capturer:
|
||||
result = my_function()
|
||||
assert result is None
|
||||
output = output_capturer.string_io.getvalue()
|
||||
assert_output(
|
||||
output,
|
||||
(
|
||||
VariableEntry('Bad'),
|
||||
CallEntry('def my_function():'),
|
||||
LineEntry('bad = Bad()'),
|
||||
VariableEntry('bad', value_regex=r'<Bad instance at 0x\w+ \(__repr__ raised ZeroDivisionError\)>'),
|
||||
ReturnEntry(),
|
||||
ReturnValueEntry('None')
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_depth():
|
||||
string_io = io.StringIO()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue