summaryrefslogtreecommitdiff
path: root/Lib/imghdr.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2007-08-12 14:37:20 +0000
committerBarry Warsaw <barry@python.org>2007-08-12 14:37:20 +0000
commit594e7a17f085238d654b91c61c132034b3f6901d (patch)
tree29800878cc229146ae08f2e8ea72161ba1af12ac /Lib/imghdr.py
parentc490c586e8e96c5968deabe2c6b99bb0d1f0cee6 (diff)
downloadcpython-594e7a17f085238d654b91c61c132034b3f6901d.tar.gz
Compare what's read from files against proper byte literals. Neither of these
modules have unittests AFAICT, and I'm not improving things here, but these changes make a bunch of email package tests succeed, and command line testing against real files seems to make things better. Added an __main__ section to imghdr.py so that it can be run from the command line similarly to sndhdr.py. Someone else can figure out a better -m way of doing this. In sndhdr.py do a couple of very minor cleanups, and use a with statement to not wait for gc to close the file.
Diffstat (limited to 'Lib/imghdr.py')
-rw-r--r--Lib/imghdr.py27
1 files changed, 15 insertions, 12 deletions
diff --git a/Lib/imghdr.py b/Lib/imghdr.py
index f34f234877..d0f66e5b72 100644
--- a/Lib/imghdr.py
+++ b/Lib/imghdr.py
@@ -36,14 +36,14 @@ tests = []
def test_rgb(h, f):
"""SGI image library"""
- if h[:2] == '\001\332':
+ if h[:2] == b'\001\332':
return 'rgb'
tests.append(test_rgb)
def test_gif(h, f):
"""GIF ('87 and '89 variants)"""
- if h[:6] in ('GIF87a', 'GIF89a'):
+ if h[:6] in (b'GIF87a', b'GIF89a'):
return 'gif'
tests.append(test_gif)
@@ -51,7 +51,7 @@ tests.append(test_gif)
def test_pbm(h, f):
"""PBM (portable bitmap)"""
if len(h) >= 3 and \
- h[0] == 'P' and h[1] in '14' and h[2] in ' \t\n\r':
+ h[0] == ord('P') and h[1] in b'14' and h[2] in b' \t\n\r':
return 'pbm'
tests.append(test_pbm)
@@ -59,7 +59,7 @@ tests.append(test_pbm)
def test_pgm(h, f):
"""PGM (portable graymap)"""
if len(h) >= 3 and \
- h[0] == 'P' and h[1] in '25' and h[2] in ' \t\n\r':
+ h[0] == ord('P') and h[1] in b'25' and h[2] in b' \t\n\r':
return 'pgm'
tests.append(test_pgm)
@@ -67,28 +67,28 @@ tests.append(test_pgm)
def test_ppm(h, f):
"""PPM (portable pixmap)"""
if len(h) >= 3 and \
- h[0] == 'P' and h[1] in '36' and h[2] in ' \t\n\r':
+ h[0] == ord('P') and h[1] in b'36' and h[2] in b' \t\n\r':
return 'ppm'
tests.append(test_ppm)
def test_tiff(h, f):
"""TIFF (can be in Motorola or Intel byte order)"""
- if h[:2] in ('MM', 'II'):
+ if h[:2] in (b'MM', b'II'):
return 'tiff'
tests.append(test_tiff)
def test_rast(h, f):
"""Sun raster file"""
- if h[:4] == '\x59\xA6\x6A\x95':
+ if h[:4] == b'\x59\xA6\x6A\x95':
return 'rast'
tests.append(test_rast)
def test_xbm(h, f):
"""X bitmap (X10 or X11)"""
- s = '#define '
+ s = b'#define '
if h[:len(s)] == s:
return 'xbm'
@@ -96,26 +96,26 @@ tests.append(test_xbm)
def test_jpeg(h, f):
"""JPEG data in JFIF format"""
- if h[6:10] == 'JFIF':
+ if h[6:10] == b'JFIF':
return 'jpeg'
tests.append(test_jpeg)
def test_exif(h, f):
"""JPEG data in Exif format"""
- if h[6:10] == 'Exif':
+ if h[6:10] == b'Exif':
return 'jpeg'
tests.append(test_exif)
def test_bmp(h, f):
- if h[:2] == 'BM':
+ if h[:2] == b'BM':
return 'bmp'
tests.append(test_bmp)
def test_png(h, f):
- if h[:8] == "\211PNG\r\n\032\n":
+ if h[:8] == b'\211PNG\r\n\032\n':
return 'png'
tests.append(test_png)
@@ -159,3 +159,6 @@ def testall(list, recursive, toplevel):
print(what(filename))
except IOError:
print('*** not found ***')
+
+if __name__ == '__main__':
+ test()