← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:mime-py38 into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:mime-py38 into launchpad:master.

Commit message:
Adjust lp.services.mime for Python >= 3.8

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/429238

Python 3.8 slightly changed how the `mimetypes` module is initialized (https://github.com/python/cpython/pull/14375), and this interacted badly with the reinitialization code in `zope.contenttype` in such a way as to make the extensions in `lp.services.mime` be ineffective.  Work around this in a way that works with both old and new versions of Python by monkey-patching `mimetypes.init`.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:mime-py38 into launchpad:master.
diff --git a/lib/lp/services/mime.py b/lib/lp/services/mime.py
index dc55ba2..b264efc 100644
--- a/lib/lp/services/mime.py
+++ b/lib/lp/services/mime.py
@@ -9,15 +9,11 @@ __all__ = [
 
 import mimetypes
 
+_original_mimetypes_init = mimetypes.init
 
-def customizeMimetypes():
-    """Initialize and extend the standard mimetypes library for our needs.
 
-    This method is to be called before any requests are processed to ensure
-    any call site that imports the standard mimetypes module will take
-    advantage of these customizations.
-    """
-    mimetypes.init()
+def _patched_mimetypes_init(*args, **kwargs):
+    _original_mimetypes_init(*args, **kwargs)
 
     # Add support for .bzip2 as well as .bz2.  Up to Python 3.2 (at least),
     # only .bz2 is present.
@@ -33,3 +29,14 @@ def customizeMimetypes():
 
     # Add support for Launchpad's OWL decription of its RDF metadata.
     mimetypes.add_type("application/rdf+xml", ".owl")
+
+
+def customizeMimetypes():
+    """Initialize and extend the standard mimetypes library for our needs.
+
+    This method is to be called before any requests are processed to ensure
+    any call site that imports the standard mimetypes module will take
+    advantage of these customizations.
+    """
+    mimetypes.init = _patched_mimetypes_init
+    mimetypes.init()
diff --git a/lib/lp/services/tests/test_mime.py b/lib/lp/services/tests/test_mime.py
index 43eabcf..0d729c9 100644
--- a/lib/lp/services/tests/test_mime.py
+++ b/lib/lp/services/tests/test_mime.py
@@ -9,7 +9,7 @@ from lp.testing import TestCase
 
 
 class TestBzip(TestCase):
-    """Tests for iter_split."""
+    """Tests for lp.services.mime.customizeMimetypes."""
 
     def test_bzip2(self):
         # Test for '.tar.bzip2' support.