|
From: <mi...@us...> - 2022-06-15 15:28:38
|
Revision: 9074
http://sourceforge.net/p/docutils/code/9074
Author: milde
Date: 2022-06-15 15:28:35 +0000 (Wed, 15 Jun 2022)
Log Message:
-----------
Ensure locale_encoding is lower case.
Use `locale.getpreferredencoding(False)` since we dropped support
for older Python versions with side-effects.
Modified Paths:
--------------
trunk/docutils/docutils/io.py
trunk/docutils/docutils/utils/error_reporting.py
trunk/docutils/test/test_io.py
trunk/docutils/test/test_parsers/test_rst/test_directives/test_date.py
Modified: trunk/docutils/docutils/io.py
===================================================================
--- trunk/docutils/docutils/io.py 2022-06-15 15:28:26 UTC (rev 9073)
+++ trunk/docutils/docutils/io.py 2022-06-15 15:28:35 UTC (rev 9074)
@@ -19,16 +19,20 @@
from docutils import TransformSpec
-# Guess the locale's encoding.
+# Guess the locale's preferred encoding.
# If no valid guess can be made, locale_encoding is set to `None`:
+#
+# TODO: check whether this is set correctly with every OS and Python version
+# or whether front-end tools need to call `locale.setlocale()`
+# before importing this module
try:
- locale_encoding = locale.getlocale()[1] or locale.getdefaultlocale()[1]
+ locale_encoding = locale.getpreferredencoding(do_setlocale=False).lower()
except ValueError as error: # OS X may set UTF-8 without language code
# See https://bugs.python.org/issue18378 fixed in 3.8
# and https://sourceforge.net/p/docutils/bugs/298/.
# Drop the special case after requiring Python >= 3.8
if "unknown locale: UTF-8" in error.args:
- locale_encoding = "UTF-8"
+ locale_encoding = "utf-8"
else:
locale_encoding = None
except: # noqa any other problems determining the locale -> use None
Modified: trunk/docutils/docutils/utils/error_reporting.py
===================================================================
--- trunk/docutils/docutils/utils/error_reporting.py 2022-06-15 15:28:26 UTC (rev 9073)
+++ trunk/docutils/docutils/utils/error_reporting.py 2022-06-15 15:28:35 UTC (rev 9074)
@@ -45,41 +45,17 @@
common exceptions.
"""
-import codecs
import sys
import warnings
+from docutils.io import locale_encoding
+
warnings.warn('The `docutils.utils.error_reporting` module is deprecated '
'and will be removed in Docutils 0.21 or later.\n'
'Details with help("docutils.utils.error_reporting").',
DeprecationWarning, stacklevel=2)
-# Guess the locale's encoding.
-# If no valid guess can be made, locale_encoding is set to `None`:
-try:
- import locale # module missing in Jython
-except ImportError:
- locale_encoding = None
-else:
- try:
- locale_encoding = locale.getlocale()[1] or locale.getdefaultlocale()[1]
- # locale.getpreferredencoding([do_setlocale=True|False])
- # has side-effects | might return a wrong guess.
- except ValueError as error: # OS X may set UTF-8 without language code
- # see http://bugs.python.org/issue18378
- # and https://sourceforge.net/p/docutils/bugs/298/
- if "unknown locale: UTF-8" in error.args:
- locale_encoding = "UTF-8"
- else:
- locale_encoding = None
- except: # noqa any other problems determining the locale -> use None
- locale_encoding = None
- try:
- codecs.lookup(locale_encoding or '') # None -> ''
- except LookupError:
- locale_encoding = None
-
if sys.version_info >= (3, 0):
unicode = str # noqa
Modified: trunk/docutils/test/test_io.py
===================================================================
--- trunk/docutils/test/test_io.py 2022-06-15 15:28:26 UTC (rev 9073)
+++ trunk/docutils/test/test_io.py 2022-06-15 15:28:35 UTC (rev 9074)
@@ -132,7 +132,7 @@
def test_heuristics_no_utf8(self):
# if no encoding is given and decoding with utf-8 fails,
# use either the locale encoding (if specified) or latin-1:
- if io.locale_encoding.lower() not in ('utf-8', 'utf8'):
+ if io.locale_encoding not in ('utf-8', 'utf8'):
# in Py3k, the locale encoding is used without --input-encoding
# skipping the heuristic unless decoding fails.
return
Modified: trunk/docutils/test/test_parsers/test_rst/test_directives/test_date.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_directives/test_date.py 2022-06-15 15:28:26 UTC (rev 9073)
+++ trunk/docutils/test/test_parsers/test_rst/test_directives/test_date.py 2022-06-15 15:28:35 UTC (rev 9074)
@@ -63,7 +63,7 @@
# some locales return non-ASCII characters for names of days or months
# ensure the directive handles them correctly
-if locale_encoding.lower() in ('utf8', 'utf-8', 'latin-1', 'iso8859-1'):
+if locale_encoding in ('utf-8', 'utf8', 'latin-1', 'iso-8859-1'):
totest['decode date'] = [
["""\
.. |date| date:: täglich
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|