From 4e38e2ae9d7380015349e6aee59c78bb3938befd Mon Sep 17 00:00:00 2001 From: Simon Sawicki Date: Sun, 15 Oct 2023 10:54:38 +0200 Subject: [PATCH] [rh:requests] Handle both `bytes` and `int` for `IncompleteRead.partial` (Fix 8a8b54523addf46dfd50ef599761a81bc22362e6) (#8348) Authored by: bashonly, coletdjnz, Grub4K --- test/test_networking.py | 2 +- yt_dlp/networking/_requests.py | 11 +++++------ yt_dlp/networking/exceptions.py | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/test/test_networking.py b/test/test_networking.py index 2b45deac7..689161fb2 100644 --- a/test/test_networking.py +++ b/test/test_networking.py @@ -865,7 +865,7 @@ def request(self, *args, **kwargs): '3 bytes read, 4 more expected' ), ( - lambda: urllib3.exceptions.IncompleteRead(partial=3, expected=5), + lambda: urllib3.exceptions.ProtocolError('error', urllib3.exceptions.IncompleteRead(partial=3, expected=5)), IncompleteRead, '3 bytes read, 5 more expected' ), diff --git a/yt_dlp/networking/_requests.py b/yt_dlp/networking/_requests.py index 27974357a..fe3f60b0b 100644 --- a/yt_dlp/networking/_requests.py +++ b/yt_dlp/networking/_requests.py @@ -142,18 +142,17 @@ def read(self, amt: int = None): except urllib3.exceptions.SSLError as e: raise SSLError(cause=e) from e - except urllib3.exceptions.IncompleteRead as e: - # urllib3 IncompleteRead.partial is always an integer - raise IncompleteRead(partial=e.partial, expected=e.expected) from e - except urllib3.exceptions.ProtocolError as e: - # http.client.IncompleteRead may be contained within ProtocolError + # IncompleteRead is always contained within ProtocolError # See urllib3.response.HTTPResponse._error_catcher() ir_err = next( (err for err in (e.__context__, e.__cause__, *variadic(e.args)) if isinstance(err, http.client.IncompleteRead)), None) if ir_err is not None: - raise IncompleteRead(partial=len(ir_err.partial), expected=ir_err.expected) from e + # `urllib3.exceptions.IncompleteRead` is subclass of `http.client.IncompleteRead` + # but uses an `int` for its `partial` property. + partial = ir_err.partial if isinstance(ir_err.partial, int) else len(ir_err.partial) + raise IncompleteRead(partial=partial, expected=ir_err.expected) from e raise TransportError(cause=e) from e except urllib3.exceptions.HTTPError as e: diff --git a/yt_dlp/networking/exceptions.py b/yt_dlp/networking/exceptions.py index f58dc246e..12441901c 100644 --- a/yt_dlp/networking/exceptions.py +++ b/yt_dlp/networking/exceptions.py @@ -75,7 +75,7 @@ def __repr__(self): class IncompleteRead(TransportError): - def __init__(self, partial: int, expected: int = None, **kwargs): + def __init__(self, partial: int, expected: int | None = None, **kwargs): self.partial = partial self.expected = expected msg = f'{partial} bytes read'