diff options
author | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-02-14 00:49:36 +0300 |
---|---|---|
committer | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-02-14 00:49:36 +0300 |
commit | 82cfd1b7cab2d843cdf5467d9737f72597a493bd (patch) | |
tree | 1dfdcfe81a1a6b193ceacc2a828c521b657a339b /contrib/python/pytest/py3/_pytest/_io/saferepr.py | |
parent | 3df7211d3e3691f8e33b0a1fb1764fe810d59302 (diff) | |
download | ydb-82cfd1b7cab2d843cdf5467d9737f72597a493bd.tar.gz |
intermediate changes
ref:68b1302de4b5da30b6bdf02193f7a2604d8b5cf8
Diffstat (limited to 'contrib/python/pytest/py3/_pytest/_io/saferepr.py')
-rw-r--r-- | contrib/python/pytest/py3/_pytest/_io/saferepr.py | 46 |
1 files changed, 35 insertions, 11 deletions
diff --git a/contrib/python/pytest/py3/_pytest/_io/saferepr.py b/contrib/python/pytest/py3/_pytest/_io/saferepr.py index 5eb1e08890..e7ff5cab20 100644 --- a/contrib/python/pytest/py3/_pytest/_io/saferepr.py +++ b/contrib/python/pytest/py3/_pytest/_io/saferepr.py @@ -12,7 +12,7 @@ def _try_repr_or_str(obj: object) -> str: except (KeyboardInterrupt, SystemExit): raise except BaseException: - return '{}("{}")'.format(type(obj).__name__, obj) + return f'{type(obj).__name__}("{obj}")' def _format_repr_exception(exc: BaseException, obj: object) -> str: @@ -21,7 +21,7 @@ def _format_repr_exception(exc: BaseException, obj: object) -> str: except (KeyboardInterrupt, SystemExit): raise except BaseException as exc: - exc_info = "unpresentable exception ({})".format(_try_repr_or_str(exc)) + exc_info = f"unpresentable exception ({_try_repr_or_str(exc)})" return "<[{} raised in repr()] {} object at 0x{:x}>".format( exc_info, type(obj).__name__, id(obj) ) @@ -36,12 +36,23 @@ def _ellipsize(s: str, maxsize: int) -> str: class SafeRepr(reprlib.Repr): - """repr.Repr that limits the resulting size of repr() and includes - information on exceptions raised during the call.""" + """ + repr.Repr that limits the resulting size of repr() and includes + information on exceptions raised during the call. + """ - def __init__(self, maxsize: int) -> None: + def __init__(self, maxsize: Optional[int]) -> None: + """ + :param maxsize: + If not None, will truncate the resulting repr to that specific size, using ellipsis + somewhere in the middle to hide the extra text. + If None, will not impose any size limits on the returning repr. + """ super().__init__() - self.maxstring = maxsize + # ``maxstring`` is used by the superclass, and needs to be an int; using a + # very large number in case maxsize is None, meaning we want to disable + # truncation. + self.maxstring = maxsize if maxsize is not None else 1_000_000_000 self.maxsize = maxsize def repr(self, x: object) -> str: @@ -51,7 +62,9 @@ class SafeRepr(reprlib.Repr): raise except BaseException as exc: s = _format_repr_exception(exc, x) - return _ellipsize(s, self.maxsize) + if self.maxsize is not None: + s = _ellipsize(s, self.maxsize) + return s def repr_instance(self, x: object, level: int) -> str: try: @@ -60,7 +73,9 @@ class SafeRepr(reprlib.Repr): raise except BaseException as exc: s = _format_repr_exception(exc, x) - return _ellipsize(s, self.maxsize) + if self.maxsize is not None: + s = _ellipsize(s, self.maxsize) + return s def safeformat(obj: object) -> str: @@ -75,7 +90,11 @@ def safeformat(obj: object) -> str: return _format_repr_exception(exc, obj) -def saferepr(obj: object, maxsize: int = 240) -> str: +# Maximum size of overall repr of objects to display during assertion errors. +DEFAULT_REPR_MAX_SIZE = 240 + + +def saferepr(obj: object, maxsize: Optional[int] = DEFAULT_REPR_MAX_SIZE) -> str: """Return a size-limited safe repr-string for the given object. Failing __repr__ functions of user instances will be represented @@ -83,7 +102,7 @@ def saferepr(obj: object, maxsize: int = 240) -> str: care to never raise exceptions itself. This function is a wrapper around the Repr/reprlib functionality of the - standard 2.6 lib. + stdlib. """ return SafeRepr(maxsize).repr(obj) @@ -107,7 +126,12 @@ class AlwaysDispatchingPrettyPrinter(pprint.PrettyPrinter): if objid in context or p is None: # Type ignored because _format is private. super()._format( # type: ignore[misc] - object, stream, indent, allowance, context, level, + object, + stream, + indent, + allowance, + context, + level, ) return |