From 607eeee21c03f953ecc4fa67a8c3fb456129a970 Mon Sep 17 00:00:00 2001 From: lenamonj Date: Tue, 1 Sep 2026 19:48:55 -0400 Subject: [PATCH] Fix null_string_locale_max so it sorts after every PyICU key b"x7f" is three ASCII bytes, not the byte 0x7f, and any byte string starting above 0x78 sorted after it. Use b"\xff" so the NUMAFTER sentinel really is the maximum. --- natsort/compat/locale.py | 2 +- tests/test_compat_locale.py | 45 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/test_compat_locale.py 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