Skip to content

Commit a159fc5

Browse files
committed
fix: Dont throw when user agent construction fails
1 parent ec90ae3 commit a159fc5

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## Unreleased
9+
### Fixed
10+
* Catch failures while constructing the user agent string.
11+
12+
813
## [1.15.0] - 2023-06-09
914
### Fixed
1015
* Removed `CHANGELOG.md` and `SECURITY.md` from the python package. The `LICENSE` file is no longer installed, but still included in package tarball and wheel files.

deepl/http_client.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import platform
99
import random
1010
import requests
11+
import traceback
1112
import time
1213
from functools import lru_cache
1314
from typing import Dict, Optional, Tuple, Union
1415
from .util import log_info
16+
from deepl import util
1517

1618

1719
user_agent = None
@@ -247,11 +249,17 @@ def _generate_user_agent(
247249
else:
248250
library_info_str = f"deepl-python/{version.VERSION}"
249251
if send_platform_info:
250-
library_info_str += (
251-
f" ({platform.platform()}) "
252-
f"python/{platform.python_version()} "
253-
f"requests/{requests.__version__}"
254-
)
252+
try:
253+
library_info_str += (
254+
f" ({platform.platform()}) "
255+
f"python/{platform.python_version()} "
256+
f"requests/{requests.__version__}"
257+
)
258+
except Exception:
259+
util.log_info(
260+
"Exception when querying platform information:\n"
261+
+ traceback.format_exc()
262+
)
255263
if app_info_name and app_info_version:
256264
library_info_str += f" {app_info_name}/{app_info_version}"
257265
return library_info_str

tests/test_general.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ def test_custom_user_agent_with_app_info(mock_send):
168168
deepl.http_client.user_agent = old_user_agent
169169

170170

171+
@patch("requests.adapters.HTTPAdapter.send")
172+
@patch("platform.platform")
173+
def test_user_agent_exception(platform_mock, mock_send):
174+
mock_send.return_value = _build_test_response()
175+
platform_mock.side_effect = OSError("mocked test exception")
176+
translator = deepl.Translator(os.environ["DEEPL_AUTH_KEY"])
177+
translator.translate_text(example_text["EN"], target_lang="DA")
178+
ua_header = mock_send.call_args[0][0].headers["User-agent"]
179+
assert "deepl-python" in ua_header
180+
assert "requests/" not in ua_header
181+
assert " python/" not in ua_header
182+
assert "(" not in ua_header
183+
184+
171185
@needs_mock_proxy_server
172186
def test_proxy_usage(
173187
server,

0 commit comments

Comments
 (0)