cpython: 8fac90d0f4cd (original) (raw)
Mercurial > cpython
changeset 74467:8fac90d0f4cd 2.7
Issue #13589: Fix some serialization primitives in the aifc module. Patch by Oleg Plakhotnyuk. [#13589]
Antoine Pitrou solipsis@pitrou.net | |
---|---|
date | Tue, 17 Jan 2012 17:13:04 +0100 |
parents | f2ef537aaf61 |
children | ef1612a6a4f7 |
files | Lib/aifc.py Lib/test/test_aifc.py Misc/ACKS Misc/NEWS |
diffstat | 4 files changed, 72 insertions(+), 18 deletions(-)[+] [-] Lib/aifc.py 48 Lib/test/test_aifc.py 38 Misc/ACKS 1 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/aifc.py +++ b/Lib/aifc.py @@ -162,6 +162,12 @@ def _read_short(file): except struct.error: raise EOFError +def _read_ushort(file):
- try:
return struct.unpack('>H', file.read(2))[0][](#l1.9)
- except struct.error:
raise EOFError[](#l1.11)
+ def _read_string(file): length = ord(file.read(1)) if length == 0: @@ -194,13 +200,19 @@ def _read_float(f): # 10 bytes def _write_short(f, x): f.write(struct.pack('>h', x)) +def _write_ushort(f, x):
+ +def _write_ulong(f, x): f.write(struct.pack('>L', x)) def _write_string(f, s): if len(s) > 255: raise ValueError("string exceeds maximum pstring length")
@@ -218,7 +230,7 @@ def _write_float(f, x): lomant = 0 else: fmant, expon = math.frexp(x)
if expon > 16384 or fmant >= 1: # Infinity or NaN[](#l1.41)
if expon > 16384 or fmant >= 1 or fmant != fmant: # Infinity or NaN[](#l1.42) expon = sign|0x7FFF[](#l1.43) himant = 0[](#l1.44) lomant = 0[](#l1.45)
@@ -234,9 +246,9 @@ def _write_float(f, x): fmant = math.ldexp(fmant - fsmant, 32) fsmant = math.floor(fmant) lomant = long(fsmant)
from chunk import Chunk @@ -840,15 +852,15 @@ class Aifc_write: if self._aifc: self._file.write('AIFC') self._file.write('FVER')
_write_long(self._file, 4)[](#l1.63)
_write_long(self._file, self._version)[](#l1.64)
_write_ulong(self._file, 4)[](#l1.65)
_write_ulong(self._file, self._version)[](#l1.66) else:[](#l1.67) self._file.write('AIFF')[](#l1.68) self._file.write('COMM')[](#l1.69)
_write_long(self._file, commlength)[](#l1.70)
_write_ulong(self._file, commlength)[](#l1.71) _write_short(self._file, self._nchannels)[](#l1.72) self._nframes_pos = self._file.tell()[](#l1.73)
_write_long(self._file, self._nframes)[](#l1.74)
_write_ulong(self._file, self._nframes)[](#l1.75) _write_short(self._file, self._sampwidth * 8)[](#l1.76) _write_float(self._file, self._framerate)[](#l1.77) if self._aifc:[](#l1.78)
@@ -856,9 +868,9 @@ class Aifc_write: _write_string(self._file, self._compname) self._file.write('SSND') self._ssnd_length_pos = self._file.tell()
_write_long(self._file, self._datalength + 8)[](#l1.83)
_write_long(self._file, 0)[](#l1.84)
_write_long(self._file, 0)[](#l1.85)
_write_ulong(self._file, self._datalength + 8)[](#l1.86)
_write_ulong(self._file, 0)[](#l1.87)
_write_ulong(self._file, 0)[](#l1.88)
def _write_form_length(self, datalength): if self._aifc: @@ -869,8 +881,8 @@ class Aifc_write: else: commlength = 18 verslength = 0
_write_long(self._file, 4 + verslength + self._marklength + \[](#l1.96)
8 + commlength + 16 + datalength)[](#l1.97)
_write_ulong(self._file, 4 + verslength + self._marklength + \[](#l1.98)
8 + commlength + 16 + datalength)[](#l1.99) return commlength[](#l1.100)
def _patchheader(self): @@ -888,9 +900,9 @@ class Aifc_write: self._file.seek(self._form_length_pos, 0) dummy = self._write_form_length(datalength) self._file.seek(self._nframes_pos, 0)
_write_long(self._file, self._nframeswritten)[](#l1.107)
_write_ulong(self._file, self._nframeswritten)[](#l1.108) self._file.seek(self._ssnd_length_pos, 0)[](#l1.109)
_write_long(self._file, datalength + 8)[](#l1.110)
_write_ulong(self._file, datalength + 8)[](#l1.111) self._file.seek(curpos, 0)[](#l1.112) self._nframes = self._nframeswritten[](#l1.113) self._datalength = datalength[](#l1.114)
@@ -905,13 +917,13 @@ class Aifc_write: length = length + len(name) + 1 + 6 if len(name) & 1 == 0: length = length + 1
_write_long(self._file, length)[](#l1.119)
_write_ulong(self._file, length)[](#l1.120) self._marklength = length + 8[](#l1.121) _write_short(self._file, len(self._markers))[](#l1.122) for marker in self._markers:[](#l1.123) id, pos, name = marker[](#l1.124) _write_short(self._file, id)[](#l1.125)
_write_long(self._file, pos)[](#l1.126)
_write_ulong(self._file, pos)[](#l1.127) _write_string(self._file, name)[](#l1.128)
--- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@ -1,6 +1,7 @@ from test.test_support import findfile, run_unittest, TESTFN import unittest import os +import io import aifc @@ -107,8 +108,45 @@ class AIFCTest(unittest.TestCase): self.assertEqual(testfile.closed, True) +class AIFCLowLevelTest(unittest.TestCase): +
- def test_read_written(self):
def read_written(self, what):[](#l2.18)
f = io.BytesIO()[](#l2.19)
getattr(aifc, '_write_' + what)(f, x)[](#l2.20)
f.seek(0)[](#l2.21)
return getattr(aifc, '_read_' + what)(f)[](#l2.22)
for x in (-1, 0, 0.1, 1):[](#l2.23)
self.assertEqual(read_written(x, 'float'), x)[](#l2.24)
for x in (float('NaN'), float('Inf')):[](#l2.25)
self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL)[](#l2.26)
for x in (b'', b'foo', b'a' * 255):[](#l2.27)
self.assertEqual(read_written(x, 'string'), x)[](#l2.28)
for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF):[](#l2.29)
self.assertEqual(read_written(x, 'long'), x)[](#l2.30)
for x in (0, 1, 0xFFFFFFFF):[](#l2.31)
self.assertEqual(read_written(x, 'ulong'), x)[](#l2.32)
for x in (-0x7FFF, -1, 0, 1, 0x7FFF):[](#l2.33)
self.assertEqual(read_written(x, 'short'), x)[](#l2.34)
for x in (0, 1, 0xFFFF):[](#l2.35)
self.assertEqual(read_written(x, 'ushort'), x)[](#l2.36)
- def test_read_raises(self):
f = io.BytesIO(b'\x00')[](#l2.39)
self.assertRaises(EOFError, aifc._read_ulong, f)[](#l2.40)
self.assertRaises(EOFError, aifc._read_long, f)[](#l2.41)
self.assertRaises(EOFError, aifc._read_ushort, f)[](#l2.42)
self.assertRaises(EOFError, aifc._read_short, f)[](#l2.43)
- def test_write_long_string_raises(self):
f = io.BytesIO()[](#l2.46)
with self.assertRaises(ValueError):[](#l2.47)
aifc._write_string(f, b'too long' * 255)[](#l2.48)
+ + def test_main(): run_unittest(AIFCTest)
--- a/Misc/ACKS +++ b/Misc/ACKS @@ -662,6 +662,7 @@ Zach Pincus Michael Piotrowski Antoine Pitrou Jean-François Piéronne +Oleg Plakhotnyuk Guilherme Polo Michael Pomraning Iustin Pop
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -89,6 +89,9 @@ Core and Builtins Library ------- +- Issue #13589: Fix some serialization primitives in the aifc module.