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.
This commit is contained in:
parent
46fc337395
commit
72937f3216
|
@ -36,14 +36,14 @@ tests = []
|
||||||
|
|
||||||
def test_rgb(h, f):
|
def test_rgb(h, f):
|
||||||
"""SGI image library"""
|
"""SGI image library"""
|
||||||
if h[:2] == '\001\332':
|
if h[:2] == b'\001\332':
|
||||||
return 'rgb'
|
return 'rgb'
|
||||||
|
|
||||||
tests.append(test_rgb)
|
tests.append(test_rgb)
|
||||||
|
|
||||||
def test_gif(h, f):
|
def test_gif(h, f):
|
||||||
"""GIF ('87 and '89 variants)"""
|
"""GIF ('87 and '89 variants)"""
|
||||||
if h[:6] in ('GIF87a', 'GIF89a'):
|
if h[:6] in (b'GIF87a', b'GIF89a'):
|
||||||
return 'gif'
|
return 'gif'
|
||||||
|
|
||||||
tests.append(test_gif)
|
tests.append(test_gif)
|
||||||
|
@ -51,7 +51,7 @@ tests.append(test_gif)
|
||||||
def test_pbm(h, f):
|
def test_pbm(h, f):
|
||||||
"""PBM (portable bitmap)"""
|
"""PBM (portable bitmap)"""
|
||||||
if len(h) >= 3 and \
|
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'
|
return 'pbm'
|
||||||
|
|
||||||
tests.append(test_pbm)
|
tests.append(test_pbm)
|
||||||
|
@ -59,7 +59,7 @@ tests.append(test_pbm)
|
||||||
def test_pgm(h, f):
|
def test_pgm(h, f):
|
||||||
"""PGM (portable graymap)"""
|
"""PGM (portable graymap)"""
|
||||||
if len(h) >= 3 and \
|
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'
|
return 'pgm'
|
||||||
|
|
||||||
tests.append(test_pgm)
|
tests.append(test_pgm)
|
||||||
|
@ -67,28 +67,28 @@ tests.append(test_pgm)
|
||||||
def test_ppm(h, f):
|
def test_ppm(h, f):
|
||||||
"""PPM (portable pixmap)"""
|
"""PPM (portable pixmap)"""
|
||||||
if len(h) >= 3 and \
|
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'
|
return 'ppm'
|
||||||
|
|
||||||
tests.append(test_ppm)
|
tests.append(test_ppm)
|
||||||
|
|
||||||
def test_tiff(h, f):
|
def test_tiff(h, f):
|
||||||
"""TIFF (can be in Motorola or Intel byte order)"""
|
"""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'
|
return 'tiff'
|
||||||
|
|
||||||
tests.append(test_tiff)
|
tests.append(test_tiff)
|
||||||
|
|
||||||
def test_rast(h, f):
|
def test_rast(h, f):
|
||||||
"""Sun raster file"""
|
"""Sun raster file"""
|
||||||
if h[:4] == '\x59\xA6\x6A\x95':
|
if h[:4] == b'\x59\xA6\x6A\x95':
|
||||||
return 'rast'
|
return 'rast'
|
||||||
|
|
||||||
tests.append(test_rast)
|
tests.append(test_rast)
|
||||||
|
|
||||||
def test_xbm(h, f):
|
def test_xbm(h, f):
|
||||||
"""X bitmap (X10 or X11)"""
|
"""X bitmap (X10 or X11)"""
|
||||||
s = '#define '
|
s = b'#define '
|
||||||
if h[:len(s)] == s:
|
if h[:len(s)] == s:
|
||||||
return 'xbm'
|
return 'xbm'
|
||||||
|
|
||||||
|
@ -96,26 +96,26 @@ tests.append(test_xbm)
|
||||||
|
|
||||||
def test_jpeg(h, f):
|
def test_jpeg(h, f):
|
||||||
"""JPEG data in JFIF format"""
|
"""JPEG data in JFIF format"""
|
||||||
if h[6:10] == 'JFIF':
|
if h[6:10] == b'JFIF':
|
||||||
return 'jpeg'
|
return 'jpeg'
|
||||||
|
|
||||||
tests.append(test_jpeg)
|
tests.append(test_jpeg)
|
||||||
|
|
||||||
def test_exif(h, f):
|
def test_exif(h, f):
|
||||||
"""JPEG data in Exif format"""
|
"""JPEG data in Exif format"""
|
||||||
if h[6:10] == 'Exif':
|
if h[6:10] == b'Exif':
|
||||||
return 'jpeg'
|
return 'jpeg'
|
||||||
|
|
||||||
tests.append(test_exif)
|
tests.append(test_exif)
|
||||||
|
|
||||||
def test_bmp(h, f):
|
def test_bmp(h, f):
|
||||||
if h[:2] == 'BM':
|
if h[:2] == b'BM':
|
||||||
return 'bmp'
|
return 'bmp'
|
||||||
|
|
||||||
tests.append(test_bmp)
|
tests.append(test_bmp)
|
||||||
|
|
||||||
def test_png(h, f):
|
def test_png(h, f):
|
||||||
if h[:8] == "\211PNG\r\n\032\n":
|
if h[:8] == b'\211PNG\r\n\032\n':
|
||||||
return 'png'
|
return 'png'
|
||||||
|
|
||||||
tests.append(test_png)
|
tests.append(test_png)
|
||||||
|
@ -159,3 +159,6 @@ def testall(list, recursive, toplevel):
|
||||||
print(what(filename))
|
print(what(filename))
|
||||||
except IOError:
|
except IOError:
|
||||||
print('*** not found ***')
|
print('*** not found ***')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test()
|
||||||
|
|
|
@ -30,23 +30,23 @@ explicitly given directories.
|
||||||
# The file structure is top-down except that the test program and its
|
# The file structure is top-down except that the test program and its
|
||||||
# subroutine come last.
|
# subroutine come last.
|
||||||
|
|
||||||
__all__ = ["what","whathdr"]
|
__all__ = ['what', 'whathdr']
|
||||||
|
|
||||||
def what(filename):
|
def what(filename):
|
||||||
"""Guess the type of a sound file"""
|
"""Guess the type of a sound file."""
|
||||||
res = whathdr(filename)
|
res = whathdr(filename)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
def whathdr(filename):
|
def whathdr(filename):
|
||||||
"""Recognize sound headers"""
|
"""Recognize sound headers."""
|
||||||
f = open(filename, 'rb')
|
with open(filename, 'rb') as f:
|
||||||
h = f.read(512)
|
h = f.read(512)
|
||||||
for tf in tests:
|
for tf in tests:
|
||||||
res = tf(h, f)
|
res = tf(h, f)
|
||||||
if res:
|
if res:
|
||||||
return res
|
return res
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
#-----------------------------------#
|
#-----------------------------------#
|
||||||
|
@ -57,12 +57,12 @@ tests = []
|
||||||
|
|
||||||
def test_aifc(h, f):
|
def test_aifc(h, f):
|
||||||
import aifc
|
import aifc
|
||||||
if h[:4] != 'FORM':
|
if h[:4] != b'FORM':
|
||||||
return None
|
return None
|
||||||
if h[8:12] == 'AIFC':
|
if h[8:12] == b'AIFC':
|
||||||
fmt = 'aifc'
|
fmt = 'aifc'
|
||||||
elif h[8:12] == 'AIFF':
|
elif h[8:12] == b'AIFF':
|
||||||
fmt = 'aiff'
|
fmt = b'aiff'
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
f.seek(0)
|
f.seek(0)
|
||||||
|
@ -70,25 +70,25 @@ def test_aifc(h, f):
|
||||||
a = aifc.openfp(f, 'r')
|
a = aifc.openfp(f, 'r')
|
||||||
except (EOFError, aifc.Error):
|
except (EOFError, aifc.Error):
|
||||||
return None
|
return None
|
||||||
return (fmt, a.getframerate(), a.getnchannels(), \
|
return (fmt, a.getframerate(), a.getnchannels(),
|
||||||
a.getnframes(), 8*a.getsampwidth())
|
a.getnframes(), 8 * a.getsampwidth())
|
||||||
|
|
||||||
tests.append(test_aifc)
|
tests.append(test_aifc)
|
||||||
|
|
||||||
|
|
||||||
def test_au(h, f):
|
def test_au(h, f):
|
||||||
if h[:4] == '.snd':
|
if h[:4] == b'.snd':
|
||||||
f = get_long_be
|
func = get_long_be
|
||||||
elif h[:4] in ('\0ds.', 'dns.'):
|
elif h[:4] in (b'\0ds.', b'dns.'):
|
||||||
f = get_long_le
|
func = get_long_le
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
type = 'au'
|
filetype = 'au'
|
||||||
hdr_size = f(h[4:8])
|
hdr_size = func(h[4:8])
|
||||||
data_size = f(h[8:12])
|
data_size = func(h[8:12])
|
||||||
encoding = f(h[12:16])
|
encoding = func(h[12:16])
|
||||||
rate = f(h[16:20])
|
rate = func(h[16:20])
|
||||||
nchannels = f(h[20:24])
|
nchannels = func(h[20:24])
|
||||||
sample_size = 1 # default
|
sample_size = 1 # default
|
||||||
if encoding == 1:
|
if encoding == 1:
|
||||||
sample_bits = 'U'
|
sample_bits = 'U'
|
||||||
|
@ -100,26 +100,26 @@ def test_au(h, f):
|
||||||
else:
|
else:
|
||||||
sample_bits = '?'
|
sample_bits = '?'
|
||||||
frame_size = sample_size * nchannels
|
frame_size = sample_size * nchannels
|
||||||
return type, rate, nchannels, data_size/frame_size, sample_bits
|
return filetype, rate, nchannels, data_size / frame_size, sample_bits
|
||||||
|
|
||||||
tests.append(test_au)
|
tests.append(test_au)
|
||||||
|
|
||||||
|
|
||||||
def test_hcom(h, f):
|
def test_hcom(h, f):
|
||||||
if h[65:69] != 'FSSD' or h[128:132] != 'HCOM':
|
if h[65:69] != b'FSSD' or h[128:132] != b'HCOM':
|
||||||
return None
|
return None
|
||||||
divisor = get_long_be(h[128+16:128+20])
|
divisor = get_long_be(h[128+16:128+20])
|
||||||
return 'hcom', 22050/divisor, 1, -1, 8
|
return 'hcom', 22050 / divisor, 1, -1, 8
|
||||||
|
|
||||||
tests.append(test_hcom)
|
tests.append(test_hcom)
|
||||||
|
|
||||||
|
|
||||||
def test_voc(h, f):
|
def test_voc(h, f):
|
||||||
if h[:20] != 'Creative Voice File\032':
|
if h[:20] != b'Creative Voice File\032':
|
||||||
return None
|
return None
|
||||||
sbseek = get_short_le(h[20:22])
|
sbseek = get_short_le(h[20:22])
|
||||||
rate = 0
|
rate = 0
|
||||||
if 0 <= sbseek < 500 and h[sbseek] == '\1':
|
if 0 <= sbseek < 500 and h[sbseek] == b'\1':
|
||||||
ratecode = ord(h[sbseek+4])
|
ratecode = ord(h[sbseek+4])
|
||||||
rate = int(1000000.0 / (256 - ratecode))
|
rate = int(1000000.0 / (256 - ratecode))
|
||||||
return 'voc', rate, 1, -1, 8
|
return 'voc', rate, 1, -1, 8
|
||||||
|
@ -129,7 +129,7 @@ tests.append(test_voc)
|
||||||
|
|
||||||
def test_wav(h, f):
|
def test_wav(h, f):
|
||||||
# 'RIFF' <len> 'WAVE' 'fmt ' <len>
|
# 'RIFF' <len> 'WAVE' 'fmt ' <len>
|
||||||
if h[:4] != 'RIFF' or h[8:12] != 'WAVE' or h[12:16] != 'fmt ':
|
if h[:4] != b'RIFF' or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
|
||||||
return None
|
return None
|
||||||
style = get_short_le(h[20:22])
|
style = get_short_le(h[20:22])
|
||||||
nchannels = get_short_le(h[22:24])
|
nchannels = get_short_le(h[22:24])
|
||||||
|
@ -141,7 +141,7 @@ tests.append(test_wav)
|
||||||
|
|
||||||
|
|
||||||
def test_8svx(h, f):
|
def test_8svx(h, f):
|
||||||
if h[:4] != 'FORM' or h[8:12] != '8SVX':
|
if h[:4] != b'FORM' or h[8:12] != b'8SVX':
|
||||||
return None
|
return None
|
||||||
# Should decode it to get #channels -- assume always 1
|
# Should decode it to get #channels -- assume always 1
|
||||||
return '8svx', 0, 1, 0, 8
|
return '8svx', 0, 1, 0, 8
|
||||||
|
@ -150,7 +150,7 @@ tests.append(test_8svx)
|
||||||
|
|
||||||
|
|
||||||
def test_sndt(h, f):
|
def test_sndt(h, f):
|
||||||
if h[:5] == 'SOUND':
|
if h[:5] == b'SOUND':
|
||||||
nsamples = get_long_le(h[8:12])
|
nsamples = get_long_le(h[8:12])
|
||||||
rate = get_short_le(h[20:22])
|
rate = get_short_le(h[20:22])
|
||||||
return 'sndt', rate, 1, nsamples, 8
|
return 'sndt', rate, 1, nsamples, 8
|
||||||
|
@ -159,7 +159,7 @@ tests.append(test_sndt)
|
||||||
|
|
||||||
|
|
||||||
def test_sndr(h, f):
|
def test_sndr(h, f):
|
||||||
if h[:2] == '\0\0':
|
if h[:2] == b'\0\0':
|
||||||
rate = get_short_le(h[2:4])
|
rate = get_short_le(h[2:4])
|
||||||
if 4000 <= rate <= 25000:
|
if 4000 <= rate <= 25000:
|
||||||
return 'sndr', rate, 1, -1, 8
|
return 'sndr', rate, 1, -1, 8
|
||||||
|
@ -167,21 +167,21 @@ def test_sndr(h, f):
|
||||||
tests.append(test_sndr)
|
tests.append(test_sndr)
|
||||||
|
|
||||||
|
|
||||||
#---------------------------------------------#
|
#-------------------------------------------#
|
||||||
# Subroutines to extract numbers from strings #
|
# Subroutines to extract numbers from bytes #
|
||||||
#---------------------------------------------#
|
#-------------------------------------------#
|
||||||
|
|
||||||
def get_long_be(s):
|
def get_long_be(b):
|
||||||
return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
|
return (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]
|
||||||
|
|
||||||
def get_long_le(s):
|
def get_long_le(b):
|
||||||
return (ord(s[3])<<24) | (ord(s[2])<<16) | (ord(s[1])<<8) | ord(s[0])
|
return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]
|
||||||
|
|
||||||
def get_short_be(s):
|
def get_short_be(b):
|
||||||
return (ord(s[0])<<8) | ord(s[1])
|
return (b[0] << 8) | b[1]
|
||||||
|
|
||||||
def get_short_le(s):
|
def get_short_le(b):
|
||||||
return (ord(s[1])<<8) | ord(s[0])
|
return (b[1] << 8) | b[0]
|
||||||
|
|
||||||
|
|
||||||
#--------------------#
|
#--------------------#
|
||||||
|
|
Loading…
Reference in New Issue