MCPcopy
hub / github.com/pytest-dev/pytest / _saferepr

Function _saferepr

src/_pytest/assertion/rewrite.py:413–430  ·  view source on GitHub ↗

r"""Get a safe repr of an object for assertion error messages. The assertion formatting (util.format_explanation()) requires newlines to be escaped since they are a special character for it. Normally assertion.util.format_explanation() does this but for a custom repr it is possible

(obj: object)

Source from the content-addressed store, hash-verified

411
412
413def _saferepr(obj: object) -> str:
414 r"""Get a safe repr of an object for assertion error messages.
415
416 The assertion formatting (util.format_explanation()) requires
417 newlines to be escaped since they are a special character for it.
418 Normally assertion.util.format_explanation() does this but for a
419 custom repr it is possible to contain one of the special escape
420 sequences, especially '\n{' and '\n}' are likely to be present in
421 JSON reprs.
422 """
423 if isinstance(obj, types.MethodType):
424 # for bound methods, skip redundant <bound method ...> information
425 return obj.__name__
426
427 maxsize = _get_maxsize_for_saferepr(util._config)
428 if not maxsize:
429 return saferepr_unlimited(obj).replace("\n", "\\n")
430 return saferepr(obj, maxsize=maxsize).replace("\n", "\\n")
431
432
433def _get_maxsize_for_saferepr(config: Config | None) -> int | None:

Callers 2

Calls 3

saferepr_unlimitedFunction · 0.90
safereprFunction · 0.90

Tested by 2