Skip to content
Permalink
Browse files
bpo-41048: mimetypes should read the rule file using UTF-8, not the l…
…ocale encoding (GH-20998)
  • Loading branch information
srinivasreddy committed Jun 29, 2020
1 parent e4f1fe6 commit 7f569c9bc0079906012b3034d30fe8abc742e7fc
Showing with 16 additions and 1 deletion.
  1. +1 −1 Lib/mimetypes.py
  2. +12 −0 Lib/test/test_mimetypes.py
  3. +1 −0 Misc/ACKS
  4. +2 −0 Misc/NEWS.d/next/Library/2020-06-20-10-16-57.bpo-41048.hEXB-B.rst
@@ -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:
@@ -67,6 +67,18 @@ def test_read_mime_types(self):
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
@@ -1706,6 +1706,7 @@ Mikhail Terekhov
Victor Terrón
Pablo Galindo
Richard M. Tew
Srinivas Reddy Thatiparthy
Tobias Thelen
Christian Theune
Févry Thibault
@@ -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.

0 comments on commit 7f569c9

Please sign in to comment.