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)
| 411 | |
| 412 | |
| 413 | def _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 | |
| 433 | def _get_maxsize_for_saferepr(config: Config | None) -> int | None: |