r"""Format the custom assertion message given. For strings this simply replaces newlines with '\n~' so that util.format_explanation() will preserve them instead of escaping newlines. For other objects saferepr() is used first.
(obj: object)
| 451 | |
| 452 | |
| 453 | def _format_assertmsg(obj: object) -> str: |
| 454 | r"""Format the custom assertion message given. |
| 455 | |
| 456 | For strings this simply replaces newlines with '\n~' so that |
| 457 | util.format_explanation() will preserve them instead of escaping |
| 458 | newlines. For other objects saferepr() is used first. |
| 459 | """ |
| 460 | # reprlib appears to have a bug which means that if a string |
| 461 | # contains a newline it gets escaped, however if an object has a |
| 462 | # .__repr__() which contains newlines it does not get escaped. |
| 463 | # However in either case we want to preserve the newline. |
| 464 | replaces = [("\n", "\n~"), ("%", "%%")] |
| 465 | if not isinstance(obj, str): |
| 466 | obj = saferepr(obj) |
| 467 | replaces.append(("\\n", "\n~")) |
| 468 | |
| 469 | for r1, r2 in replaces: |
| 470 | obj = obj.replace(r1, r2) |
| 471 | |
| 472 | return obj |
| 473 | |
| 474 | |
| 475 | def _should_repr_global_name(obj: object) -> bool: |