ยปCore Development>Code coverage>Lib/imghdr.py

Python code coverage for Lib/imghdr.py

#countcontent
1n/a"""Recognize image file formats based on their first few bytes."""
2n/a
3n/afrom os import PathLike
4n/a
5n/a__all__ = ["what"]
6n/a
7n/a#-------------------------#
8n/a# Recognize image headers #
9n/a#-------------------------#
10n/a
11n/adef what(file, h=None):
12n/a f = None
13n/a try:
14n/a if h is None:
15n/a if isinstance(file, (str, PathLike)):
16n/a f = open(file, 'rb')
17n/a h = f.read(32)
18n/a else:
19n/a location = file.tell()
20n/a h = file.read(32)
21n/a file.seek(location)
22n/a for tf in tests:
23n/a res = tf(h, f)
24n/a if res:
25n/a return res
26n/a finally:
27n/a if f: f.close()
28n/a return None
29n/a
30n/a
31n/a#---------------------------------#
32n/a# Subroutines per image file type #
33n/a#---------------------------------#
34n/a
35n/atests = []
36n/a
37n/adef test_jpeg(h, f):
38n/a """JPEG data in JFIF or Exif format"""
39n/a if h[6:10] in (b'JFIF', b'Exif'):
40n/a return 'jpeg'
41n/a
42n/atests.append(test_jpeg)
43n/a
44n/adef test_png(h, f):
45n/a if h.startswith(b'\211PNG\r\n\032\n'):
46n/a return 'png'
47n/a
48n/atests.append(test_png)
49n/a
50n/adef test_gif(h, f):
51n/a """GIF ('87 and '89 variants)"""
52n/a if h[:6] in (b'GIF87a', b'GIF89a'):
53n/a return 'gif'
54n/a
55n/atests.append(test_gif)
56n/a
57n/adef test_tiff(h, f):
58n/a """TIFF (can be in Motorola or Intel byte order)"""
59n/a if h[:2] in (b'MM', b'II'):
60n/a return 'tiff'
61n/a
62n/atests.append(test_tiff)
63n/a
64n/adef test_rgb(h, f):
65n/a """SGI image library"""
66n/a if h.startswith(b'\001\332'):
67n/a return 'rgb'
68n/a
69n/atests.append(test_rgb)
70n/a
71n/adef test_pbm(h, f):
72n/a """PBM (portable bitmap)"""
73n/a if len(h) >= 3 and \
74n/a h[0] == ord(b'P') and h[1] in b'14' and h[2] in b' \t\n\r':
75n/a return 'pbm'
76n/a
77n/atests.append(test_pbm)
78n/a
79n/adef test_pgm(h, f):
80n/a """PGM (portable graymap)"""
81n/a if len(h) >= 3 and \
82n/a h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r':
83n/a return 'pgm'
84n/a
85n/atests.append(test_pgm)
86n/a
87n/adef test_ppm(h, f):
88n/a """PPM (portable pixmap)"""
89n/a if len(h) >= 3 and \
90n/a h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r':
91n/a return 'ppm'
92n/a
93n/atests.append(test_ppm)
94n/a
95n/adef test_rast(h, f):
96n/a """Sun raster file"""
97n/a if h.startswith(b'\x59\xA6\x6A\x95'):
98n/a return 'rast'
99n/a
100n/atests.append(test_rast)
101n/a
102n/adef test_xbm(h, f):
103n/a """X bitmap (X10 or X11)"""
104n/a if h.startswith(b'#define '):
105n/a return 'xbm'
106n/a
107n/atests.append(test_xbm)
108n/a
109n/adef test_bmp(h, f):
110n/a if h.startswith(b'BM'):
111n/a return 'bmp'
112n/a
113n/atests.append(test_bmp)
114n/a
115n/adef test_webp(h, f):
116n/a if h.startswith(b'RIFF') and h[8:12] == b'WEBP':
117n/a return 'webp'
118n/a
119n/atests.append(test_webp)
120n/a
121n/adef test_exr(h, f):
122n/a if h.startswith(b'\x76\x2f\x31\x01'):
123n/a return 'exr'
124n/a
125n/atests.append(test_exr)
126n/a
127n/a#--------------------#
128n/a# Small test program #
129n/a#--------------------#
130n/a
131n/adef test():
132n/a import sys
133n/a recursive = 0
134n/a if sys.argv[1:] and sys.argv[1] == '-r':
135n/a del sys.argv[1:2]
136n/a recursive = 1
137n/a try:
138n/a if sys.argv[1:]:
139n/a testall(sys.argv[1:], recursive, 1)
140n/a else:
141n/a testall(['.'], recursive, 1)
142n/a except KeyboardInterrupt:
143n/a sys.stderr.write('\n[Interrupted]\n')
144n/a sys.exit(1)
145n/a
146n/adef testall(list, recursive, toplevel):
147n/a import sys
148n/a import os
149n/a for filename in list:
150n/a if os.path.isdir(filename):
151n/a print(filename + '/:', end=' ')
152n/a if recursive or toplevel:
153n/a print('recursing down:')
154n/a import glob
155n/a names = glob.glob(os.path.join(filename, '*'))
156n/a testall(names, recursive, 0)
157n/a else:
158n/a print('*** directory (use -r) ***')
159n/a else:
160n/a print(filename + ':', end=' ')
161n/a sys.stdout.flush()
162n/a try:
163n/a print(what(filename))
164n/a except OSError:
165n/a print('*** not found ***')
166n/a
167n/aif __name__ == '__main__':
168n/a test()