Print a text replacing chars not representable in stdout encoding.
(text: str)
| 2495 | |
| 2496 | |
| 2497 | def safe_print(text: str) -> None: |
| 2498 | """Print a text replacing chars not representable in stdout encoding.""" |
| 2499 | # If `sys.stdout` encoding is not the same as out (usually UTF8) string, |
| 2500 | # if may cause painful crashes. I don't want to reconfigure `sys.stdout` |
| 2501 | # to do `errors = "replace"` as that sounds scary. |
| 2502 | out_encoding = sys.stdout.encoding |
| 2503 | if out_encoding is not None: |
| 2504 | # Can be None if stdout is replaced (including our own tests). This should be |
| 2505 | # safe to omit if the actual stream doesn't care about encoding. |
| 2506 | text = text.encode(out_encoding, errors="replace").decode(out_encoding, errors="replace") |
| 2507 | print(text) |
| 2508 | |
| 2509 | |
| 2510 | def parse_options(args: list[str]) -> _Arguments: |
no test coverage detected
searching dependent graphs…