[Python-3000-checkins] r56987 - python/branches/py3k/Lib/imghdr.py python/branches/py3k/Lib/sndhdr.py

guido.van.rossum python-3000-checkins at python.org
Mon Aug 13 19:50:01 CEST 2007


Author: guido.van.rossum
Date: Mon Aug 13 19:50:00 2007
New Revision: 56987

Modified:
   python/branches/py3k/Lib/imghdr.py
   python/branches/py3k/Lib/sndhdr.py
Log:
Improved patches for sndhdr and imghdr by Victor Stinner, who writes:
 - fix "h[sbseek] == b'\1'" and "ratecode = ord(h[sbseek+4])" in test_voc()
 - avoid division by zero
 - use startswith method: replace h[:2] == b'BM' by h.startswith(b'BM')
 - use aifc.open() instead of old aifc.openfp()
 - use ord(b'P') instead of ord('P')


Modified: python/branches/py3k/Lib/imghdr.py
==============================================================================
--- python/branches/py3k/Lib/imghdr.py	(original)
+++ python/branches/py3k/Lib/imghdr.py	Mon Aug 13 19:50:00 2007
@@ -36,7 +36,7 @@
 
 def test_rgb(h, f):
     """SGI image library"""
-    if h[:2] == b'\001\332':
+    if h.startswith(b'\001\332'):
         return 'rgb'
 
 tests.append(test_rgb)
@@ -51,7 +51,7 @@
 def test_pbm(h, f):
     """PBM (portable bitmap)"""
     if len(h) >= 3 and \
-        h[0] == ord('P') and h[1] in b'14' and h[2] in b' \t\n\r':
+        h[0] == ord(b'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 @@
 def test_pgm(h, f):
     """PGM (portable graymap)"""
     if len(h) >= 3 and \
-        h[0] == ord('P') and h[1] in b'25' and h[2] in b' \t\n\r':
+        h[0] == ord(b'P') and h[1] in b'25' and h[2] in b' \t\n\r':
         return 'pgm'
 
 tests.append(test_pgm)
@@ -67,7 +67,7 @@
 def test_ppm(h, f):
     """PPM (portable pixmap)"""
     if len(h) >= 3 and \
-        h[0] == ord('P') and h[1] in b'36' and h[2] in b' \t\n\r':
+        h[0] == ord(b'P') and h[1] in b'36' and h[2] in b' \t\n\r':
         return 'ppm'
 
 tests.append(test_ppm)
@@ -81,41 +81,33 @@
 
 def test_rast(h, f):
     """Sun raster file"""
-    if h[:4] == b'\x59\xA6\x6A\x95':
+    if h.startswith(b'\x59\xA6\x6A\x95'):
         return 'rast'
 
 tests.append(test_rast)
 
 def test_xbm(h, f):
     """X bitmap (X10 or X11)"""
-    s = b'#define '
-    if h[:len(s)] == s:
+    if h.startswith(b'#define '):
         return 'xbm'
 
 tests.append(test_xbm)
 
 def test_jpeg(h, f):
-    """JPEG data in JFIF format"""
-    if h[6:10] == b'JFIF':
+    """JPEG data in JFIF or Exif format"""
+    if h[6:10] in (b'JFIF', b'Exif'):
         return 'jpeg'
 
 tests.append(test_jpeg)
 
-def test_exif(h, f):
-    """JPEG data in Exif format"""
-    if h[6:10] == b'Exif':
-        return 'jpeg'
-
-tests.append(test_exif)
-
 def test_bmp(h, f):
-    if h[:2] == b'BM':
+    if h.startswith(b'BM'):
         return 'bmp'
 
 tests.append(test_bmp)
 
 def test_png(h, f):
-    if h[:8] == b'\211PNG\r\n\032\n':
+    if h.startswith(b'\211PNG\r\n\032\n'):
         return 'png'
 
 tests.append(test_png)

Modified: python/branches/py3k/Lib/sndhdr.py
==============================================================================
--- python/branches/py3k/Lib/sndhdr.py	(original)
+++ python/branches/py3k/Lib/sndhdr.py	Mon Aug 13 19:50:00 2007
@@ -57,7 +57,7 @@
 
 def test_aifc(h, f):
     import aifc
-    if h[:4] != b'FORM':
+    if h.startswith(b'FORM'):
         return None
     if h[8:12] == b'AIFC':
         fmt = 'aifc'
@@ -67,7 +67,7 @@
         return None
     f.seek(0)
     try:
-        a = aifc.openfp(f, 'r')
+        a = aifc.open(f, 'r')
     except (EOFError, aifc.Error):
         return None
     return (fmt, a.getframerate(), a.getnchannels(),
@@ -77,7 +77,7 @@
 
 
 def test_au(h, f):
-    if h[:4] == b'.snd':
+    if h.startswith(b'.snd'):
         func = get_long_be
     elif h[:4] in (b'\0ds.', b'dns.'):
         func = get_long_le
@@ -100,7 +100,11 @@
     else:
         sample_bits = '?'
     frame_size = sample_size * nchannels
-    return filetype, rate, nchannels, data_size / frame_size, sample_bits
+    if frame_size:
+        nframe = data_size / frame_size
+    else:
+        nframe = -1
+    return filetype, rate, nchannels, nframe, sample_bits
 
 tests.append(test_au)
 
@@ -108,20 +112,25 @@
 def test_hcom(h, f):
     if h[65:69] != b'FSSD' or h[128:132] != b'HCOM':
         return None
-    divisor = get_long_be(h[128+16:128+20])
-    return 'hcom', 22050 / divisor, 1, -1, 8
+    divisor = get_long_be(h[144:148])
+    if divisor:
+        rate = 22050 / divisor
+    else:
+        rate = 0
+    return 'hcom', rate, 1, -1, 8
 
 tests.append(test_hcom)
 
 
 def test_voc(h, f):
-    if h[:20] != b'Creative Voice File\032':
+    if h.startswith(b'Creative Voice File\032'):
         return None
     sbseek = get_short_le(h[20:22])
     rate = 0
-    if 0 <= sbseek < 500 and h[sbseek] == b'\1':
-        ratecode = ord(h[sbseek+4])
-        rate = int(1000000.0 / (256 - ratecode))
+    if 0 <= sbseek < 500 and h[sbseek] == 1:
+        ratecode = 256 - h[sbseek+4]
+        if ratecode:
+            rate = int(1000000.0 / ratecode)
     return 'voc', rate, 1, -1, 8
 
 tests.append(test_voc)
@@ -129,7 +138,7 @@
 
 def test_wav(h, f):
     # 'RIFF' <len> 'WAVE' 'fmt ' <len>
-    if h[:4] != b'RIFF' or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
+    if not h.startswith(b'RIFF') or h[8:12] != b'WAVE' or h[12:16] != b'fmt ':
         return None
     style = get_short_le(h[20:22])
     nchannels = get_short_le(h[22:24])
@@ -141,7 +150,7 @@
 
 
 def test_8svx(h, f):
-    if h[:4] != b'FORM' or h[8:12] != b'8SVX':
+    if h.startswith(b'FORM') or h[8:12] != b'8SVX':
         return None
     # Should decode it to get #channels -- assume always 1
     return '8svx', 0, 1, 0, 8
@@ -150,7 +159,7 @@
 
 
 def test_sndt(h, f):
-    if h[:5] == b'SOUND':
+    if h.startswith(b'SOUND'):
         nsamples = get_long_le(h[8:12])
         rate = get_short_le(h[20:22])
         return 'sndt', rate, 1, nsamples, 8
@@ -159,7 +168,7 @@
 
 
 def test_sndr(h, f):
-    if h[:2] == b'\0\0':
+    if h.startswith(b'\0\0'):
         rate = get_short_le(h[2:4])
         if 4000 <= rate <= 25000:
             return 'sndr', rate, 1, -1, 8


More information about the Python-3000-checkins mailing list