From 8ef41ce69d3ec5e5e3079178ea5d1d603d354fe4 Mon Sep 17 00:00:00 2001 From: mhorky Date: Thu, 11 Jan 2024 14:01:00 +0100 Subject: [PATCH] Remove deprecated `locale.*()` functions These functions are deprecated since Python 3.11. - `getdefaultlocale()` can be replaced with `getlocale(category=)` - `resetlocale()` can be replaced with `setlocale(category=, locale=)` --- src/rhsmlib/facts/host_collector.py | 7 ++----- test/rhsm/unit/test_connection.py | 2 +- test/rhsmlib/facts/test_host_collector.py | 6 +++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/rhsmlib/facts/host_collector.py b/src/rhsmlib/facts/host_collector.py index 9740317a0f..799f7b27c9 100644 --- a/src/rhsmlib/facts/host_collector.py +++ b/src/rhsmlib/facts/host_collector.py @@ -62,14 +62,11 @@ def get_all(self) -> Dict[str, Union[str, int, bool, None]]: locale_info = {} effective_locale = "Unknown" - # When there is no locale set (system variable LANG is unset), - # then this is value returned by locale.getdefaultlocale() - # Tuple contains: (language[_territory], encoding identifier) - default_locale = (None, None) try: - default_locale = locale.getdefaultlocale() + default_locale = locale.getlocale(category=locale.LC_MESSAGES) except ValueError as err: log.warning("Unable to get default locale (bad environment variable?): %s" % err) + default_locale = (None, None) if default_locale[0] is not None: effective_locale = ".".join([_f for _f in default_locale if _f]) locale_info["system.default_locale"] = effective_locale diff --git a/test/rhsm/unit/test_connection.py b/test/rhsm/unit/test_connection.py index 0d0cdc3cd1..e6d0212b8f 100644 --- a/test/rhsm/unit/test_connection.py +++ b/test/rhsm/unit/test_connection.py @@ -1067,7 +1067,7 @@ def setUp(self): self.cp = UEPConnection(username="dummy", password="dummy", handler="/Test/", insecure=True) def tearDown(self): - locale.resetlocale() + locale.setlocale(category=locale.LC_ALL, locale="") @patch("subscription_manager.cache.open", MOCK_OPEN_CACHE) def test_date_formatted_properly_with_japanese_locale(self): diff --git a/test/rhsmlib/facts/test_host_collector.py b/test/rhsmlib/facts/test_host_collector.py index 0de7b03861..f763a8798b 100644 --- a/test/rhsmlib/facts/test_host_collector.py +++ b/test/rhsmlib/facts/test_host_collector.py @@ -19,7 +19,7 @@ class HostCollectorTest(unittest.TestCase): - @mock.patch("locale.getdefaultlocale") + @mock.patch("locale.getlocale") def test_unknown_locale(self, mock_locale): collector = host_collector.HostCollector() mock_locale.return_value = (None, None) @@ -28,7 +28,7 @@ def test_unknown_locale(self, mock_locale): self.assertTrue(isinstance(facts, dict)) self.assertEqual(facts["system.default_locale"], "Unknown") - @mock.patch("locale.getdefaultlocale") + @mock.patch("locale.getlocale") def test_en_us_utf8_locale(self, mock_locale): collector = host_collector.HostCollector() mock_locale.return_value = ("en_US", "UTF-8") @@ -37,7 +37,7 @@ def test_en_us_utf8_locale(self, mock_locale): self.assertTrue(isinstance(facts, dict)) self.assertEqual(facts["system.default_locale"], "en_US.UTF-8") - @mock.patch("locale.getdefaultlocale") + @mock.patch("locale.getlocale") def test_en_us_no_encoding_locale(self, mock_locale): collector = host_collector.HostCollector() mock_locale.return_value = ("en_US", None)