From 7bb5de0a423d905f490f17b8a019b3319ac9d182 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Wed, 15 Jul 2020 16:41:42 +0200 Subject: [PATCH] remove default locale C.UTF-8 --- docs/configuration.rst | 4 ++-- tests/rules/test_key_ordering.py | 4 ++-- tests/test_cli.py | 24 +++++++++++++++++------- yamllint/cli.py | 3 ++- yamllint/config.py | 2 +- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index a93f21c..67e6fc6 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -197,10 +197,10 @@ It is possible to set the ``locale`` option globally. This is passed to Python's `locale.setlocale `_, so an empty string ``""`` will use the system default locale, while e.g. -``"en_US.UTF-8"`` will use that. If unset, the default is ``"C.UTF-8"``. +``"en_US.UTF-8"`` will use that. Currently this only affects the ``key-ordering`` rule. The default will order -by Unicode code point number, while other locales will sort case and accents +by Unicode code point number, while locales will sort case and accents properly as well. .. code-block:: yaml diff --git a/tests/rules/test_key_ordering.py b/tests/rules/test_key_ordering.py index 54bab4a..7e94b81 100644 --- a/tests/rules/test_key_ordering.py +++ b/tests/rules/test_key_ordering.py @@ -114,7 +114,7 @@ class KeyOrderingTestCase(RuleTestCase): ']\n', conf) def test_locale_case(self): - self.addCleanup(locale.setlocale, locale.LC_ALL, 'C.UTF-8') + self.addCleanup(locale.setlocale, locale.LC_ALL, (None, None)) try: locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') except locale.Error: @@ -133,7 +133,7 @@ class KeyOrderingTestCase(RuleTestCase): problem=(4, 1)) def test_locale_accents(self): - self.addCleanup(locale.setlocale, locale.LC_ALL, 'C.UTF-8') + self.addCleanup(locale.setlocale, locale.LC_ALL, (None, None)) try: locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') except locale.Error: diff --git a/tests/test_cli.py b/tests/test_cli.py index c4478d2..854743b 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -331,11 +331,12 @@ class CommandLineTestCase(unittest.TestCase): self.assertEqual(ctx.returncode, 1) def test_run_with_locale(self): - self.addCleanup(locale.setlocale, locale.LC_ALL, 'C.UTF-8') + self.addCleanup(locale.setlocale, locale.LC_ALL, (None, None)) try: locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') except locale.Error: self.skipTest('locale en_US.UTF-8 not available') + locale.setlocale(locale.LC_ALL, (None, None)) # C + en.yaml should fail with RunContext(self) as ctx: @@ -343,6 +344,12 @@ class CommandLineTestCase(unittest.TestCase): os.path.join(self.wd, 'en.yaml'))) self.assertEqual(ctx.returncode, 1) + # C + c.yaml should pass + with RunContext(self) as ctx: + cli.run(('-d', 'rules: { key-ordering: enable }', + os.path.join(self.wd, 'c.yaml'))) + self.assertEqual(ctx.returncode, 0) + # en_US + en.yaml should pass with RunContext(self) as ctx: cli.run(('-d', 'locale: en_US.UTF-8\n' @@ -357,12 +364,6 @@ class CommandLineTestCase(unittest.TestCase): os.path.join(self.wd, 'c.yaml'))) self.assertEqual(ctx.returncode, 1) - # C + c.yaml should pass - with RunContext(self) as ctx: - cli.run(('-d', 'rules: { key-ordering: enable }', - os.path.join(self.wd, 'c.yaml'))) - self.assertEqual(ctx.returncode, 0) - def test_run_version(self): with RunContext(self) as ctx: cli.run(('--version', )) @@ -421,6 +422,15 @@ class CommandLineTestCase(unittest.TestCase): def test_run_non_ascii_file(self): path = os.path.join(self.wd, 'non-ascii', 'éçäγλνπ¥', 'utf-8') + # Make sure the default localization conditions on this "system" + # support UTF-8 encoding. + loc = locale.getlocale() + try: + locale.setlocale(locale.LC_ALL, 'C.UTF-8') + except locale.Error: + locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') + self.addCleanup(locale.setlocale, locale.LC_ALL, loc) + with RunContext(self) as ctx: cli.run(('-f', 'parsable', path)) self.assertEqual((ctx.returncode, ctx.stdout, ctx.stderr), (0, '', '')) diff --git a/yamllint/cli.py b/yamllint/cli.py index 68f8a41..311d66c 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -176,7 +176,8 @@ def run(argv=None): print(e, file=sys.stderr) sys.exit(-1) - locale.setlocale(locale.LC_ALL, conf.locale) + if conf.locale is not None: + locale.setlocale(locale.LC_ALL, conf.locale) max_level = 0 diff --git a/yamllint/config.py b/yamllint/config.py index 9092a65..a42e744 100644 --- a/yamllint/config.py +++ b/yamllint/config.py @@ -35,7 +35,7 @@ class YamlLintConfig(object): self.yaml_files = pathspec.PathSpec.from_lines( 'gitwildmatch', ['*.yaml', '*.yml', '.yamllint']) - self.locale = 'C.UTF-8' + self.locale = None if file is not None: with open(file) as f: