PEP-263 for detecting Python file encoding
(text: bytes)
| 123 | |
| 124 | |
| 125 | def find_python_encoding(text: bytes) -> tuple[str, int]: |
| 126 | """PEP-263 for detecting Python file encoding""" |
| 127 | result = ENCODING_RE.match(text) |
| 128 | if result: |
| 129 | line = 2 if result.group(1) else 1 |
| 130 | encoding = result.group(3).decode("ascii") |
| 131 | # Handle some aliases that Python is happy to accept and that are used in the wild. |
| 132 | if encoding.startswith(("iso-latin-1-", "latin-1-")) or encoding == "iso-latin-1": |
| 133 | encoding = "latin-1" |
| 134 | return encoding, line |
| 135 | else: |
| 136 | default_encoding = "utf8" |
| 137 | return default_encoding, -1 |
| 138 | |
| 139 | |
| 140 | def bytes_to_human_readable_repr(b: bytes) -> str: |
no test coverage detected
searching dependent graphs…