Skip to content

Commit

Permalink
Remove deprecated locale.*() functions
Browse files Browse the repository at this point in the history
These functions are deprecated since Python 3.11.
- `getdefaultlocale()` can be replaced with `getlocale(category=)`
- `resetlocale()` can be replaced with `setlocale(category=, locale=)`
  • Loading branch information
m-horky authored and ptoscano committed Jan 11, 2024
1 parent 4b4ebfb commit 8ef41ce
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/rhsmlib/facts/host_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/rhsm/unit/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions test/rhsmlib/facts/test_host_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit 8ef41ce

Please sign in to comment.