saferepr: Use an __init__ instead of setting attributes after construction
This will be easier to type-check, and also somewhat clearer.
This commit is contained in:
parent
0225be53a2
commit
0394ebffee
|
@ -22,6 +22,12 @@ class SafeRepr(reprlib.Repr):
|
||||||
and includes information on exceptions raised during the call.
|
and includes information on exceptions raised during the call.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self, maxsize):
|
||||||
|
super().__init__()
|
||||||
|
self.maxstring = maxsize
|
||||||
|
self.maxsize = maxsize
|
||||||
|
self.maxother = 160
|
||||||
|
|
||||||
def repr(self, x):
|
def repr(self, x):
|
||||||
return self._callhelper(reprlib.Repr.repr, self, x)
|
return self._callhelper(reprlib.Repr.repr, self, x)
|
||||||
|
|
||||||
|
@ -52,9 +58,4 @@ def saferepr(obj, maxsize=240):
|
||||||
care to never raise exceptions itself. This function is a wrapper
|
care to never raise exceptions itself. This function is a wrapper
|
||||||
around the Repr/reprlib functionality of the standard 2.6 lib.
|
around the Repr/reprlib functionality of the standard 2.6 lib.
|
||||||
"""
|
"""
|
||||||
# review exception handling
|
return SafeRepr(maxsize).repr(obj)
|
||||||
srepr = SafeRepr()
|
|
||||||
srepr.maxstring = maxsize
|
|
||||||
srepr.maxsize = maxsize
|
|
||||||
srepr.maxother = 160
|
|
||||||
return srepr.repr(obj)
|
|
||||||
|
|
|
@ -48,7 +48,7 @@ def test_exceptions():
|
||||||
def test_big_repr():
|
def test_big_repr():
|
||||||
from _pytest._io.saferepr import SafeRepr
|
from _pytest._io.saferepr import SafeRepr
|
||||||
|
|
||||||
assert len(saferepr(range(1000))) <= len("[" + SafeRepr().maxlist * "1000" + "]")
|
assert len(saferepr(range(1000))) <= len("[" + SafeRepr(0).maxlist * "1000" + "]")
|
||||||
|
|
||||||
|
|
||||||
def test_repr_on_newstyle():
|
def test_repr_on_newstyle():
|
||||||
|
|
Loading…
Reference in New Issue