summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth M Morton <seth.m.morton@gmail.com>2018-02-08 22:52:58 -0800
committerSeth M Morton <seth.m.morton@gmail.com>2018-02-10 13:21:56 -0800
commit48b50476044860764f2e4cd1f76e0e152940abdc (patch)
treeb58f0d9269273566994913f269d5b01ee3341d85
parent2e474460d65998dd72f1db56556b03cb3f69f806 (diff)
downloadnatsort-48b50476044860764f2e4cd1f76e0e152940abdc.tar.gz
natsort.compat.(null_string -> null_string_locale).
This is in preparation for adding a few new versions of the null string, one which will be sorted after other strings, and another pair which will be used when locale is not enabled.
-rw-r--r--natsort/compat/locale.py8
-rw-r--r--natsort/natsort.py5
-rw-r--r--test_natsort/test_natsort_keygen.py8
-rw-r--r--test_natsort/test_utils.py4
4 files changed, 15 insertions, 10 deletions
diff --git a/natsort/compat/locale.py b/natsort/compat/locale.py
index 4cbe5f5..cbed495 100644
--- a/natsort/compat/locale.py
+++ b/natsort/compat/locale.py
@@ -16,7 +16,7 @@ try:
import icu
from locale import getlocale
- null_string = b''
+ null_string_locale = b''
def dumb_sort():
return False
@@ -44,11 +44,13 @@ except ImportError:
if PY_VERSION < 3:
from locale import strcoll
strxfrm = cmp_to_key(strcoll)
- null_string = strxfrm('')
else:
from locale import strxfrm
- null_string = ''
+ null_string_locale = ''
+
+ if PY_VERSION < 3:
+ null_string_locale = cmp_to_key(cmp)(null_string_locale)
# On some systems, locale is broken and does not sort in the expected
# order. We will try to detect this and compensate.
def dumb_sort():
diff --git a/natsort/natsort.py b/natsort/natsort.py
index be887c7..8703498 100644
--- a/natsort/natsort.py
+++ b/natsort/natsort.py
@@ -207,7 +207,10 @@ def natsort_keygen(key=None, alg=0, **_kwargs):
alg |= ns._DUMB
# Set some variables that will be passed to the factory functions
- sep = natsort.compat.locale.null_string if alg & ns.LOCALEALPHA else ''
+ if alg & ns.LOCALEALPHA:
+ sep = natsort.compat.locale.null_string_locale
+ else:
+ sep = ''
regex = _regex_chooser[alg & ns._NUMERIC_ONLY]
# Create the functions that will be used to split strings.
diff --git a/test_natsort/test_natsort_keygen.py b/test_natsort/test_natsort_keygen.py
index e94d3d3..9ea408a 100644
--- a/test_natsort/test_natsort_keygen.py
+++ b/test_natsort/test_natsort_keygen.py
@@ -16,7 +16,7 @@ from natsort import (
)
from natsort.compat.py23 import PY_VERSION
from natsort.compat.locale import (
- null_string,
+ null_string_locale,
get_strxfrm,
)
from compat.mock import patch
@@ -79,9 +79,9 @@ def test_natsort_keygen_splits_input_with_locale():
load_locale('en_US')
strxfrm = get_strxfrm()
with patch('natsort.compat.locale.dumb_sort', return_value=False):
- assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1), (strxfrm('/Folder ('), 1, strxfrm(')/Foo')), (null_string, 56.7))
+ assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string_locale, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1), (strxfrm('/Folder ('), 1, strxfrm(')/Foo')), (null_string_locale, 56.7))
with patch('natsort.compat.locale.dumb_sort', return_value=True):
- assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string, 6, strxfrm('aa--'), 5, strxfrm('..'), 34, strxfrm('eE++'), 1), (strxfrm('//ffoOlLdDeErR (('), 1, strxfrm('))//ffoOoO')), (null_string, 56.7))
+ assert natsort_keygen(alg=ns.L)(INPUT) == ((null_string_locale, 6, strxfrm('aa--'), 5, strxfrm('..'), 34, strxfrm('eE++'), 1), (strxfrm('//ffoOlLdDeErR (('), 1, strxfrm('))//ffoOoO')), (null_string_locale, 56.7))
if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
locale.setlocale(locale.LC_ALL, str(''))
@@ -90,7 +90,7 @@ def test_natsort_keygen_splits_input_with_locale_and_capitalfirst():
load_locale('en_US')
strxfrm = get_strxfrm()
with patch('natsort.compat.locale.dumb_sort', return_value=False):
- assert natsort_keygen(alg=ns.LA | ns.C)(INPUT) == ((('',), (null_string, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1)), (('/',), (strxfrm('/Folder ('), 1, strxfrm(')/Foo'))), (('',), (null_string, 56.7)))
+ assert natsort_keygen(alg=ns.LA | ns.C)(INPUT) == ((('',), (null_string_locale, 6, strxfrm('A-'), 5, strxfrm('.'), 34, strxfrm('e+'), 1)), (('/',), (strxfrm('/Folder ('), 1, strxfrm(')/Foo'))), (('',), (null_string_locale, 56.7)))
if PY_VERSION >= 3: assert natsort_keygen(alg=ns.LA | ns.C)(b'6A-5.034e+1') == (b'6A-5.034e+1',)
locale.setlocale(locale.LC_ALL, str(''))
diff --git a/test_natsort/test_utils.py b/test_natsort/test_utils.py
index cfa06b6..3a367cb 100644
--- a/test_natsort/test_utils.py
+++ b/test_natsort/test_utils.py
@@ -24,7 +24,7 @@ from natsort.utils import (
chain_functions,
)
from natsort.compat.py23 import py23_str, py23_cmp
-from natsort.compat.locale import null_string
+from natsort.compat.locale import null_string_locale
from slow_splitters import (
sep_inserter,
add_leading_space_if_first_is_num,
@@ -195,7 +195,7 @@ def test_sep_inserter_does_nothing_if_only_one_number_example():
def test_sep_inserter_inserts_separator_string_between_two_numbers_example():
assert list(_sep_inserter(iter([5, 9]), '')) == ['', 5, '', 9]
- assert list(_sep_inserter(iter([5, 9]), null_string)) == [null_string, 5, null_string, 9]
+ assert list(_sep_inserter(iter([5, 9]), null_string_locale)) == [null_string_locale, 5, null_string_locale, 9]
@given(lists(elements=text().filter(bool) | integers()))