]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41048: mimetypes should read the rule file using UTF-8, not the locale encoding...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 29 Jun 2020 12:07:31 +0000 (05:07 -0700)
committerGitHub <noreply@github.com>
Mon, 29 Jun 2020 12:07:31 +0000 (05:07 -0700)
(cherry picked from commit 7f569c9bc0079906012b3034d30fe8abc742e7fc)

Co-authored-by: Srinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) <thatiparthysreenivas@gmail.com>
Lib/mimetypes.py
Lib/test/test_mimetypes.py
Misc/ACKS
Misc/NEWS.d/next/Library/2020-06-20-10-16-57.bpo-41048.hEXB-B.rst [new file with mode: 0644]

index e972ca2e291a0bda29cc21da7df3751424ec39f8..92c2a470e9789df5ab41be97a14a562f6aff2398 100644 (file)
@@ -372,7 +372,7 @@ def init(files=None):
 
 def read_mime_types(file):
     try:
-        f = open(file)
+        f = open(file, encoding='utf-8')
     except OSError:
         return None
     with f:
index 9cac6ce0225e1cd900b908ad05a3fa9f2d431b15..683d393fdb4913978a9e1ff92ac9cb5937d6baab 100644 (file)
@@ -67,6 +67,18 @@ class MimeTypesTestCase(unittest.TestCase):
             mime_dict = mimetypes.read_mime_types(file)
             eq(mime_dict[".pyunit"], "x-application/x-unittest")
 
+        # bpo-41048: read_mime_types should read the rule file with 'utf-8' encoding.
+        # Not with locale encoding. _bootlocale has been imported because io.open(...)
+        # uses it.
+        with support.temp_dir() as directory:
+            data = "application/no-mans-land  Fran\u00E7ais"
+            file = pathlib.Path(directory, "sample.mimetype")
+            file.write_text(data, encoding='utf-8')
+            import _bootlocale
+            with support.swap_attr(_bootlocale, 'getpreferredencoding', lambda do_setlocale=True: 'ASCII'):
+                mime_dict = mimetypes.read_mime_types(file)
+            eq(mime_dict[".Français"], "application/no-mans-land")
+
     def test_non_standard_types(self):
         eq = self.assertEqual
         # First try strict
index 0fc1954a2223754d2898fa042ad6e471695fadd9..2a180eb67cc5c337e86ad7b871b2c4019e3bc83e 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1705,6 +1705,7 @@ Mikhail Terekhov
 Victor Terrón
 Pablo Galindo
 Richard M. Tew
+Srinivas Reddy Thatiparthy
 Tobias Thelen
 Christian Theune
 Févry Thibault
diff --git a/Misc/NEWS.d/next/Library/2020-06-20-10-16-57.bpo-41048.hEXB-B.rst b/Misc/NEWS.d/next/Library/2020-06-20-10-16-57.bpo-41048.hEXB-B.rst
new file mode 100644 (file)
index 0000000..2595900
--- /dev/null
@@ -0,0 +1,2 @@
+:func:`mimetypes.read_mime_types` function reads the rule file using UTF-8 encoding, not the locale encoding.
+Patch by Srinivas Reddy Thatiparthy.
\ No newline at end of file