Skip to content
Open
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
4 changes: 2 additions & 2 deletions Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def read(self, filename, strict=True):
list of standard types, else to the list of non-standard
types.
"""
with open(filename, encoding='utf-8') as fp:
with open(filename, encoding='utf-8', errors='replace') as fp:
self.readfp(fp, strict)

def readfp(self, fp, strict=True):
Expand Down Expand Up @@ -428,7 +428,7 @@ def init(files=None):

def read_mime_types(file):
try:
f = open(file, encoding='utf-8')
f = open(file, encoding='utf-8', errors='replace')
except OSError:
return None
with f:
Expand Down
26 changes: 25 additions & 1 deletion Lib/test/test_mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,33 @@ def test_read_mime_types(self):
with unittest.mock.patch.object(mimetypes, 'open',
return_value=fp) as mock_open:
mime_dict = mimetypes.read_mime_types(filename)
mock_open.assert_called_with(filename, encoding='utf-8')
mock_open.assert_called_with(filename, encoding='utf-8',
errors='replace')
eq(mime_dict[".Français"], "application/no-mans-land")

def test_read_mime_types_invalid_utf8_comment(self):
with os_helper.temp_dir() as directory:
data = (b"# non-UTF-8 comment: \x83\n"
b"x-application/x-unittest pyunit\n")
file = os.path.join(directory, "sample.mimetype")
with open(file, "wb") as f:
f.write(data)

mime_dict = mimetypes.read_mime_types(file)
self.assertEqual(
mime_dict[".pyunit"], "x-application/x-unittest")

db = mimetypes.MimeTypes()
db.read(file)
self.assertEqual(
db.guess_file_type("sample.pyunit")[0],
"x-application/x-unittest")

mimetypes.init(files=[file])
self.assertEqual(
mimetypes.guess_file_type("sample.pyunit")[0],
"x-application/x-unittest")

def test_init_reinitializes(self):
# Issue 4936: make sure an init starts clean
# First, put some poison into the types table
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :mod:`mimetypes` initialization from MIME map files containing invalid
UTF-8 bytes in comments.
Loading