Hi,
As imghdr, sndhdr tests were strill based on Unicode strings instead of bytes.
Attached patch should fix the module. I'm very, I was unable to test it.
Note: I replaced aifc.openfp with aifc.open since it's the new public
function.
sndhdr requires some cleanup: it doesn't check division by zero in functions
test_hcom and test_voc. I think that division by zero means that the file is
invalid. I didn't want to fix these bugs in the same patch. So first I'm
waiting your comments about this one :-)
Victor Stinner
http://hachoir.org/
Index: Lib/sndhdr.py
===================================================================
--- Lib/sndhdr.py (révision 56910)
+++ Lib/sndhdr.py (copie de travail)
@@ -57,17 +57,17 @@
def test_aifc(h, f):
import aifc
- if h[:4] != 'FORM':
+ if h[:4] != b'FORM':
return None
- if h[8:12] == 'AIFC':
+ if h[8:12] == b'AIFC':
fmt = 'aifc'
- elif h[8:12] == 'AIFF':
+ elif h[8:12] == b'AIFF':
fmt = 'aiff'
else:
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,9 +77,9 @@
def test_au(h, f):
- if h[:4] == '.snd':
+ if h[:4] == b'.snd':
f = get_long_be
- elif h[:4] in ('\0ds.', 'dns.'):
+ elif h[:4] in (b'\0ds.', b'dns.'):
f = get_long_le
else:
return None
@@ -106,7 +106,7 @@
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
divisor = get_long_be(h[128+16:128+20])
return 'hcom', 22050/divisor, 1, -1, 8
@@ -115,12 +115,12 @@
def test_voc(h, f):
- if h[:20] != 'Creative Voice File\032':
+ if h[:20] != b'Creative Voice File\032':
return None
sbseek = get_short_le(h[20:22])
rate = 0
- if 0 <= sbseek < 500 and h[sbseek] == '\1':
- ratecode = ord(h[sbseek+4])
+ if 0 <= sbseek < 500 and h[sbseek] == 1:
+ ratecode = h[sbseek+4]
rate = int(1000000.0 / (256 - ratecode))
return 'voc', rate, 1, -1, 8
@@ -129,7 +129,7 @@
def test_wav(h, f):
# '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
style = get_short_le(h[20:22])
nchannels = get_short_le(h[22:24])
@@ -141,7 +141,7 @@
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
# Should decode it to get #channels -- assume always 1
return '8svx', 0, 1, 0, 8
@@ -150,7 +150,7 @@
def test_sndt(h, f):
- if h[:5] == 'SOUND':
+ if h[:5] == b'SOUND':
nsamples = get_long_le(h[8:12])
rate = get_short_le(h[20:22])
return 'sndt', rate, 1, nsamples, 8
@@ -159,7 +159,7 @@
def test_sndr(h, f):
- if h[:2] == '\0\0':
+ if h[:2] == b'\0\0':
rate = get_short_le(h[2:4])
if 4000 <= rate <= 25000:
return 'sndr', rate, 1, -1, 8
@@ -172,16 +172,16 @@
#---------------------------------------------#
def get_long_be(s):
- return (ord(s[0])<<24) | (ord(s[1])<<16) | (ord(s[2])<<8) | ord(s[3])
+ return (s[0]<<24) | (s[1]<<16) | (s[2]<<8) | s[3]
def get_long_le(s):
- return (ord(s[3])<<24) | (ord(s[2])<<16) | (ord(s[1])<<8) | ord(s[0])
+ return (s[3]<<24) | (s[2]<<16) | (s[1]<<8) | s[0]
def get_short_be(s):
- return (ord(s[0])<<8) | ord(s[1])
+ return (s[0]<<8) | s[1]
def get_short_le(s):
- return (ord(s[1])<<8) | ord(s[0])
+ return (s[1]<<8) | s[0]
#--------------------#
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe:
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com