fix: add Windows O_BINARY guard for binary file reads

Native Windows opens files in text mode by default, which corrupts
binary media files via CRLF translation. O_BINARY prevents this.

This completes the portability story — the PR was already correct on
all supported platforms (Linux/macOS/WSL2), but the round-3 change was
explicitly about portability, so we close the Windows path too.
This commit is contained in:
happy5318 committed 2026-08-12 17:19:27 +08:00
1 parent 72cab65817
commit a959a4c754
1 file changed
+6 -1
+6 -1
View File
@@ -18424,7 +18424,12 @@ def _parse_range_header(range_header: str, file_size: int) -> tuple[int, int] |
def _open_file_read_fd(target: Path, anchor_root: Path | None = None) -> int:
if anchor_root is None:
return os.open(str(target), os.O_RDONLY)
flags = os.O_RDONLY
# On Windows, files are opened in text mode by default; O_BINARY
# prevents CRLF translation that would corrupt binary media files.
if hasattr(os, "O_BINARY"):
flags |= os.O_BINARY
return os.open(str(target), flags)
return open_anchored_fd(anchor_root, target.resolve(), want_dir=False)