mirror of https://github.com/python/cpython
gh-118761: Improve import time of `mimetypes` (#126979)
This commit is contained in:
parent
bab4b0462e
commit
dc7a2b6522
|
@ -23,11 +23,6 @@ init([files]) -- parse a list of files, default knownfiles (on Windows, the
|
||||||
read_mime_types(file) -- parse one file, return a dictionary or None
|
read_mime_types(file) -- parse one file, return a dictionary or None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import posixpath
|
|
||||||
import urllib.parse
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from _winapi import _mimetypes_read_windows_registry
|
from _winapi import _mimetypes_read_windows_registry
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -119,6 +114,10 @@ class MimeTypes:
|
||||||
Optional 'strict' argument when False adds a bunch of commonly found,
|
Optional 'strict' argument when False adds a bunch of commonly found,
|
||||||
but non-standard types.
|
but non-standard types.
|
||||||
"""
|
"""
|
||||||
|
# Lazy import to improve module import time
|
||||||
|
import os
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
# TODO: Deprecate accepting file paths (in particular path-like objects).
|
# TODO: Deprecate accepting file paths (in particular path-like objects).
|
||||||
url = os.fspath(url)
|
url = os.fspath(url)
|
||||||
p = urllib.parse.urlparse(url)
|
p = urllib.parse.urlparse(url)
|
||||||
|
@ -146,6 +145,10 @@ class MimeTypes:
|
||||||
if '=' in type or '/' not in type:
|
if '=' in type or '/' not in type:
|
||||||
type = 'text/plain'
|
type = 'text/plain'
|
||||||
return type, None # never compressed, so encoding is None
|
return type, None # never compressed, so encoding is None
|
||||||
|
|
||||||
|
# Lazy import to improve module import time
|
||||||
|
import posixpath
|
||||||
|
|
||||||
return self._guess_file_type(url, strict, posixpath.splitext)
|
return self._guess_file_type(url, strict, posixpath.splitext)
|
||||||
|
|
||||||
def guess_file_type(self, path, *, strict=True):
|
def guess_file_type(self, path, *, strict=True):
|
||||||
|
@ -153,6 +156,9 @@ class MimeTypes:
|
||||||
|
|
||||||
Similar to guess_type(), but takes file path instead of URL.
|
Similar to guess_type(), but takes file path instead of URL.
|
||||||
"""
|
"""
|
||||||
|
# Lazy import to improve module import time
|
||||||
|
import os
|
||||||
|
|
||||||
path = os.fsdecode(path)
|
path = os.fsdecode(path)
|
||||||
path = os.path.splitdrive(path)[1]
|
path = os.path.splitdrive(path)[1]
|
||||||
return self._guess_file_type(path, strict, os.path.splitext)
|
return self._guess_file_type(path, strict, os.path.splitext)
|
||||||
|
@ -399,6 +405,9 @@ def init(files=None):
|
||||||
else:
|
else:
|
||||||
db = _db
|
db = _db
|
||||||
|
|
||||||
|
# Lazy import to improve module import time
|
||||||
|
import os
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
if os.path.isfile(file):
|
if os.path.isfile(file):
|
||||||
db.read(file)
|
db.read(file)
|
||||||
|
@ -445,7 +454,7 @@ def _default_mime_types():
|
||||||
}
|
}
|
||||||
|
|
||||||
# Before adding new types, make sure they are either registered with IANA,
|
# Before adding new types, make sure they are either registered with IANA,
|
||||||
# at http://www.iana.org/assignments/media-types
|
# at https://www.iana.org/assignments/media-types/media-types.xhtml
|
||||||
# or extensions, i.e. using the x- prefix
|
# or extensions, i.e. using the x- prefix
|
||||||
|
|
||||||
# If you add to these, please keep them sorted by mime type.
|
# If you add to these, please keep them sorted by mime type.
|
||||||
|
@ -646,6 +655,7 @@ _default_mime_types()
|
||||||
|
|
||||||
def _main():
|
def _main():
|
||||||
import getopt
|
import getopt
|
||||||
|
import sys
|
||||||
|
|
||||||
USAGE = """\
|
USAGE = """\
|
||||||
Usage: mimetypes.py [options] type
|
Usage: mimetypes.py [options] type
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Improve import time of :mod:`mimetypes` by around 11-16 times. Patch by Hugo
|
||||||
|
van Kemenade.
|
Loading…
Reference in New Issue