SF.net SVN: docutils:[9820 ] trunk/docutils

aa-turner--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9820
          http://sourceforge.net/p/docutils/code/9820
Author:   aa-turner
Date:     2024-08-01 19:29:38 +0000 (Thu, 01 Aug 2024)
Log Message:
-----------
Fix test_fallback_{utf8,no_utf8}

Modified Paths:
--------------
    trunk/docutils/docutils/io.py
    trunk/docutils/test/test_io.py

Modified: trunk/docutils/docutils/io.py
===================================================================
--- trunk/docutils/docutils/io.py	2024-08-01 18:42:09 UTC (rev 9819)
+++ trunk/docutils/docutils/io.py	2024-08-01 19:29:38 UTC (rev 9820)
@@ -160,7 +160,10 @@
                 # data that *IS* UTF-8:
                 encoding_candidates = ['utf-8']
                 # If UTF-8 fails, fall back to the locale's preferred encoding:
-                fallback = locale.getpreferredencoding(do_setlocale=False)
+                if sys.version_info[:2] >= (3, 11):
+                    fallback = locale.getencoding()
+                else:
+                    fallback = locale.getpreferredencoding(do_setlocale=False)
                 if fallback and fallback.lower() != 'utf-8':
                     encoding_candidates.append(fallback)
         for enc in encoding_candidates:

Modified: trunk/docutils/test/test_io.py
===================================================================
--- trunk/docutils/test/test_io.py	2024-08-01 18:42:09 UTC (rev 9819)
+++ trunk/docutils/test/test_io.py	2024-08-01 19:29:38 UTC (rev 9820)
@@ -42,13 +42,6 @@
     preferredencoding = codecs.lookup(
         locale.getpreferredencoding(do_setlocale=False)).name
 
-if preferredencoding in {'cp1252', 'iso8859-1'}:
-    expected_failure_on_non_utf8 = unittest.expectedFailure
-else:
-    def expected_failure_on_non_utf8(test_item):
-        return test_item
-
-
 # Stub: Buffer with 'strict' auto-conversion of input to byte string:
 class BBuf(BytesIO):
     def write(self, data):
@@ -311,13 +304,14 @@
 
     def test_fallback_utf8(self):
         """Try 'utf-8', if encoding is not specified in the source."""
-        source = du_io.FileInput(
-            source_path=os.path.join(DATA_ROOT, 'utf8.txt'))
+        with warnings.catch_warnings():
+            if SUPPRESS_ENCODING_WARNING:
+                warnings.filterwarnings('ignore', category=EncodingWarning)
+            source = du_io.FileInput(
+                source_path=os.path.join(DATA_ROOT, 'utf8.txt'),
+                encoding=None)
         self.assertEqual('Grüße\n', source.read())
 
-    # Failing on non-UTF-8 locales (with UTF-8 mode disabled):
-    # see https://sourceforge.net/p/docutils/bugs/490/#43b7
-    @expected_failure_on_non_utf8
     @unittest.skipIf(preferredencoding in (None, 'ascii', 'utf-8'),
                      'locale encoding not set or UTF-8')
     def test_fallback_no_utf8(self):
@@ -325,8 +319,12 @@
         # use the locale's preferred encoding (if not None).
         # Provisional: the default will become 'utf-8'
         # (without auto-detection and fallback) in Docutils 0.22.
-        source = du_io.FileInput(
-            source_path=os.path.join(DATA_ROOT, 'latin1.txt'))
+        with warnings.catch_warnings():
+            if SUPPRESS_ENCODING_WARNING:
+                warnings.filterwarnings('ignore', category=EncodingWarning)
+            source = du_io.FileInput(
+                source_path=os.path.join(DATA_ROOT, 'latin1.txt'),
+                encoding=None)
         data = source.read()
         successful_encoding = codecs.lookup(source.successful_encoding).name
         self.assertEqual(preferredencoding, successful_encoding)

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



_______________________________________________
Docutils-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/docutils-checkins
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.