From b4e0214ca36a0af8496b12246c7977cb46f33019 Mon Sep 17 00:00:00 2001 From: Rahul Tuli Date: Wed, 31 May 2023 13:19:14 -0400 Subject: [PATCH] Sparsezoo stdout fix (#322) * Revert "Suppress analytics errors and messages (#318)" This reverts commit 960cae23e8273cc7e3cfff9ceb65a8f3ecb8628f. * Catch HttpError over exception * Catch HttpError over exception when checking country (cherry picked from commit 9b5a0bd1cd630ee740bea2aa6f489b689b7f1c94) --- src/sparsezoo/analytics.py | 51 ++++++++++++++++----------------- src/sparsezoo/utils/gdpr.py | 29 +++++++++---------- src/sparsezoo/utils/suppress.py | 45 ----------------------------- 3 files changed, 38 insertions(+), 87 deletions(-) delete mode 100644 src/sparsezoo/utils/suppress.py diff --git a/src/sparsezoo/analytics.py b/src/sparsezoo/analytics.py index e2f719d1..7190ab8c 100644 --- a/src/sparsezoo/analytics.py +++ b/src/sparsezoo/analytics.py @@ -22,9 +22,9 @@ import machineid import requests +from requests import HTTPError from sparsezoo.utils.gdpr import is_gdpr_country -from sparsezoo.utils.suppress import suppress_stdout_stderr from sparsezoo.version import version as sparsezoo_version @@ -122,31 +122,30 @@ def send_event( event_params = {} def _send_request(): - with suppress_stdout_stderr(suppress=not _DEBUG): - event_params.update(self._package_params) - event_params["package"] = self._package - event_params["version"] = self._version - payload = { - "client_id": self._client_id, - "events": [{"name": event_name, "params": event_params}], - } - headers = { - "Content-Type": "application/json", - } - data = json.dumps(payload) - - try: - response = requests.post(self._url, headers=headers, data=data) - response.raise_for_status() - body = response.content - if _DEBUG: - print(body) - except Exception as err: - if _DEBUG: - print(err) - - if raise_errors: - raise err + event_params.update(self._package_params) + event_params["package"] = self._package + event_params["version"] = self._version + payload = { + "client_id": self._client_id, + "events": [{"name": event_name, "params": event_params}], + } + headers = { + "Content-Type": "application/json", + } + data = json.dumps(payload) + + try: + response = requests.post(self._url, headers=headers, data=data) + response.raise_for_status() + body = response.content + if _DEBUG: + print(body) + except HTTPError as http_error: + if _DEBUG: + print(http_error) + + if raise_errors: + raise http_error thread = threading.Thread(target=_send_request) thread.start() diff --git a/src/sparsezoo/utils/gdpr.py b/src/sparsezoo/utils/gdpr.py index 93f391fd..2b83f4df 100644 --- a/src/sparsezoo/utils/gdpr.py +++ b/src/sparsezoo/utils/gdpr.py @@ -16,8 +16,7 @@ import geocoder import requests - -from sparsezoo.utils.suppress import suppress_stdout_stderr +from requests import HTTPError __all__ = ["get_external_ip", "get_country_code", "is_gdpr_country"] @@ -58,28 +57,26 @@ def get_external_ip() -> Optional[str]: """ :return: the external ip of the machine, None if unable to get """ - with suppress_stdout_stderr(): - try: - response = requests.get("https://ident.me") - external_ip = response.text.strip() + try: + response = requests.get("https://ident.me") + external_ip = response.text.strip() - return external_ip - except Exception: - return None + return external_ip + except Exception: + return None def get_country_code() -> Optional[str]: """ :return: the country code of the machine, None if unable to get """ - with suppress_stdout_stderr(): - try: - ip = get_external_ip() - geo = geocoder.ip(ip) + try: + ip = get_external_ip() + geo = geocoder.ip(ip) - return geo.country - except Exception: - return None + return geo.country + except HTTPError: + return None def is_gdpr_country() -> bool: diff --git a/src/sparsezoo/utils/suppress.py b/src/sparsezoo/utils/suppress.py deleted file mode 100644 index 55338d92..00000000 --- a/src/sparsezoo/utils/suppress.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright (c) 2021 - present / Neuralmagic, Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import sys -from contextlib import contextmanager - - -__all__ = ["suppress_stdout_stderr", "NullDevice"] - - -class NullDevice: - def write(self, s): - pass - - -@contextmanager -def suppress_stdout_stderr(suppress: bool = True): - """ - Suppresses stdout and stderr for the duration of the context. - """ - original_stdout = sys.stdout - original_stderr = sys.stderr - null_device = NullDevice() - - try: - if suppress: - # Redirect stdout and stderr to the null device - sys.stdout = null_device - sys.stderr = null_device - yield - finally: - # Restore the original stdout and stderr - sys.stdout = original_stdout - sys.stderr = original_stderr