summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Morton <seth.m.morton@gmail.com>2023-03-01 20:11:19 -0800
committerSeth Morton <seth.m.morton@gmail.com>2023-03-01 20:53:09 -0800
commitdef59286fc678e366f13c1184fa3548bf4680144 (patch)
treed925339cb477ff983c3be985734b80d044a5b128
parent0f993d00d6c5120e0773452977ae3ce6a36dccb9 (diff)
downloadnatsort-def59286fc678e366f13c1184fa3548bf4680144.tar.gz
Add FreeBSD fix for locale failure
FreeBSD seems to throw an OSError when locale.strxfrm is given 'Å', which is surprising behavior. Well, maybe not, considering how many bugs I have found with FreeBSD's implementation of locale over the course of natsort development. Anyway, we just ignore any input that causes locale.strxfrm to barf in our tests.
-rw-r--r--natsort/compat/fake_fastnumbers.py2
-rw-r--r--tests/test_string_component_transform_factory.py17
2 files changed, 17 insertions, 2 deletions
diff --git a/natsort/compat/fake_fastnumbers.py b/natsort/compat/fake_fastnumbers.py
index cb1c900..430345a 100644
--- a/natsort/compat/fake_fastnumbers.py
+++ b/natsort/compat/fake_fastnumbers.py
@@ -55,7 +55,7 @@ def fast_float(
String to attempt to convert to a float.
key : callable
Single-argument function to apply to *x* if conversion fails.
- nan : object
+ nan : float
Value to return instead of NaN if NaN would be returned.
Returns
diff --git a/tests/test_string_component_transform_factory.py b/tests/test_string_component_transform_factory.py
index bc48d5b..40b4d34 100644
--- a/tests/test_string_component_transform_factory.py
+++ b/tests/test_string_component_transform_factory.py
@@ -5,7 +5,7 @@ from functools import partial
from typing import Any, Callable, FrozenSet, Union
import pytest
-from hypothesis import example, given
+from hypothesis import assume, example, given
from hypothesis.strategies import floats, integers, text
from natsort.compat.fastnumbers import try_float, try_int
from natsort.compat.locale import get_strxfrm
@@ -32,6 +32,20 @@ def no_null(x: str) -> bool:
return "\0" not in x
+def input_is_ok_with_locale(x: str) -> bool:
+ """Ensure this input won't cause locale.strxfrm to barf"""
+ # On FreeBSD, locale.strxfrm raises an OSError on input like 'Å'.
+ # You read that right - an *OSError* for invalid input.
+ # We cannot really fix that, so we just filter out any value
+ # that could cause locale.strxfrm to barf with this function.
+ try:
+ get_strxfrm()(x)
+ except OSError:
+ return False
+ else:
+ return True
+
+
@pytest.mark.parametrize(
"alg, example_func",
[
@@ -77,6 +91,7 @@ def test_string_component_transform_factory(
) -> None:
string_component_transform_func = string_component_transform_factory(alg)
x = str(x)
+ assume(input_is_ok_with_locale(x)) # handle broken locale lib on BSD.
try:
assert list(string_component_transform_func(x)) == list(example_func(x))
except ValueError as e: # handle broken locale lib on BSD.