Skip to content

A feature-rich command-line audio/video downloader

[rh:requests] Do not allocate 2GB on read (#13603)

Simon Sawickic316416b972d
diff --git a/yt_dlp/networking/_requests.py b/yt_dlp/networking/_requests.py
index 2927ea7ffb..555c21ac33 100644
--- a/yt_dlp/networking/_requests.py
+++ b/yt_dlp/networking/_requests.py
@@ -143,7 +143,9 @@ class RequestsResponseAdapter(Response):
# Work around issue with `.read(amt)` then `.read()`
# See: https://github.com/urllib3/urllib3/issues/3636
if amt is None:
- amt = (1 << 31) - 1
+ # Python 3.9 preallocates the whole read buffer, read in chunks
+ read_chunk = functools.partial(self.fp.read, 1 << 20, decode_content=True)
+ return b''.join(iter(read_chunk, b''))
# Interact with urllib3 response directly.
return self.fp.read(amt, decode_content=True)