Commit 9fc720e5 authored by David K. Hess's avatar David K. Hess Committed by Steve Dower

bpo-4963: Fix for initialization and non-deterministic behavior issues in mimetypes (GH-3062)

parent 8bd2872a
...@@ -93,6 +93,10 @@ behavior of the module. ...@@ -93,6 +93,10 @@ behavior of the module.
Specifying an empty list for *files* will prevent the system defaults from Specifying an empty list for *files* will prevent the system defaults from
being applied: only the well-known values will be present from a built-in list. being applied: only the well-known values will be present from a built-in list.
If *files* is ``None`` the internal data structure is completely rebuilt to its
initial default value. This is a stable operation and will produce the same results
when called multiple times.
.. versionchanged:: 3.2 .. versionchanged:: 3.2
Previously, Windows registry settings were ignored. Previously, Windows registry settings were ignored.
......
This diff is collapsed.
...@@ -79,6 +79,57 @@ class MimeTypesTestCase(unittest.TestCase): ...@@ -79,6 +79,57 @@ class MimeTypesTestCase(unittest.TestCase):
strict=True) strict=True)
self.assertEqual(exts, ['.g3', '.g\xb3']) self.assertEqual(exts, ['.g3', '.g\xb3'])
def test_init_reinitializes(self):
# Issue 4936: make sure an init starts clean
# First, put some poison into the types table
mimetypes.add_type('foo/bar', '.foobar')
self.assertEqual(mimetypes.guess_extension('foo/bar'), '.foobar')
# Reinitialize
mimetypes.init()
# Poison should be gone.
self.assertEqual(mimetypes.guess_extension('foo/bar'), None)
def test_preferred_extension(self):
def check_extensions():
self.assertEqual(mimetypes.guess_extension('application/octet-stream'), '.bin')
self.assertEqual(mimetypes.guess_extension('application/postscript'), '.ps')
self.assertEqual(mimetypes.guess_extension('application/vnd.apple.mpegurl'), '.m3u')
self.assertEqual(mimetypes.guess_extension('application/vnd.ms-excel'), '.xls')
self.assertEqual(mimetypes.guess_extension('application/vnd.ms-powerpoint'), '.ppt')
self.assertEqual(mimetypes.guess_extension('application/x-texinfo'), '.texi')
self.assertEqual(mimetypes.guess_extension('application/x-troff'), '.roff')
self.assertEqual(mimetypes.guess_extension('application/xml'), '.xsl')
self.assertEqual(mimetypes.guess_extension('audio/mpeg'), '.mp3')
self.assertEqual(mimetypes.guess_extension('image/jpeg'), '.jpg')
self.assertEqual(mimetypes.guess_extension('image/tiff'), '.tiff')
self.assertEqual(mimetypes.guess_extension('message/rfc822'), '.eml')
self.assertEqual(mimetypes.guess_extension('text/html'), '.html')
self.assertEqual(mimetypes.guess_extension('text/plain'), '.txt')
self.assertEqual(mimetypes.guess_extension('video/mpeg'), '.mpeg')
self.assertEqual(mimetypes.guess_extension('video/quicktime'), '.mov')
check_extensions()
mimetypes.init()
check_extensions()
def test_init_stability(self):
mimetypes.init()
suffix_map = mimetypes.suffix_map
encodings_map = mimetypes.encodings_map
types_map = mimetypes.types_map
common_types = mimetypes.common_types
mimetypes.init()
self.assertIsNot(suffix_map, mimetypes.suffix_map)
self.assertIsNot(encodings_map, mimetypes.encodings_map)
self.assertIsNot(types_map, mimetypes.types_map)
self.assertIsNot(common_types, mimetypes.common_types)
self.assertEqual(suffix_map, mimetypes.suffix_map)
self.assertEqual(encodings_map, mimetypes.encodings_map)
self.assertEqual(types_map, mimetypes.types_map)
self.assertEqual(common_types, mimetypes.common_types)
def test_path_like_ob(self): def test_path_like_ob(self):
filename = "LICENSE.txt" filename = "LICENSE.txt"
filepath = pathlib.Path(filename) filepath = pathlib.Path(filename)
......
Fixed non-deterministic behavior related to mimetypes extension mapping and
module reinitialization.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment