Skip to content

Commit 2ac8c38

Browse files
committed
Reword comment, tidy blank line, test colon-path branch
Address review: frame the fast path as 'no colon means it cannot be a URL' (a file path may legitimately contain ':' on POSIX), add the blank line before the lazy import, and cover a ':'-containing argument that is not a URL (single-letter drive, colon in the name) reaching the file path branch.
1 parent 175764e commit 2ac8c38

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

Lib/mimetypes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ def guess_type(self, url, strict=True):
126126

127127
# TODO: Deprecate accepting file paths (in particular path-like objects).
128128
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.
129+
# Without a ':' the argument cannot carry a URL scheme, so it cannot
130+
# be a URL; skip the relatively expensive urlparse() in that case.
131131
if isinstance(url, str) and ':' not in url:
132132
return self.guess_file_type(url, strict=strict)
133+
133134
import urllib.parse
134135
p = urllib.parse.urlparse(url)
135136
if p.scheme and len(p.scheme) > 1:

Lib/test/test_mimetypes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,15 @@ def test_url(self):
375375
result = self.db.guess_type('http://example.com/host.html?q=x.tar')
376376
self.assertSequenceEqual(result, ('text/html', None))
377377

378+
def test_path_with_colon_but_no_url_scheme(self):
379+
# A ':' that does not introduce a real URL scheme -- a single-letter
380+
# Windows drive, or a colon elsewhere in the name -- is treated as a
381+
# file path rather than a URL.
382+
eq = self.assertSequenceEqual
383+
eq(self.db.guess_type("c:fake.html"), ("text/html", None))
384+
eq(self.db.guess_type(r"c:\dir\fake.html"), ("text/html", None))
385+
eq(self.db.guess_type("note 12:30.txt"), ("text/plain", None))
386+
378387
def test_guess_all_types(self):
379388
# First try strict. Use a set here for testing the results because if
380389
# test_urllib2 is run before test_mimetypes, global state is modified

0 commit comments

Comments
 (0)