]> granicus.if.org Git - python/commitdiff
guess_all_extensions(): Return the empty list instead of None when
authorBarry Warsaw <barry@python.org>
Mon, 9 Jun 2003 22:27:41 +0000 (22:27 +0000)
committerBarry Warsaw <barry@python.org>
Mon, 9 Jun 2003 22:27:41 +0000 (22:27 +0000)
there are no matching types.  Updated the docs and docstrings.  Added
some unit tests.

Doc/lib/libmimetypes.tex
Lib/mimetypes.py
Lib/test/test_mimetypes.py

index 64296f0301232073a5e3708729e3c8884aea037d..8e07768f75cb03d622da0d7542f3e06204eab40c 100644 (file)
@@ -53,8 +53,7 @@ Guess the extensions for a file based on its MIME type, given by
 The return value is a list of strings giving all possible filename extensions,
 including the leading dot (\character{.}).  The extensions are not guaranteed
 to have been associated with any particular data stream, but would be mapped
-to the MIME type \var{type} by \function{guess_type()}.  If no extension can
-be guessed for \var{type}, \code{None} is returned.
+to the MIME type \var{type} by \function{guess_type()}.
 
 Optional \var{strict} has the same meaning as with the
 \function{guess_type()} function.
index a909366fd481365efce693ced2f3aebc54ace39b..5784e230942519acd901c79ebeee95067573b449 100644 (file)
@@ -148,10 +148,8 @@ class MimeTypes:
 
         Return value is a list of strings giving the possible filename
         extensions, including the leading dot ('.').  The extension is not
-        guaranteed to have been associated with any particular data
-        stream, but would be mapped to the MIME type `type' by
-        guess_type().  If no extension can be guessed for `type', None
-        is returned.
+        guaranteed to have been associated with any particular data stream,
+        but would be mapped to the MIME type `type' by guess_type().
 
         Optional `strict' argument when false adds a bunch of commonly found,
         but non-standard types.
@@ -162,8 +160,7 @@ class MimeTypes:
             for ext in self.types_map_inv[False].get(type, []):
                 if ext not in extensions:
                     extensions.append(ext)
-        if len(extensions):
-            return extensions
+        return extensions
 
     def guess_extension(self, type, strict=True):
         """Guess the extension for a file based on its MIME type.
@@ -179,9 +176,9 @@ class MimeTypes:
         but non-standard types.
         """
         extensions = self.guess_all_extensions(type, strict)
-        if extensions is not None:
-            extensions = extensions[0]
-        return extensions
+        if not extensions:
+            return None
+        return extensions[0]
 
     def read(self, filename, strict=True):
         """
index 69539663ca6fa3e1717119d3fdf596cf9d0d61d0..81829c12c6190a3d690d69e0dcfe6e23522fa91d 100644 (file)
@@ -13,42 +13,49 @@ class MimeTypesTestCase(unittest.TestCase):
         self.db = mimetypes.MimeTypes()
 
     def test_default_data(self):
-        self.assertEqual(self.db.guess_type("foo.html"),
-                         ("text/html", None))
-        self.assertEqual(self.db.guess_type("foo.tgz"),
-                         ("application/x-tar", "gzip"))
-        self.assertEqual(self.db.guess_type("foo.tar.gz"),
-                         ("application/x-tar", "gzip"))
-        self.assertEqual(self.db.guess_type("foo.tar.Z"),
-                         ("application/x-tar", "compress"))
+        eq = self.assertEqual
+        eq(self.db.guess_type("foo.html"), ("text/html", None))
+        eq(self.db.guess_type("foo.tgz"), ("application/x-tar", "gzip"))
+        eq(self.db.guess_type("foo.tar.gz"), ("application/x-tar", "gzip"))
+        eq(self.db.guess_type("foo.tar.Z"), ("application/x-tar", "compress"))
 
     def test_data_urls(self):
-        self.assertEqual(self.db.guess_type("data:,thisIsTextPlain"),
-                         ("text/plain", None))
-        self.assertEqual(self.db.guess_type("data:;base64,thisIsTextPlain"),
-                         ("text/plain", None))
-        self.assertEqual(self.db.guess_type("data:text/x-foo,thisIsTextXFoo"),
-                         ("text/x-foo", None))
+        eq = self.assertEqual
+        guess_type = self.db.guess_type
+        eq(guess_type("data:,thisIsTextPlain"), ("text/plain", None))
+        eq(guess_type("data:;base64,thisIsTextPlain"), ("text/plain", None))
+        eq(guess_type("data:text/x-foo,thisIsTextXFoo"), ("text/x-foo", None))
 
     def test_file_parsing(self):
+        eq = self.assertEqual
         sio = StringIO.StringIO("x-application/x-unittest pyunit\n")
         self.db.readfp(sio)
-        self.assertEqual(self.db.guess_type("foo.pyunit"),
-                         ("x-application/x-unittest", None))
-        self.assertEqual(self.db.guess_extension("x-application/x-unittest"),
-                         ".pyunit")
+        eq(self.db.guess_type("foo.pyunit"),
+           ("x-application/x-unittest", None))
+        eq(self.db.guess_extension("x-application/x-unittest"), ".pyunit")
 
     def test_non_standard_types(self):
+        eq = self.assertEqual
         # First try strict
-        self.assertEqual(self.db.guess_type('foo.xul', strict=1),
-                         (None, None))
-        self.assertEqual(self.db.guess_extension('image/jpg', strict=1),
-                         None)
+        eq(self.db.guess_type('foo.xul', strict=True), (None, None))
+        eq(self.db.guess_extension('image/jpg', strict=True), None)
         # And then non-strict
-        self.assertEqual(self.db.guess_type('foo.xul', strict=0),
-                         ('text/xul', None))
-        self.assertEqual(self.db.guess_extension('image/jpg', strict=0),
-                         '.jpg')
+        eq(self.db.guess_type('foo.xul', strict=False), ('text/xul', None))
+        eq(self.db.guess_extension('image/jpg', strict=False), '.jpg')
+
+    def test_guess_all_types(self):
+        eq = self.assertEqual
+        # First try strict
+        all = self.db.guess_all_extensions('text/plain', strict=True)
+        all.sort()
+        eq(all, ['.bat', '.c', '.h', '.ksh', '.pl', '.txt'])
+        # And now non-strict
+        all = self.db.guess_all_extensions('image/jpg', strict=False)
+        all.sort()
+        eq(all, ['.jpg'])
+        # And now for no hits
+        all = self.db.guess_all_extensions('image/jpg', strict=True)
+        eq(all, [])
 
 
 def test_main():