ยปCore Development>Code coverage>Lib/test/test_imghdr.py

Python code coverage for Lib/test/test_imghdr.py

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