[Python-checkins] bpo-35292: Avoid calling mimetypes.init when http.server is imported (GH-17822)

Steve Dower webhook-mailer at python.org
Wed Jan 8 13:28:22 EST 2020


https://github.com/python/cpython/commit/5907e61a8d4da6d0f11bf1062d6d17484560a15e
commit: 5907e61a8d4da6d0f11bf1062d6d17484560a15e
branch: master
author: An Long <aisk at users.noreply.github.com>
committer: Steve Dower <steve.dower at python.org>
date: 2020-01-08T10:28:14-08:00
summary:

bpo-35292: Avoid calling mimetypes.init when http.server is imported (GH-17822)

files:
A Misc/NEWS.d/next/Library/2020-01-08-14-39-19.bpo-35292.ihRT1z.rst
M Doc/library/http.server.rst
M Lib/http/server.py

diff --git a/Doc/library/http.server.rst b/Doc/library/http.server.rst
index 5173decb2b2ef..478a5b31475cf 100644
--- a/Doc/library/http.server.rst
+++ b/Doc/library/http.server.rst
@@ -335,11 +335,14 @@ provides three different variants:
 
    .. attribute:: extensions_map
 
-      A dictionary mapping suffixes into MIME types. The default is
-      signified by an empty string, and is considered to be
-      ``application/octet-stream``. The mapping is used case-insensitively,
+      A dictionary mapping suffixes into MIME types, contains custom overrides
+      for the default system mappings. The mapping is used case-insensitively,
       and so should contain only lower-cased keys.
 
+      .. versionchanged:: 3.9
+         This dictionary is no longer filled with the default system mappings,
+         but only contains overrides.
+
    .. attribute:: directory
 
       If not specified, the directory to serve is the current working directory.
diff --git a/Lib/http/server.py b/Lib/http/server.py
index c6e5ed6ea0e00..2d74b95586cff 100644
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -639,6 +639,12 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
     """
 
     server_version = "SimpleHTTP/" + __version__
+    extensions_map = _encodings_map_default = {
+        '.gz': 'application/gzip',
+        '.Z': 'application/octet-stream',
+        '.bz2': 'application/x-bzip2',
+        '.xz': 'application/x-xz',
+    }
 
     def __init__(self, *args, directory=None, **kwargs):
         if directory is None:
@@ -866,25 +872,16 @@ def guess_type(self, path):
         slow) to look inside the data to make a better guess.
 
         """
-
         base, ext = posixpath.splitext(path)
         if ext in self.extensions_map:
             return self.extensions_map[ext]
         ext = ext.lower()
         if ext in self.extensions_map:
             return self.extensions_map[ext]
-        else:
-            return self.extensions_map['']
-
-    if not mimetypes.inited:
-        mimetypes.init() # try to read system mime.types
-    extensions_map = mimetypes.types_map.copy()
-    extensions_map.update({
-        '': 'application/octet-stream', # Default
-        '.py': 'text/plain',
-        '.c': 'text/plain',
-        '.h': 'text/plain',
-        })
+        guess, _ = mimetypes.guess_type(path)
+        if guess:
+            return guess
+        return 'application/octet-stream'
 
 
 # Utilities for CGIHTTPRequestHandler
diff --git a/Misc/NEWS.d/next/Library/2020-01-08-14-39-19.bpo-35292.ihRT1z.rst b/Misc/NEWS.d/next/Library/2020-01-08-14-39-19.bpo-35292.ihRT1z.rst
new file mode 100644
index 0000000000000..ae52f970d0b71
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2020-01-08-14-39-19.bpo-35292.ihRT1z.rst
@@ -0,0 +1 @@
+Proxy the `SimpleHTTPRequestHandler.guess_type` to `mimetypes.guess_type` so the `mimetypes.init` is called lazily to avoid unnecessary costs when :mod:`http.server` module is imported.
\ No newline at end of file



More information about the Python-checkins mailing list