[Python-checkins] cpython (2.7): Issue #19990: Added tests for the imghdr module.

serhiy.storchaka python-checkins at python.org
Sun Jan 26 22:53:56 CET 2014


http://hg.python.org/cpython/rev/b28909d501c0
changeset:   88733:b28909d501c0
branch:      2.7
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Jan 26 23:48:20 2014 +0200
summary:
  Issue #19990: Added tests for the imghdr module.
Based on patch by Claudiu Popa.

files:
  Lib/test/imghdrdata/python.bmp  |  Bin 
  Lib/test/imghdrdata/python.gif  |  Bin 
  Lib/test/imghdrdata/python.jpg  |  Bin 
  Lib/test/imghdrdata/python.pbm  |    3 +
  Lib/test/imghdrdata/python.pgm  |  Bin 
  Lib/test/imghdrdata/python.png  |  Bin 
  Lib/test/imghdrdata/python.ppm  |  Bin 
  Lib/test/imghdrdata/python.ras  |  Bin 
  Lib/test/imghdrdata/python.sgi  |  Bin 
  Lib/test/imghdrdata/python.tiff |  Bin 
  Lib/test/imghdrdata/python.xbm  |    6 +
  Lib/test/test_imghdr.py         |  120 ++++++++++++++++++++
  Lib/test/test_sundry.py         |    1 -
  Misc/NEWS                       |    3 +
  14 files changed, 132 insertions(+), 1 deletions(-)


diff --git a/Lib/test/imghdrdata/python.bmp b/Lib/test/imghdrdata/python.bmp
new file mode 100644
index 0000000000000000000000000000000000000000..675f95191a45fdb3ae845996d6871b86286f848a
GIT binary patch
[stripped]
diff --git a/Lib/test/imghdrdata/python.gif b/Lib/test/imghdrdata/python.gif
new file mode 100644
index 0000000000000000000000000000000000000000..96fd9fef76b10899807d3724c97335f35a03743a
GIT binary patch
[stripped]
diff --git a/Lib/test/imghdrdata/python.jpg b/Lib/test/imghdrdata/python.jpg
new file mode 100644
index 0000000000000000000000000000000000000000..21222c09f5a71d9d86a0323e49ebc5da6639b22f
GIT binary patch
[stripped]
diff --git a/Lib/test/imghdrdata/python.pbm b/Lib/test/imghdrdata/python.pbm
new file mode 100644
--- /dev/null
+++ b/Lib/test/imghdrdata/python.pbm
@@ -0,0 +1,3 @@
+P4
+16 16
+ûñ¿úßÕ­±[ñ¥a_ÁX°°ðððð?ÿÿ
\ No newline at end of file
diff --git a/Lib/test/imghdrdata/python.pgm b/Lib/test/imghdrdata/python.pgm
new file mode 100644
index 0000000000000000000000000000000000000000..8349f2a53a9be1d895a561cc0fd31f91d7d42b33
GIT binary patch
[stripped]
diff --git a/Lib/test/imghdrdata/python.png b/Lib/test/imghdrdata/python.png
new file mode 100644
index 0000000000000000000000000000000000000000..1a987f79fcd248a94fcd1c45934136919cc0ffdd
GIT binary patch
[stripped]
diff --git a/Lib/test/imghdrdata/python.ppm b/Lib/test/imghdrdata/python.ppm
new file mode 100644
index 0000000000000000000000000000000000000000..7d9cdb3215877b4604de4c177fc81ebb645b8290
GIT binary patch
[stripped]
diff --git a/Lib/test/imghdrdata/python.ras b/Lib/test/imghdrdata/python.ras
new file mode 100644
index 0000000000000000000000000000000000000000..130e96f817ed9dd45f411f2f93e0749a4b33ba49
GIT binary patch
[stripped]
diff --git a/Lib/test/imghdrdata/python.sgi b/Lib/test/imghdrdata/python.sgi
new file mode 100644
index 0000000000000000000000000000000000000000..ffe9081c7a5b67ed14285041fd0650f4010bc36d
GIT binary patch
[stripped]
diff --git a/Lib/test/imghdrdata/python.tiff b/Lib/test/imghdrdata/python.tiff
new file mode 100644
index 0000000000000000000000000000000000000000..39d0bfcec02533ade8934a8796aebb8990c9d389
GIT binary patch
[stripped]
diff --git a/Lib/test/imghdrdata/python.xbm b/Lib/test/imghdrdata/python.xbm
new file mode 100644
--- /dev/null
+++ b/Lib/test/imghdrdata/python.xbm
@@ -0,0 +1,6 @@
+#define python_width 16
+#define python_height 16
+static char python_bits[] = {
+  0xDF, 0xFE, 0x8F, 0xFD, 0x5F, 0xFB, 0xAB, 0xFE, 0xB5, 0x8D, 0xDA, 0x8F, 
+  0xA5, 0x86, 0xFA, 0x83, 0x1A, 0x80, 0x0D, 0x80, 0x0D, 0x80, 0x0F, 0xE0, 
+  0x0F, 0xF8, 0x0F, 0xF8, 0x0F, 0xFC, 0xFF, 0xFF, };
diff --git a/Lib/test/test_imghdr.py b/Lib/test/test_imghdr.py
new file mode 100644
--- /dev/null
+++ b/Lib/test/test_imghdr.py
@@ -0,0 +1,120 @@
+import imghdr
+import io
+import sys
+import unittest
+from test.test_support import findfile, TESTFN, unlink, run_unittest
+
+TEST_FILES = (
+    ('python.png', 'png'),
+    ('python.gif', 'gif'),
+    ('python.bmp', 'bmp'),
+    ('python.ppm', 'ppm'),
+    ('python.pgm', 'pgm'),
+    ('python.pbm', 'pbm'),
+    ('python.jpg', 'jpeg'),
+    ('python.ras', 'rast'),
+    ('python.sgi', 'rgb'),
+    ('python.tiff', 'tiff'),
+    ('python.xbm', 'xbm')
+)
+
+class UnseekableIO(io.FileIO):
+    def tell(self):
+        raise io.UnsupportedOperation
+
+    def seek(self, *args, **kwargs):
+        raise io.UnsupportedOperation
+
+class TestImghdr(unittest.TestCase):
+    @classmethod
+    def setUpClass(cls):
+        cls.testfile = findfile('python.png', subdir='imghdrdata')
+        with open(cls.testfile, 'rb') as stream:
+            cls.testdata = stream.read()
+
+    def tearDown(self):
+        unlink(TESTFN)
+
+    def test_data(self):
+        for filename, expected in TEST_FILES:
+            filename = findfile(filename, subdir='imghdrdata')
+            self.assertEqual(imghdr.what(filename), expected)
+            ufilename = filename.decode(sys.getfilesystemencoding())
+            self.assertEqual(imghdr.what(ufilename), expected)
+            with open(filename, 'rb') as stream:
+                self.assertEqual(imghdr.what(stream), expected)
+            with open(filename, 'rb') as stream:
+                data = stream.read()
+            self.assertEqual(imghdr.what(None, data), expected)
+
+    def test_register_test(self):
+        def test_jumbo(h, file):
+            if h.startswith(b'eggs'):
+                return 'ham'
+        imghdr.tests.append(test_jumbo)
+        self.addCleanup(imghdr.tests.pop)
+        self.assertEqual(imghdr.what(None, b'eggs'), 'ham')
+
+    def test_file_pos(self):
+        with open(TESTFN, 'wb') as stream:
+            stream.write(b'ababagalamaga')
+            pos = stream.tell()
+            stream.write(self.testdata)
+        with open(TESTFN, 'rb') as stream:
+            stream.seek(pos)
+            self.assertEqual(imghdr.what(stream), 'png')
+            self.assertEqual(stream.tell(), pos)
+
+    def test_bad_args(self):
+        with self.assertRaises(TypeError):
+            imghdr.what()
+        with self.assertRaises(AttributeError):
+            imghdr.what(None)
+        with self.assertRaises(TypeError):
+            imghdr.what(self.testfile, 1)
+        with open(self.testfile, 'rb') as f:
+            with self.assertRaises(AttributeError):
+                imghdr.what(f.fileno())
+
+    def test_invalid_headers(self):
+        for header in (b'\211PN\r\n',
+                       b'\001\331',
+                       b'\x59\xA6',
+                       b'cutecat',
+                       b'000000JFI',
+                       b'GIF80'):
+            self.assertIsNone(imghdr.what(None, header))
+
+    def test_missing_file(self):
+        with self.assertRaises(IOError):
+            imghdr.what('missing')
+
+    def test_closed_file(self):
+        stream = open(self.testfile, 'rb')
+        stream.close()
+        with self.assertRaises(ValueError) as cm:
+            imghdr.what(stream)
+        stream = io.BytesIO(self.testdata)
+        stream.close()
+        with self.assertRaises(ValueError) as cm:
+            imghdr.what(stream)
+
+    def test_unseekable(self):
+        with open(TESTFN, 'wb') as stream:
+            stream.write(self.testdata)
+        with UnseekableIO(TESTFN, 'rb') as stream:
+            with self.assertRaises(io.UnsupportedOperation):
+                imghdr.what(stream)
+
+    def test_output_stream(self):
+        with open(TESTFN, 'wb') as stream:
+            stream.write(self.testdata)
+            stream.seek(0)
+            with self.assertRaises(IOError) as cm:
+                imghdr.what(stream)
+
+def test_main():
+    run_unittest(TestImghdr)
+
+if __name__ == '__main__':
+    test_main()
diff --git a/Lib/test/test_sundry.py b/Lib/test/test_sundry.py
--- a/Lib/test/test_sundry.py
+++ b/Lib/test/test_sundry.py
@@ -49,7 +49,6 @@
             import getpass
             import htmlentitydefs
             import ihooks
-            import imghdr
             import imputil
             import keyword
             import linecache
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -203,6 +203,9 @@
 Tests
 -----
 
+- Issue #19990: Added tests for the imghdr module.  Based on patch by
+  Claudiu Popa.
+
 - Issue #19804: The test_find_mac test in test_uuid is now skipped if the
   ifconfig executable is not available.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list