Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 9b5a0bd

Browse files
authored
Sparsezoo stdout fix (#322)
* Revert "Suppress analytics errors and messages (#318)" This reverts commit 960cae2. * Catch HttpError over exception * Catch HttpError over exception when checking country
1 parent 960cae2 commit 9b5a0bd

File tree

3 files changed

+38
-87
lines changed

3 files changed

+38
-87
lines changed

src/sparsezoo/analytics.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
import machineid
2424
import requests
25+
from requests import HTTPError
2526

2627
from sparsezoo.utils.gdpr import is_gdpr_country
27-
from sparsezoo.utils.suppress import suppress_stdout_stderr
2828
from sparsezoo.version import version as sparsezoo_version
2929

3030

@@ -129,31 +129,30 @@ def send_event(
129129
event_params = {}
130130

131131
def _send_request():
132-
with suppress_stdout_stderr(suppress=not _DEBUG):
133-
event_params.update(self._package_params)
134-
event_params["package"] = self._package
135-
event_params["version"] = self._version
136-
payload = {
137-
"client_id": self._client_id,
138-
"events": [{"name": event_name, "params": event_params}],
139-
}
140-
headers = {
141-
"Content-Type": "application/json",
142-
}
143-
data = json.dumps(payload)
144-
145-
try:
146-
response = requests.post(self._url, headers=headers, data=data)
147-
response.raise_for_status()
148-
body = response.content
149-
if _DEBUG:
150-
print(body)
151-
except Exception as err:
152-
if _DEBUG:
153-
print(err)
154-
155-
if raise_errors:
156-
raise err
132+
event_params.update(self._package_params)
133+
event_params["package"] = self._package
134+
event_params["version"] = self._version
135+
payload = {
136+
"client_id": self._client_id,
137+
"events": [{"name": event_name, "params": event_params}],
138+
}
139+
headers = {
140+
"Content-Type": "application/json",
141+
}
142+
data = json.dumps(payload)
143+
144+
try:
145+
response = requests.post(self._url, headers=headers, data=data)
146+
response.raise_for_status()
147+
body = response.content
148+
if _DEBUG:
149+
print(body)
150+
except HTTPError as http_error:
151+
if _DEBUG:
152+
print(http_error)
153+
154+
if raise_errors:
155+
raise http_error
157156

158157
thread = threading.Thread(target=_send_request)
159158
thread.start()

src/sparsezoo/utils/gdpr.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
import geocoder
1818
import requests
19-
20-
from sparsezoo.utils.suppress import suppress_stdout_stderr
19+
from requests import HTTPError
2120

2221

2322
__all__ = ["get_external_ip", "get_country_code", "is_gdpr_country"]
@@ -58,28 +57,26 @@ def get_external_ip() -> Optional[str]:
5857
"""
5958
:return: the external ip of the machine, None if unable to get
6059
"""
61-
with suppress_stdout_stderr():
62-
try:
63-
response = requests.get("https://ident.me")
64-
external_ip = response.text.strip()
60+
try:
61+
response = requests.get("https://ident.me")
62+
external_ip = response.text.strip()
6563

66-
return external_ip
67-
except Exception:
68-
return None
64+
return external_ip
65+
except Exception:
66+
return None
6967

7068

7169
def get_country_code() -> Optional[str]:
7270
"""
7371
:return: the country code of the machine, None if unable to get
7472
"""
75-
with suppress_stdout_stderr():
76-
try:
77-
ip = get_external_ip()
78-
geo = geocoder.ip(ip)
73+
try:
74+
ip = get_external_ip()
75+
geo = geocoder.ip(ip)
7976

80-
return geo.country
81-
except Exception:
82-
return None
77+
return geo.country
78+
except HTTPError:
79+
return None
8380

8481

8582
def is_gdpr_country() -> bool:

src/sparsezoo/utils/suppress.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)