Read the Python file with while obeying PEP-263 encoding detection. Returns the source as a string.
(source: bytes)
| 159 | |
| 160 | |
| 161 | def decode_python_encoding(source: bytes) -> str: |
| 162 | """Read the Python file with while obeying PEP-263 encoding detection. |
| 163 | |
| 164 | Returns the source as a string. |
| 165 | """ |
| 166 | # check for BOM UTF-8 encoding and strip it out if present |
| 167 | if source.startswith(b"\xef\xbb\xbf"): |
| 168 | encoding = "utf8" |
| 169 | source = source[3:] |
| 170 | else: |
| 171 | # look at first two lines and check if PEP-263 coding is present |
| 172 | encoding, _ = find_python_encoding(source) |
| 173 | |
| 174 | try: |
| 175 | source_text = source.decode(encoding) |
| 176 | except LookupError as lookuperr: |
| 177 | raise DecodeError(str(lookuperr)) from lookuperr |
| 178 | return source_text |
| 179 | |
| 180 | |
| 181 | def read_py_file(path: str, read: Callable[[str], bytes]) -> list[str] | None: |
no test coverage detected
searching dependent graphs…