MCPcopy
hub / github.com/psf/black / decode_bytes

Function decode_bytes

src/black/__init__.py:1321–1361  ·  view source on GitHub ↗

Return a tuple of (decoded_contents, encoding, newline). `newline` is either CRLF, LF, or CR, but `decoded_contents` is decoded with universal newlines (i.e. only contains LF). Use the keyword only encoding_overwrite argument if the bytes are encoded differently to their possible e

(
    src: bytes, mode: Mode, *, encoding_overwrite: str | None = None
)

Source from the content-addressed store, hash-verified

1319
1320
1321def decode_bytes(
1322 src: bytes, mode: Mode, *, encoding_overwrite: str | None = None
1323) -> tuple[FileContent, Encoding, NewLine]:
1324 """Return a tuple of (decoded_contents, encoding, newline).
1325
1326 `newline` is either CRLF, LF, or CR, but `decoded_contents` is decoded with
1327 universal newlines (i.e. only contains LF).
1328
1329 Use the keyword only encoding_overwrite argument if the bytes are encoded
1330 differently to their possible encoding magic comment.
1331 """
1332 srcbuf = io.BytesIO(src)
1333
1334 # Still use detect encoding even if overwrite set because otherwise lines
1335 # might be different
1336 encoding, lines = tokenize.detect_encoding(srcbuf.readline)
1337 if encoding_overwrite is not None:
1338 encoding = encoding_overwrite
1339
1340 if not lines:
1341 return "", encoding, "\n"
1342
1343 if lines[0][-2:] == b"\r\n":
1344 if b"\r" in lines[0][:-2]:
1345 newline = "\r"
1346 else:
1347 newline = "\r\n"
1348 elif lines[0][-1:] == b"\n":
1349 if b"\r" in lines[0][:-1]:
1350 newline = "\r"
1351 else:
1352 newline = "\n"
1353 else:
1354 if b"\r" in lines[0]:
1355 newline = "\r"
1356 else:
1357 newline = "\n"
1358
1359 srcbuf.seek(0)
1360 with io.TextIOWrapper(srcbuf, encoding) as tiow:
1361 return tiow.read(), encoding, newline
1362
1363
1364def get_features_used(

Callers 3

format_file_in_placeFunction · 0.85
format_stdin_to_stdoutFunction · 0.85
_format_str_onceFunction · 0.85

Calls 1

readMethod · 0.80

Tested by

no test coverage detected