Skip to content

Commit 175764e

Browse files
committed
gh-150821: Skip URL parsing in mimetypes.guess_type() for file paths
guess_type() parsed every argument as a URL before checking the extension, even for plain file paths that have no scheme. Detect the no-scheme case and go straight to extension lookup, avoiding urlparse() and its lazy import. Real URLs keep the full parsing path; results are unchanged.
1 parent 29805f0 commit 175764e

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

Lib/mimetypes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,14 @@ def guess_type(self, url, strict=True):
123123
"""
124124
# Lazy import to improve module import time
125125
import os
126-
import urllib.parse
127126

128127
# TODO: Deprecate accepting file paths (in particular path-like objects).
129128
url = os.fspath(url)
129+
# A URL scheme requires a ':'; a plain file path (the common case) has
130+
# none, so skip the relatively expensive urlparse() for it.
131+
if isinstance(url, str) and ':' not in url:
132+
return self.guess_file_type(url, strict=strict)
133+
import urllib.parse
130134
p = urllib.parse.urlparse(url)
131135
if p.scheme and len(p.scheme) > 1:
132136
scheme = p.scheme
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up :func:`mimetypes.guess_type` for plain file paths by skipping URL
2+
parsing when the argument has no scheme. Patch by Bernát Gábor.

0 commit comments

Comments
 (0)