Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9caa0d1

Browse files
committed
guess_all_extensions(): Return the empty list instead of None when
there are no matching types. Updated the docs and docstrings. Added some unit tests.
1 parent e07b835 commit 9caa0d1

3 files changed

Lines changed: 40 additions & 37 deletions

File tree

Doc/lib/libmimetypes.tex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ \section{\module{mimetypes} ---
5353
The return value is a list of strings giving all possible filename extensions,
5454
including the leading dot (\character{.}). The extensions are not guaranteed
5555
to have been associated with any particular data stream, but would be mapped
56-
to the MIME type \var{type} by \function{guess_type()}. If no extension can
57-
be guessed for \var{type}, \code{None} is returned.
56+
to the MIME type \var{type} by \function{guess_type()}.
5857

5958
Optional \var{strict} has the same meaning as with the
6059
\function{guess_type()} function.

Lib/mimetypes.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,8 @@ def guess_all_extensions(self, type, strict=True):
148148
149149
Return value is a list of strings giving the possible filename
150150
extensions, including the leading dot ('.'). The extension is not
151-
guaranteed to have been associated with any particular data
152-
stream, but would be mapped to the MIME type `type' by
153-
guess_type(). If no extension can be guessed for `type', None
154-
is returned.
151+
guaranteed to have been associated with any particular data stream,
152+
but would be mapped to the MIME type `type' by guess_type().
155153
156154
Optional `strict' argument when false adds a bunch of commonly found,
157155
but non-standard types.
@@ -162,8 +160,7 @@ def guess_all_extensions(self, type, strict=True):
162160
for ext in self.types_map_inv[False].get(type, []):
163161
if ext not in extensions:
164162
extensions.append(ext)
165-
if len(extensions):
166-
return extensions
163+
return extensions
167164

168165
def guess_extension(self, type, strict=True):
169166
"""Guess the extension for a file based on its MIME type.
@@ -179,9 +176,9 @@ def guess_extension(self, type, strict=True):
179176
but non-standard types.
180177
"""
181178
extensions = self.guess_all_extensions(type, strict)
182-
if extensions is not None:
183-
extensions = extensions[0]
184-
return extensions
179+
if not extensions:
180+
return None
181+
return extensions[0]
185182

186183
def read(self, filename, strict=True):
187184
"""

Lib/test/test_mimetypes.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,49 @@ def setUp(self):
1313
self.db = mimetypes.MimeTypes()
1414

1515
def test_default_data(self):
16-
self.assertEqual(self.db.guess_type("foo.html"),
17-
("text/html", None))
18-
self.assertEqual(self.db.guess_type("foo.tgz"),
19-
("application/x-tar", "gzip"))
20-
self.assertEqual(self.db.guess_type("foo.tar.gz"),
21-
("application/x-tar", "gzip"))
22-
self.assertEqual(self.db.guess_type("foo.tar.Z"),
23-
("application/x-tar", "compress"))
16+
eq = self.assertEqual
17+
eq(self.db.guess_type("foo.html"), ("text/html", None))
18+
eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
19+
eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
20+
eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
2421

2522
def test_data_urls(self):
26-
self.assertEqual(self.db.guess_type("data:,thisIsTextPlain"),
27-
("text/plain", None))
28-
self.assertEqual(self.db.guess_type("data:;base64,thisIsTextPlain"),
29-
("text/plain", None))
30-
self.assertEqual(self.db.guess_type("data:text/x-foo,thisIsTextXFoo"),
31-
("text/x-foo", None))
23+
eq = self.assertEqual
24+
guess_type = self.db.guess_type
25+
eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
26+
eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
27+
eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
3228

3329
def test_file_parsing(self):
30+
eq = self.assertEqual
3431
sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
3532
self.db.readfp(sio)
36-
self.assertEqual(self.db.guess_type("foo.pyunit"),
37-
("x-application/x-unittest", None))
38-
self.assertEqual(self.db.guess_extension("x-application/x-unittest"),
39-
".pyunit")
33+
eq(self.db.guess_type("foo.pyunit"),
34+
("x-application/x-unittest", None))
35+
eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
4036

4137
def test_non_standard_types(self):
38+
eq = self.assertEqual
4239
# First try strict
43-
self.assertEqual(self.db.guess_type('foo.xul', strict=1),
44-
(None, None))
45-
self.assertEqual(self.db.guess_extension('image/jpg', strict=1),
46-
None)
40+
eq(self.db.guess_type('foo.xul', strict=True), (None, None))
41+
eq(self.db.guess_extension('image/jpg', strict=True), None)
4742
# And then non-strict
48-
self.assertEqual(self.db.guess_type('foo.xul', strict=0),
49-
('text/xul', None))
50-
self.assertEqual(self.db.guess_extension('image/jpg', strict=0),
51-
'.jpg')
43+
eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
44+
eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
45+
46+
def test_guess_all_types(self):
47+
eq = self.assertEqual
48+
# First try strict
49+
all = self.db.guess_all_extensions('text/plain', strict=True)
50+
all.sort()
51+
eq(all, ['.bat', '.c', '.h', '.ksh', '.pl', '.txt'])
52+
# And now non-strict
53+
all = self.db.guess_all_extensions('image/jpg', strict=False)
54+
all.sort()
55+
eq(all, ['.jpg'])
56+
# And now for no hits
57+
all = self.db.guess_all_extensions('image/jpg', strict=True)
58+
eq(all, [])
5259

5360

5461
def test_main():

0 commit comments

Comments
 (0)