mirror of https://github.com/python/cpython
Issue #19990: Added tests for the imghdr module.
Based on patch by Claudiu Popa.
This commit is contained in:
commit
62a85b54a3
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
Binary file not shown.
After Width: | Height: | Size: 610 B |
Binary file not shown.
After Width: | Height: | Size: 543 B |
|
@ -0,0 +1,3 @@
|
|||
P4
|
||||
16 16
|
||||
ûñ¿úßÕ±[ñ¥a_ÁX°°ðððð?ÿÿ
|
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 1020 B |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -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, };
|
|
@ -0,0 +1,131 @@
|
|||
import imghdr
|
||||
import io
|
||||
import os
|
||||
import unittest
|
||||
import warnings
|
||||
from test.support import findfile, TESTFN, unlink
|
||||
|
||||
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)
|
||||
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)
|
||||
self.assertEqual(imghdr.what(None, bytearray(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 self.assertRaises(AttributeError):
|
||||
imghdr.what(os.fsencode(self.testfile))
|
||||
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_string_data(self):
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter("ignore", BytesWarning)
|
||||
for filename, _ in TEST_FILES:
|
||||
filename = findfile(filename, subdir='imghdrdata')
|
||||
with open(filename, 'rb') as stream:
|
||||
data = stream.read().decode('latin1')
|
||||
with self.assertRaises(TypeError):
|
||||
imghdr.what(io.StringIO(data))
|
||||
with self.assertRaises(TypeError):
|
||||
imghdr.what(None, data)
|
||||
|
||||
def test_missing_file(self):
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
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(OSError) as cm:
|
||||
imghdr.what(stream)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
|
@ -6,7 +6,7 @@ import unittest
|
|||
|
||||
class TestUntestedModules(unittest.TestCase):
|
||||
def test_untested_modules_can_be_imported(self):
|
||||
untested = ('bdb', 'encodings', 'formatter', 'imghdr',
|
||||
untested = ('bdb', 'encodings', 'formatter',
|
||||
'nturl2path', 'tabnanny')
|
||||
with support.check_warnings(quiet=True):
|
||||
for name in untested:
|
||||
|
|
|
@ -152,9 +152,15 @@ IDLE
|
|||
Tests
|
||||
-----
|
||||
|
||||
- Issue #19990: Added tests for the imghdr module. Based on patch by
|
||||
Claudiu Popa.
|
||||
|
||||
- Issue #20358: Tests for curses.window.overlay and curses.window.overwrite
|
||||
no longer specify min{row,col} > max{row,col}.
|
||||
|
||||
- Issue #19804: The test_find_mac test in test_uuid is now skipped if the
|
||||
ifconfig executable is not available.
|
||||
|
||||
- Issue #19886: Use better estimated memory requirements for bigmem tests.
|
||||
|
||||
Tools/Demos
|
||||
|
@ -551,9 +557,6 @@ IDLE
|
|||
Tests
|
||||
-----
|
||||
|
||||
- Issue #19804: The test_find_mac test in test_uuid is now skipped if the
|
||||
ifconfig executable is not available.
|
||||
|
||||
- Issue #20055: Fix test_shutil under Windows with symlink privileges held.
|
||||
Patch by Vajrasky Kok.
|
||||
|
||||
|
|
Loading…
Reference in New Issue