Skip to content

Commit 7e18dee

Browse files
mayankasthanapitrou
authored andcommitted
bpo-34926: Make mimetypes.guess_type accept os.PathLike objects (GH-9777)
:meth:`mimetypes.MimeTypes.guess_type` now accepts :term:`path-like object` in addition to url strings.
1 parent 3058b7d commit 7e18dee

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

Doc/library/mimetypes.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ the information :func:`init` sets up.
3030

3131
.. index:: pair: MIME; headers
3232

33-
Guess the type of a file based on its filename or URL, given by *url*. The
34-
return value is a tuple ``(type, encoding)`` where *type* is ``None`` if the
33+
Guess the type of a file based on its filename, path or URL, given by *url*.
34+
URL can be a string or a :term:`path-like object`.
35+
36+
The return value is a tuple ``(type, encoding)`` where *type* is ``None`` if the
3537
type can't be guessed (missing or unknown suffix) or a string of the form
3638
``'type/subtype'``, usable for a MIME :mailheader:`content-type` header.
3739

@@ -49,6 +51,9 @@ the information :func:`init` sets up.
4951
*strict* is ``False``, some additional non-standard but commonly used MIME types
5052
are also recognized.
5153

54+
.. versionchanged:: 3.8
55+
Added support for url being a :term:`path-like object`.
56+
5257

5358
.. function:: guess_all_extensions(type, strict=True)
5459

Lib/mimetypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def add_type(self, type, ext, strict=True):
9595
exts.append(ext)
9696

9797
def guess_type(self, url, strict=True):
98-
"""Guess the type of a file based on its URL.
98+
"""Guess the type of a file which is either a URL or a path-like object.
9999
100100
Return value is a tuple (type, encoding) where type is None if
101101
the type can't be guessed (no or unknown suffix) or a string
@@ -113,6 +113,7 @@ def guess_type(self, url, strict=True):
113113
Optional `strict' argument when False adds a bunch of commonly found,
114114
but non-standard types.
115115
"""
116+
url = os.fspath(url)
116117
scheme, url = urllib.parse._splittype(url)
117118
if scheme == 'data':
118119
# syntax of data URLs:

Lib/test/test_mimetypes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import io
22
import locale
33
import mimetypes
4+
import pathlib
45
import sys
56
import unittest
67

@@ -77,6 +78,29 @@ def test_encoding(self):
7778
strict=True)
7879
self.assertEqual(exts, ['.g3', '.g\xb3'])
7980

81+
def test_path_like_ob(self):
82+
filename = "LICENSE.txt"
83+
filepath = pathlib.Path(filename)
84+
filepath_with_abs_dir = pathlib.Path('/dir/'+filename)
85+
filepath_relative = pathlib.Path('../dir/'+filename)
86+
path_dir = pathlib.Path('./')
87+
88+
expected = self.db.guess_type(filename)
89+
90+
self.assertEqual(self.db.guess_type(filepath), expected)
91+
self.assertEqual(self.db.guess_type(
92+
filepath_with_abs_dir), expected)
93+
self.assertEqual(self.db.guess_type(filepath_relative), expected)
94+
self.assertEqual(self.db.guess_type(path_dir), (None, None))
95+
96+
def test_keywords_args_api(self):
97+
self.assertEqual(self.db.guess_type(
98+
url="foo.html", strict=True), ("text/html", None))
99+
self.assertEqual(self.db.guess_all_extensions(
100+
type='image/jpg', strict=True), [])
101+
self.assertEqual(self.db.guess_extension(
102+
type='image/jpg', strict=False), '.jpg')
103+
80104

81105
@unittest.skipUnless(sys.platform.startswith("win"), "Windows only")
82106
class Win32MimeTypesTestCase(unittest.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:meth:`mimetypes.MimeTypes.guess_type` now accepts :term:`path-like object` in addition to url strings.
2+
Patch by Mayank Asthana.

0 commit comments

Comments
 (0)