diff --git a/natsort/compat/locale.py b/natsort/compat/locale.py index f387fd0..6713fcc 100644 --- a/natsort/compat/locale.py +++ b/natsort/compat/locale.py @@ -35,7 +35,7 @@ # This string should in theory be sorted after any other byte # string because it contains the max byte char repeated many times. # You would need some odd data to come after that. - null_string_locale_max = b"x7f" * 50 + null_string_locale_max = b"\xff" * 50 def dumb_sort() -> bool: """Determine if the locale backend is not collating correctly.""" diff --git a/tests/test_compat_locale.py b/tests/test_compat_locale.py new file mode 100644 index 0000000..f0f9f1e --- /dev/null +++ b/tests/test_compat_locale.py @@ -0,0 +1,45 @@ +"""Test the locale compatibility layer.""" + +from __future__ import annotations + +import importlib.util +import sys +import types +from typing import TYPE_CHECKING + +import pytest + +import natsort.compat.locale + +if TYPE_CHECKING: + from collections.abc import Iterator + + +@pytest.fixture +def icu_locale_module() -> Iterator[types.ModuleType]: + """Load natsort/compat/locale.py with a stub "icu" module in place.""" + saved = sys.modules.get("icu") + sys.modules["icu"] = types.ModuleType("icu") + spec = importlib.util.spec_from_file_location( + "_icu_locale", natsort.compat.locale.__file__ + ) + assert spec is not None + assert spec.loader is not None + module = importlib.util.module_from_spec(spec) + try: + spec.loader.exec_module(module) + yield module + finally: + if saved is None: + del sys.modules["icu"] + else: + sys.modules["icu"] = saved + + +def test_icu_null_string_locale_max_sorts_after_any_bytes( + icu_locale_module: types.ModuleType, +) -> None: + # Prepended to numbers under ns.NUMAFTER | ns.LOCALE, so it must sort + # after every sort key PyICU can return. + assert icu_locale_module.null_string_locale_max > b"\xff" * 49 + assert icu_locale_module.null_string_locale_max > b"\xfe" * 100