From a5e47fe514a610305cc713d8db041054887744be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jelmer=20Vernoo=C4=B3?= Date: Wed, 9 Aug 2023 20:27:54 +0100 Subject: [PATCH 1/2] Raise GitProtocolError when encountering HTTP Errors in HTTPGitClient. Fixes #1199 --- NEWS | 3 +++ dulwich/client.py | 19 ++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index dc7bc644b..054be7e3c 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,9 @@ * objects: Define a stricter return type for _parse_message (Vincent Lorentz) + * Raise GitProtocolError when encountering HTTP Errors in + HTTPGitClient. (Jelmer Vernooij, #1199) + 0.21.5 2023-05-04 * Be more tolerant to non-3-length tuple versions. diff --git a/dulwich/client.py b/dulwich/client.py index 87d69bad9..bf6b9086c 100644 --- a/dulwich/client.py +++ b/dulwich/client.py @@ -1949,6 +1949,8 @@ def _http_request(self, url, headers=None, data=None): redirect_location properties, and read is a consumable read method for the response data. + Raises: + GitProtocolError """ raise NotImplementedError(self._http_request) @@ -2225,13 +2227,16 @@ def _http_request(self, url, headers=None, data=None): req_headers.update(headers) req_headers["Pragma"] = "no-cache" - if data is None: - resp = self.pool_manager.request( - "GET", url, headers=req_headers, preload_content=False) - else: - resp = self.pool_manager.request( - "POST", url, headers=req_headers, body=data, preload_content=False - ) + try: + if data is None: + resp = self.pool_manager.request( + "GET", url, headers=req_headers, preload_content=False) + else: + resp = self.pool_manager.request( + "POST", url, headers=req_headers, body=data, preload_content=False + ) + except urllib3.exceptions.HTTPError as e: + raise GitProtocolError(str(e)) from e if resp.status == 404: raise NotGitRepository() From 47e157ee00f7b2e6782faa9828a06d5218dfb29d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jelmer=20Vernoo=C4=B3?= Date: Wed, 9 Aug 2023 20:35:40 +0100 Subject: [PATCH 2/2] Fix new flake errors --- dulwich/objects.py | 2 +- dulwich/tests/test_client.py | 4 ++-- dulwich/tests/test_repository.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dulwich/objects.py b/dulwich/objects.py index d3b2e88d3..ff5acb5f1 100644 --- a/dulwich/objects.py +++ b/dulwich/objects.py @@ -141,7 +141,7 @@ def hex_to_filename(path, hex): # os.path.join accepts bytes or unicode, but all args must be of the same # type. Make sure that hex which is expected to be bytes, is the same type # as path. - if type(path) != type(hex) and getattr(path, "encode", None) is not None: + if type(path) is not type(hex) and getattr(path, "encode", None) is not None: hex = hex.decode("ascii") dir = hex[:2] file = hex[2:] diff --git a/dulwich/tests/test_client.py b/dulwich/tests/test_client.py index a50e46458..253989551 100644 --- a/dulwich/tests/test_client.py +++ b/dulwich/tests/test_client.py @@ -1538,7 +1538,7 @@ def test_run_command_password_and_privkey(self): ) for w in warnings_list: - if type(w) == type(expected_warning) and w.args == expected_warning.args: + if type(w) is type(expected_warning) and w.args == expected_warning.args: break else: raise AssertionError( @@ -1583,7 +1583,7 @@ def test_run_command_password(self): ) for w in warnings_list: - if type(w) == type(expected_warning) and w.args == expected_warning.args: + if type(w) is type(expected_warning) and w.args == expected_warning.args: break else: raise AssertionError( diff --git a/dulwich/tests/test_repository.py b/dulwich/tests/test_repository.py index fbe6adb61..c19fcc1da 100644 --- a/dulwich/tests/test_repository.py +++ b/dulwich/tests/test_repository.py @@ -842,7 +842,7 @@ def test_shell_hook_post_commit(self): "non-zero status 1", ) for w in warnings_list: - if type(w) == type(expected_warning) and w.args == expected_warning.args: + if type(w) is type(expected_warning) and w.args == expected_warning.args: break else: raise AssertionError(