summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSrinivas Reddy Thatiparthy (శ్రీనివాస్ రెడ్డి తాటిపర్తి) <thatiparthysreenivas@gmail.com>2020-06-29 08:36:48 (GMT)
committerGitHub <noreply@github.com>2020-06-29 08:36:48 (GMT)
commit7f569c9bc0079906012b3034d30fe8abc742e7fc (patch)
tree092de759fa960b01e0d153268a835ec0e25dbfcc /Lib
parente4f1fe6edb216e04da03ae80b462ca273f00255b (diff)
downloadcpython-7f569c9bc0079906012b3034d30fe8abc742e7fc.zip
cpython-7f569c9bc0079906012b3034d30fe8abc742e7fc.tar.gz
cpython-7f569c9bc0079906012b3034d30fe8abc742e7fc.tar.bz2
bpo-41048: mimetypes should read the rule file using UTF-8, not the locale encoding (GH-20998)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/mimetypes.py2
-rw-r--r--Lib/test/test_mimetypes.py12
2 files changed, 13 insertions, 1 deletions
diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py
index 61bfff1..f3343c8 100644
--- a/Lib/mimetypes.py
+++ b/Lib/mimetypes.py
@@ -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:
diff --git a/Lib/test/test_mimetypes.py b/Lib/test/test_mimetypes.py
index 9cac6ce..683d393 100644
--- a/Lib/test/test_mimetypes.py
+++ b/Lib/test/test_mimetypes.py
@@ -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