Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7f3652e

Browse files
author
Victor Stinner
committed
Issue #8897: Fix sunau module, use bytes to write the header. Patch written by
Thomas Jollans.
1 parent 7eeb5b5 commit 7f3652e

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

Lib/sunau.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def initfp(self, file):
299299
self._nframeswritten = 0
300300
self._datawritten = 0
301301
self._datalength = 0
302-
self._info = ''
302+
self._info = b''
303303
self._comptype = 'ULAW' # default is U-law
304304

305305
def setnchannels(self, nchannels):

Lib/test/test_sunau.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from test.support import run_unittest, TESTFN
2+
import unittest
3+
import os
4+
5+
import sunau
6+
7+
nchannels = 2
8+
sampwidth = 2
9+
framerate = 8000
10+
nframes = 100
11+
12+
class SunAUTest(unittest.TestCase):
13+
14+
def setUp(self):
15+
self.f = None
16+
17+
def tearDown(self):
18+
if self.f is not None:
19+
self.f.close()
20+
try:
21+
os.remove(TESTFN)
22+
except OSError:
23+
pass
24+
25+
def test_lin(self):
26+
self.f = sunau.open(TESTFN, 'w')
27+
self.f.setnchannels(nchannels)
28+
self.f.setsampwidth(sampwidth)
29+
self.f.setframerate(framerate)
30+
self.f.setcomptype('NONE', 'not compressed')
31+
output = b'\xff\x00\x12\xcc' * (nframes * nchannels * sampwidth // 4)
32+
self.f.writeframes(output)
33+
self.f.close()
34+
35+
self.f = sunau.open(TESTFN, 'rb')
36+
self.assertEqual(nchannels, self.f.getnchannels())
37+
self.assertEqual(sampwidth, self.f.getsampwidth())
38+
self.assertEqual(framerate, self.f.getframerate())
39+
self.assertEqual(nframes, self.f.getnframes())
40+
self.assertEqual('NONE', self.f.getcomptype())
41+
self.assertEqual(self.f.readframes(nframes), output)
42+
self.f.close()
43+
44+
def test_ulaw(self):
45+
self.f = sunau.open(TESTFN, 'w')
46+
self.f.setnchannels(nchannels)
47+
self.f.setsampwidth(sampwidth)
48+
self.f.setframerate(framerate)
49+
self.f.setcomptype('ULAW', '')
50+
# u-law compression is lossy, therefore we can't expect non-zero data
51+
# to come back unchanged.
52+
output = b'\0' * nframes * nchannels * sampwidth
53+
self.f.writeframes(output)
54+
self.f.close()
55+
56+
self.f = sunau.open(TESTFN, 'rb')
57+
self.assertEqual(nchannels, self.f.getnchannels())
58+
self.assertEqual(sampwidth, self.f.getsampwidth())
59+
self.assertEqual(framerate, self.f.getframerate())
60+
self.assertEqual(nframes, self.f.getnframes())
61+
self.assertEqual('ULAW', self.f.getcomptype())
62+
self.assertEqual(self.f.readframes(nframes), output)
63+
self.f.close()
64+
65+
66+
def test_main():
67+
run_unittest(SunAUTest)
68+
69+
if __name__ == "__main__":
70+
unittest.main()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ Orjan Johansen
389389
Fredrik Johansson
390390
Gregory K. Johnson
391391
Simon Johnston
392+
Thomas Jollans
392393
Evan Jones
393394
Jeremy Jones
394395
Richard Jones

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ C-API
398398
Library
399399
-------
400400

401+
- Issue #8897: Fix sunau module, use bytes to write the header. Patch written
402+
by Thomas Jollans.
403+
401404
- Issue #8899: time.struct_time now has class and atribute docstrings.
402405

403406
- Issue #6470: Drop UNC prefix in FixTk.

0 commit comments

Comments
 (0)