MCPcopy
hub / github.com/python/mypy / decode_python_encoding

Function decode_python_encoding

mypy/util.py:161–178  ·  view source on GitHub ↗

Read the Python file with while obeying PEP-263 encoding detection. Returns the source as a string.

(source: bytes)

Source from the content-addressed store, hash-verified

159
160
161def 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
181def read_py_file(path: str, read: Callable[[str], bytes]) -> list[str] | None:

Callers 2

get_sourceMethod · 0.90
read_py_fileFunction · 0.85

Calls 5

find_python_encodingFunction · 0.85
DecodeErrorClass · 0.85
strClass · 0.85
startswithMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…