Skip to content

Commit

Permalink
Fix the test that broke after 3.12 changes to locale.normalize (#452)
Browse files Browse the repository at this point in the history
* Fix the test that broke after 3.12 changes to locale.normalize

- On Linux, C.UTF-8 locale is now part of the standard. macOS and Windows
  use en_US.UTF-8 instead.
- Up to 3.11, `locale.normalize` was converting `C.UTF-8` into `en_US.UTF-8`.
  If you normalized the locale string, it was impossible to set `C.UTF-8`.
- In 3.12, `C.UTF-8` maps to itself.
- python/cpython#14925
- https://peps.python.org/pep-0538/
- `C.UTF-8` doesn't seem to be used anywhere else in the codebase, except for this one test.
- Other fix would be to inject a previous value to `locale.locale_alias` dictionary
  for certain platforms. However, this is part of the private implementation
  and could change without prior notice.
- More changes to the locale are scheduled for 3.15.

* Format applied

* Added changelog entry for towncrier

* Prepended + to the changelog filename, as it's not closing any GitHub ticket

* Removed TODO mentioning a broken locale in CI

* Re-enabled 3.12 test matrix for both Windows and macOS
  • Loading branch information
kkalinowski-reef committed Dec 5, 2023
1 parent 23d8572 commit e0edd22
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ jobs:
python-version: "pypy-3.10"
- os: "windows-latest"
python-version: "pypy-3.10"
# TODO: 3.12 setlocale(...) is broken on Windows & Mac
- os: "macos-latest"
python-version: "3.12"
- os: "windows-latest"
python-version: "3.12"
steps:
- uses: actions/checkout@v3
with:
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+py312-setlocale-fail.infrastructure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed tests failing because of changes made to `locale.normalize` in Python 3.12.
9 changes: 6 additions & 3 deletions test/unit/b2http/test_b2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import datetime
import locale
import sys
from unittest.mock import MagicMock, call, patch

import apiver_deps
Expand Down Expand Up @@ -306,9 +307,11 @@ class TestB2HttpUserAgentAppend(TestB2Http):

class TestSetLocaleContextManager(TestBase):
def test_set_locale_context_manager(self):
test_locale = locale.normalize(
'C.utf8'
) # C.UTF-8 on Ubuntu 18.04 Bionic, C.utf8 on Ubuntu 22.04 Jammy
# C.UTF-8 on Ubuntu 18.04 Bionic, C.utf8 on Ubuntu 22.04 Jammy
# Neither macOS nor Windows have C.UTF-8 locale, and they use `en_US.UTF-8`.
# Since Python 3.12, locale.normalize no longer falls back
# to the `en_US` version, so we're providing it here manually.
test_locale = locale.normalize('C.UTF-8' if sys.platform == 'linux' else 'en_US.UTF-8')
other_locale = 'C'

saved = locale.setlocale(locale.LC_ALL)
Expand Down

0 comments on commit e0edd22

Please sign in to comment.