# HG changeset patch # User Thomas Jollans # Date 1275907863 -7200 # Branch py3k # Node ID 84a122fc7d45dc8f9d1e6355be123d8be1577e90 # Parent 263b0f92067efa417d79fa1e8a62f0b41dc64435 bring aifc in line with wave and sunau: use unicode strings. diff -r 263b0f92067e -r 84a122fc7d45 Lib/aifc.py --- a/Lib/aifc.py Mon Jun 07 19:08:31 2010 +0200 +++ b/Lib/aifc.py Mon Jun 07 12:51:03 2010 +0200 @@ -170,7 +170,7 @@ data = file.read(length) if length & 1 == 0: dummy = file.read(1) - return data + return data.decode('ascii') _HUGE_VAL = 1.79769313486231e+308 # See @@ -198,6 +198,8 @@ f.write(struct.pack('>L', x)) def _write_string(f, s): + if isinstance(s, str): + s = s.encode('ascii') if len(s) > 255: raise ValueError("string exceeds maximum pstring length") f.write(struct.pack('b', len(s))) @@ -431,7 +433,7 @@ print('Warning: bad COMM chunk size') chunk.chunksize = 23 #DEBUG end - self._comptype = chunk.read(4) + self._comptype = chunk.read(4).decode('ascii').upper() #DEBUG start if kludge: length = ord(chunk.file.read(1)) @@ -441,21 +443,21 @@ chunk.file.seek(-1, 1) #DEBUG end self._compname = _read_string(chunk) - if self._comptype != b'NONE': - if self._comptype == b'G722': + if self._comptype != 'NONE': + if self._comptype == 'G722': self._convert = self._adpcm2lin self._framesize = self._framesize // 4 - elif self._comptype in (b'ulaw', b'ULAW'): + elif self._comptype == 'ULAW': self._convert = self._ulaw2lin self._framesize = self._framesize // 2 - elif self._comptype in (b'alaw', b'ALAW'): + elif self._comptype == 'ALAW': self._convert = self._alaw2lin self._framesize = self._framesize // 2 else: raise Error('unsupported compression type') else: - self._comptype = b'NONE' - self._compname = b'not compressed' + self._comptype = 'NONE' + self._compname = 'not compressed' def _readmark(self, chunk): nmarkers = _read_short(chunk) @@ -524,8 +526,8 @@ def initfp(self, file): self._file = file self._version = _AIFC_version - self._comptype = b'NONE' - self._compname = b'not compressed' + self._comptype = 'NONE' + self._compname = 'not compressed' self._convert = None self._nchannels = 0 self._sampwidth = 0 @@ -600,10 +602,15 @@ return self._nframeswritten def setcomptype(self, comptype, compname): + # accept byte-strings for limited compatibility with 3.0/3.1 + if isinstance(comptype, bytes): + comptype = comptype.decode('ascii') + if isinstance(compname, bytes): + compname = compname.decode('ascii') + comptype = comptype.upper() # allow 'ulaw', 'alaw' if self._nframeswritten: raise Error('cannot change parameters after starting to write') - if comptype not in (b'NONE', b'ulaw', b'ULAW', - b'alaw', b'ALAW', b'G722'): + if comptype not in ('NONE', 'ULAW', 'ALAW', 'G722'): raise Error('unsupported compression type') self._comptype = comptype self._compname = compname @@ -623,8 +630,7 @@ nchannels, sampwidth, framerate, nframes, comptype, compname = params if self._nframeswritten: raise Error('cannot change parameters after starting to write') - if comptype not in (b'NONE', b'ulaw', b'ULAW', - b'alaw', b'ALAW', b'G722'): + if comptype not in ('NONE', 'ULAW', 'ALAW', 'G722'): raise Error('unsupported compression type') self.setnchannels(nchannels) self.setsampwidth(sampwidth) @@ -716,13 +722,13 @@ def _ensure_header_written(self, datasize): if not self._nframeswritten: - if self._comptype in (b'ULAW', b'ALAW'): + if self._comptype in ('ULAW', 'ALAW'): if not self._sampwidth: self._sampwidth = 2 if self._sampwidth != 2: raise Error('sample width must be 2 when compressing ' - 'with ulaw/ULAW or alaw/ALAW') - if self._comptype == b'G722': + 'with ULAW or ALAW') + if self._comptype == 'G722': if not self._sampwidth: self._sampwidth = 2 if self._sampwidth != 2: @@ -737,17 +743,17 @@ self._write_header(datasize) def _init_compression(self): - if self._comptype == b'G722': + if self._comptype == 'G722': self._convert = self._lin2adpcm - elif self._comptype in (b'ulaw', b'ULAW'): + elif self._comptype == 'ULAW': self._convert = self._lin2ulaw - elif self._comptype in (b'alaw', b'ALAW'): + elif self._comptype == 'ALAW': self._convert = self._lin2alaw else: raise Error('unsupported compression type') def _write_header(self, initlength): - if self._aifc and self._comptype != b'NONE': + if self._aifc and self._comptype != 'NONE': self._init_compression() self._file.write(b'FORM') if not self._nframes: @@ -756,11 +762,11 @@ if self._datalength & 1: self._datalength = self._datalength + 1 if self._aifc: - if self._comptype in (b'ulaw', b'ULAW', b'alaw', b'ALAW'): + if self._comptype in ('ULAW', 'ALAW'): self._datalength = self._datalength // 2 if self._datalength & 1: self._datalength = self._datalength + 1 - elif self._comptype == b'G722': + elif self._comptype == 'G722': self._datalength = (self._datalength + 3) // 4 if self._datalength & 1: self._datalength = self._datalength + 1 @@ -781,7 +787,7 @@ _write_short(self._file, self._sampwidth * 8) _write_float(self._file, self._framerate) if self._aifc: - self._file.write(self._comptype) + self._file.write(self._comptype.encode('ascii')) _write_string(self._file, self._compname) self._file.write(b'SSND') self._ssnd_length_pos = self._file.tell() diff -r 263b0f92067e -r 84a122fc7d45 Lib/test/test_aifc.py --- a/Lib/test/test_aifc.py Mon Jun 07 19:08:31 2010 +0200 +++ b/Lib/test/test_aifc.py Mon Jun 07 12:51:03 2010 +0200 @@ -35,11 +35,11 @@ self.assertEqual(f.getsampwidth(), 2) self.assertEqual(f.getframerate(), 48000) self.assertEqual(f.getnframes(), 14400) - self.assertEqual(f.getcomptype(), b'NONE') - self.assertEqual(f.getcompname(), b'not compressed') + self.assertEqual(f.getcomptype(), 'NONE') + self.assertEqual(f.getcompname(), 'not compressed') self.assertEqual( f.getparams(), - (2, 2, 48000, 14400, b'NONE', b'not compressed'), + (2, 2, 48000, 14400, 'NONE', 'not compressed'), ) def test_read(self): @@ -78,7 +78,10 @@ fout.setnchannels(f.getnchannels()) fout.setsampwidth(f.getsampwidth()) fout.setframerate(f.getframerate()) + # bytestrings are accepted, but decoded. fout.setcomptype(b'ULAW', b'foo') + self.assertEqual(fout.getcomptype(), 'ULAW') + self.assertEqual(fout.getcompname(), 'foo') for frame in range(f.getnframes()): fout.writeframes(f.readframes(1)) fout.close() @@ -89,9 +92,9 @@ fout = self.fout = aifc.open(TESTFN, 'rb') f.rewind() self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3]) - self.assertEqual(fout.getcomptype(), b'ULAW') - self.assertEqual(fout.getcompname(), b'foo') - # XXX: this test fails, not sure if it should succeed or not + self.assertEqual(fout.getcomptype(), 'ULAW') + self.assertEqual(fout.getcompname(), 'foo') + # This test fails due to u-law being a lossy compression format # self.assertEqual(f.readframes(5), fout.readframes(5)) def test_close(self):