1 | n/a | import imghdr |
---|
2 | n/a | import io |
---|
3 | n/a | import os |
---|
4 | n/a | import pathlib |
---|
5 | n/a | import unittest |
---|
6 | n/a | import warnings |
---|
7 | n/a | from test.support import findfile, TESTFN, unlink |
---|
8 | n/a | |
---|
9 | n/a | TEST_FILES = ( |
---|
10 | n/a | ('python.png', 'png'), |
---|
11 | n/a | ('python.gif', 'gif'), |
---|
12 | n/a | ('python.bmp', 'bmp'), |
---|
13 | n/a | ('python.ppm', 'ppm'), |
---|
14 | n/a | ('python.pgm', 'pgm'), |
---|
15 | n/a | ('python.pbm', 'pbm'), |
---|
16 | n/a | ('python.jpg', 'jpeg'), |
---|
17 | n/a | ('python.ras', 'rast'), |
---|
18 | n/a | ('python.sgi', 'rgb'), |
---|
19 | n/a | ('python.tiff', 'tiff'), |
---|
20 | n/a | ('python.xbm', 'xbm'), |
---|
21 | n/a | ('python.webp', 'webp'), |
---|
22 | n/a | ('python.exr', 'exr'), |
---|
23 | n/a | ) |
---|
24 | n/a | |
---|
25 | n/a | class UnseekableIO(io.FileIO): |
---|
26 | n/a | def tell(self): |
---|
27 | n/a | raise io.UnsupportedOperation |
---|
28 | n/a | |
---|
29 | n/a | def seek(self, *args, **kwargs): |
---|
30 | n/a | raise io.UnsupportedOperation |
---|
31 | n/a | |
---|
32 | n/a | class TestImghdr(unittest.TestCase): |
---|
33 | n/a | @classmethod |
---|
34 | n/a | def setUpClass(cls): |
---|
35 | n/a | cls.testfile = findfile('python.png', subdir='imghdrdata') |
---|
36 | n/a | with open(cls.testfile, 'rb') as stream: |
---|
37 | n/a | cls.testdata = stream.read() |
---|
38 | n/a | |
---|
39 | n/a | def tearDown(self): |
---|
40 | n/a | unlink(TESTFN) |
---|
41 | n/a | |
---|
42 | n/a | def test_data(self): |
---|
43 | n/a | for filename, expected in TEST_FILES: |
---|
44 | n/a | filename = findfile(filename, subdir='imghdrdata') |
---|
45 | n/a | self.assertEqual(imghdr.what(filename), expected) |
---|
46 | n/a | with open(filename, 'rb') as stream: |
---|
47 | n/a | self.assertEqual(imghdr.what(stream), expected) |
---|
48 | n/a | with open(filename, 'rb') as stream: |
---|
49 | n/a | data = stream.read() |
---|
50 | n/a | self.assertEqual(imghdr.what(None, data), expected) |
---|
51 | n/a | self.assertEqual(imghdr.what(None, bytearray(data)), expected) |
---|
52 | n/a | |
---|
53 | n/a | def test_pathlike_filename(self): |
---|
54 | n/a | for filename, expected in TEST_FILES: |
---|
55 | n/a | with self.subTest(filename=filename): |
---|
56 | n/a | filename = findfile(filename, subdir='imghdrdata') |
---|
57 | n/a | self.assertEqual(imghdr.what(pathlib.Path(filename)), expected) |
---|
58 | n/a | |
---|
59 | n/a | def test_register_test(self): |
---|
60 | n/a | def test_jumbo(h, file): |
---|
61 | n/a | if h.startswith(b'eggs'): |
---|
62 | n/a | return 'ham' |
---|
63 | n/a | imghdr.tests.append(test_jumbo) |
---|
64 | n/a | self.addCleanup(imghdr.tests.pop) |
---|
65 | n/a | self.assertEqual(imghdr.what(None, b'eggs'), 'ham') |
---|
66 | n/a | |
---|
67 | n/a | def test_file_pos(self): |
---|
68 | n/a | with open(TESTFN, 'wb') as stream: |
---|
69 | n/a | stream.write(b'ababagalamaga') |
---|
70 | n/a | pos = stream.tell() |
---|
71 | n/a | stream.write(self.testdata) |
---|
72 | n/a | with open(TESTFN, 'rb') as stream: |
---|
73 | n/a | stream.seek(pos) |
---|
74 | n/a | self.assertEqual(imghdr.what(stream), 'png') |
---|
75 | n/a | self.assertEqual(stream.tell(), pos) |
---|
76 | n/a | |
---|
77 | n/a | def test_bad_args(self): |
---|
78 | n/a | with self.assertRaises(TypeError): |
---|
79 | n/a | imghdr.what() |
---|
80 | n/a | with self.assertRaises(AttributeError): |
---|
81 | n/a | imghdr.what(None) |
---|
82 | n/a | with self.assertRaises(TypeError): |
---|
83 | n/a | imghdr.what(self.testfile, 1) |
---|
84 | n/a | with self.assertRaises(AttributeError): |
---|
85 | n/a | imghdr.what(os.fsencode(self.testfile)) |
---|
86 | n/a | with open(self.testfile, 'rb') as f: |
---|
87 | n/a | with self.assertRaises(AttributeError): |
---|
88 | n/a | imghdr.what(f.fileno()) |
---|
89 | n/a | |
---|
90 | n/a | def test_invalid_headers(self): |
---|
91 | n/a | for header in (b'\211PN\r\n', |
---|
92 | n/a | b'\001\331', |
---|
93 | n/a | b'\x59\xA6', |
---|
94 | n/a | b'cutecat', |
---|
95 | n/a | b'000000JFI', |
---|
96 | n/a | b'GIF80'): |
---|
97 | n/a | self.assertIsNone(imghdr.what(None, header)) |
---|
98 | n/a | |
---|
99 | n/a | def test_string_data(self): |
---|
100 | n/a | with warnings.catch_warnings(): |
---|
101 | n/a | warnings.simplefilter("ignore", BytesWarning) |
---|
102 | n/a | for filename, _ in TEST_FILES: |
---|
103 | n/a | filename = findfile(filename, subdir='imghdrdata') |
---|
104 | n/a | with open(filename, 'rb') as stream: |
---|
105 | n/a | data = stream.read().decode('latin1') |
---|
106 | n/a | with self.assertRaises(TypeError): |
---|
107 | n/a | imghdr.what(io.StringIO(data)) |
---|
108 | n/a | with self.assertRaises(TypeError): |
---|
109 | n/a | imghdr.what(None, data) |
---|
110 | n/a | |
---|
111 | n/a | def test_missing_file(self): |
---|
112 | n/a | with self.assertRaises(FileNotFoundError): |
---|
113 | n/a | imghdr.what('missing') |
---|
114 | n/a | |
---|
115 | n/a | def test_closed_file(self): |
---|
116 | n/a | stream = open(self.testfile, 'rb') |
---|
117 | n/a | stream.close() |
---|
118 | n/a | with self.assertRaises(ValueError) as cm: |
---|
119 | n/a | imghdr.what(stream) |
---|
120 | n/a | stream = io.BytesIO(self.testdata) |
---|
121 | n/a | stream.close() |
---|
122 | n/a | with self.assertRaises(ValueError) as cm: |
---|
123 | n/a | imghdr.what(stream) |
---|
124 | n/a | |
---|
125 | n/a | def test_unseekable(self): |
---|
126 | n/a | with open(TESTFN, 'wb') as stream: |
---|
127 | n/a | stream.write(self.testdata) |
---|
128 | n/a | with UnseekableIO(TESTFN, 'rb') as stream: |
---|
129 | n/a | with self.assertRaises(io.UnsupportedOperation): |
---|
130 | n/a | imghdr.what(stream) |
---|
131 | n/a | |
---|
132 | n/a | def test_output_stream(self): |
---|
133 | n/a | with open(TESTFN, 'wb') as stream: |
---|
134 | n/a | stream.write(self.testdata) |
---|
135 | n/a | stream.seek(0) |
---|
136 | n/a | with self.assertRaises(OSError) as cm: |
---|
137 | n/a | imghdr.what(stream) |
---|
138 | n/a | |
---|
139 | n/a | if __name__ == '__main__': |
---|
140 | n/a | unittest.main() |
---|