Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,31 @@ def test_file_parsing(self):
("x-application/x-unittest", None))
eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")

def test_read_mime_types(self):
eq = self.assertEqual

# Unreadable file returns None
self.assertIsNone(mimetypes.read_mime_types("non-existent"))

with support.temp_dir() as directory:
data = "x-application/x-unittest pyunit\n"
file = pathlib.Path(directory, "sample.mimetype")
file.write_text(data)
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
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,7 @@ Mikhail Terekhov
Victor Terrón
Pablo Galindo
Richard M. Tew
Srinivas Reddy Thatiparthy
Tobias Thelen
Christian Theune
Févry Thibault
Expand Down
Original file line number Diff line number Diff line change
@@ -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.