From 4069061cb4d0e2edc501ad7c5d3223b5ca2ebc92 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 18 Nov 2024 21:23:25 +0200 Subject: [PATCH 1/4] Improve import time of mimetypes: defer urllib.parse --- Lib/mimetypes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index 210d2264757d08..108fe151cf4059 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -26,7 +26,6 @@ import os import sys import posixpath -import urllib.parse try: from _winapi import _mimetypes_read_windows_registry @@ -119,6 +118,9 @@ def guess_type(self, url, strict=True): Optional 'strict' argument when False adds a bunch of commonly found, but non-standard types. """ + # Lazy import to improve module import time + import urllib.parse + # TODO: Deprecate accepting file paths (in particular path-like objects). url = os.fspath(url) p = urllib.parse.urlparse(url) From 12fc4fe200665e715244ef9481ba630e70195437 Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 18 Nov 2024 21:24:11 +0200 Subject: [PATCH 2/4] Update link redirect --- Lib/mimetypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index 108fe151cf4059..dd4bde94029e32 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -447,7 +447,7 @@ def _default_mime_types(): } # 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 # If you add to these, please keep them sorted by mime type. From e6e4752f36b80e3747780cb5f0092f5ad09fce2f Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 18 Nov 2024 22:03:20 +0200 Subject: [PATCH 3/4] Improve import time of mimetypes: defer os, sys, posixpath --- Lib/mimetypes.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index dd4bde94029e32..0de18e8bfadb0c 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -23,10 +23,6 @@ read_mime_types(file) -- parse one file, return a dictionary or None """ -import os -import sys -import posixpath - try: from _winapi import _mimetypes_read_windows_registry except ImportError: @@ -119,6 +115,7 @@ def guess_type(self, url, strict=True): 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). @@ -148,6 +145,10 @@ def guess_type(self, url, strict=True): if '=' in type or '/' not in type: type = 'text/plain' 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) def guess_file_type(self, path, *, strict=True): @@ -155,6 +156,9 @@ def guess_file_type(self, path, *, strict=True): 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.path.splitdrive(path)[1] return self._guess_file_type(path, strict, os.path.splitext) @@ -401,6 +405,9 @@ def init(files=None): else: db = _db + # Lazy import to improve module import time + import os + for file in files: if os.path.isfile(file): db.read(file) @@ -639,6 +646,7 @@ def _default_mime_types(): def _main(): import getopt + import sys USAGE = """\ Usage: mimetypes.py [options] type From 0f2ccf07df8770fa203d6bd13efd28f37d811bec Mon Sep 17 00:00:00 2001 From: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> Date: Mon, 18 Nov 2024 22:03:33 +0200 Subject: [PATCH 4/4] Add blurb --- .../next/Library/2024-11-18-22-02-47.gh-issue-118761.GQKD_J.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2024-11-18-22-02-47.gh-issue-118761.GQKD_J.rst diff --git a/Misc/NEWS.d/next/Library/2024-11-18-22-02-47.gh-issue-118761.GQKD_J.rst b/Misc/NEWS.d/next/Library/2024-11-18-22-02-47.gh-issue-118761.GQKD_J.rst new file mode 100644 index 00000000000000..ebb9fe8016de21 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-11-18-22-02-47.gh-issue-118761.GQKD_J.rst @@ -0,0 +1,2 @@ +Improve import time of :mod:`mimetypes` by around 11-16 times. Patch by Hugo +van Kemenade.