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

Skip to content

Commit 48d578c

Browse files
committed
#13394: add more tests for the aifc module and use warnings.warn instead of print. Patch by Oleg Plakhotnyuk.
2 parents c1f5d8a + 8576953 commit 48d578c

3 files changed

Lines changed: 157 additions & 15 deletions

File tree

Lib/aifc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136

137137
import struct
138138
import builtins
139+
import warnings
139140

140141
__all__ = ["Error", "open", "openfp"]
141142

@@ -440,7 +441,7 @@ def _read_comm_chunk(self, chunk):
440441
kludge = 0
441442
if chunk.chunksize == 18:
442443
kludge = 1
443-
print('Warning: bad COMM chunk size')
444+
warnings.warn('Warning: bad COMM chunk size')
444445
chunk.chunksize = 23
445446
#DEBUG end
446447
self._comptype = chunk.read(4)
@@ -484,11 +485,10 @@ def _readmark(self, chunk):
484485
# a position 0 and name ''
485486
self._markers.append((id, pos, name))
486487
except EOFError:
487-
print('Warning: MARK chunk contains only', end=' ')
488-
print(len(self._markers), end=' ')
489-
if len(self._markers) == 1: print('marker', end=' ')
490-
else: print('markers', end=' ')
491-
print('instead of', nmarkers)
488+
w = ('Warning: MARK chunk contains only %s marker%s instead of %s' %
489+
(len(self._markers), '' if len(self._markers) == 1 else 's',
490+
nmarkers))
491+
warnings.warn(w)
492492

493493
class Aifc_write:
494494
# Variables used in this class:

Lib/test/test_aifc.py

Lines changed: 149 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from test.support import findfile, run_unittest, TESTFN
1+
from test.support import findfile, run_unittest, TESTFN, unlink
22
import unittest
33
import os
44
import io
5+
import struct
56

67
import aifc
78

@@ -20,10 +21,8 @@ def tearDown(self):
2021
self.fout.close()
2122
except (aifc.Error, AttributeError):
2223
pass
23-
try:
24-
os.remove(TESTFN)
25-
except OSError:
26-
pass
24+
unlink(TESTFN)
25+
unlink(TESTFN + '.aiff')
2726

2827
def test_skipunknown(self):
2928
#Issue 2245
@@ -32,6 +31,7 @@ def test_skipunknown(self):
3231

3332
def test_params(self):
3433
f = self.f = aifc.open(self.sndfilepath)
34+
self.assertEqual(f.getfp().name, self.sndfilepath)
3535
self.assertEqual(f.getnchannels(), 2)
3636
self.assertEqual(f.getsampwidth(), 2)
3737
self.assertEqual(f.getframerate(), 48000)
@@ -45,6 +45,7 @@ def test_params(self):
4545

4646
def test_read(self):
4747
f = self.f = aifc.open(self.sndfilepath)
48+
self.assertEqual(f.readframes(0), b'')
4849
self.assertEqual(f.tell(), 0)
4950
self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
5051
f.rewind()
@@ -58,6 +59,10 @@ def test_read(self):
5859
self.assertEqual(f.readframes(2), b'\x17t\x17t"\xad"\xad')
5960
f.setpos(pos0)
6061
self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
62+
with self.assertRaises(aifc.Error):
63+
f.setpos(-1)
64+
with self.assertRaises(aifc.Error):
65+
f.setpos(f.getnframes() + 1)
6166

6267
def test_write(self):
6368
f = self.f = aifc.open(self.sndfilepath)
@@ -92,8 +97,6 @@ def test_compress(self):
9297
self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3])
9398
self.assertEqual(fout.getcomptype(), b'ULAW')
9499
self.assertEqual(fout.getcompname(), b'foo')
95-
# XXX: this test fails, not sure if it should succeed or not
96-
# self.assertEqual(f.readframes(5), fout.readframes(5))
97100

98101
def test_close(self):
99102
class Wrapfile(object):
@@ -112,7 +115,7 @@ def __getattr__(self, attr): return getattr(self.file, attr)
112115

113116
def test_write_header_comptype_sampwidth(self):
114117
for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
115-
fout = self.fout = aifc.open(io.BytesIO(), 'wb')
118+
fout = aifc.open(io.BytesIO(), 'wb')
116119
fout.setnchannels(1)
117120
fout.setframerate(1)
118121
fout.setcomptype(comptype, b'')
@@ -121,7 +124,7 @@ def test_write_header_comptype_sampwidth(self):
121124
fout.initfp(None)
122125

123126
def test_write_markers_values(self):
124-
fout = self.fout = aifc.open(io.BytesIO(), 'wb')
127+
fout = aifc.open(io.BytesIO(), 'wb')
125128
self.assertEqual(fout.getmarkers(), None)
126129
fout.setmark(1, 0, b'foo1')
127130
fout.setmark(1, 1, b'foo2')
@@ -179,6 +182,143 @@ def test_write_long_string_raises(self):
179182
with self.assertRaises(ValueError):
180183
aifc._write_string(f, b'too long' * 255)
181184

185+
def test_wrong_open_mode(self):
186+
with self.assertRaises(aifc.Error):
187+
aifc.open(TESTFN, 'wrong_mode')
188+
189+
def test_read_wrong_form(self):
190+
b1 = io.BytesIO(b'WRNG' + struct.pack('>L', 0))
191+
b2 = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'WRNG')
192+
self.assertRaises(aifc.Error, aifc.open, b1)
193+
self.assertRaises(aifc.Error, aifc.open, b2)
194+
195+
def test_read_no_comm_chunk(self):
196+
b = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'AIFF')
197+
self.assertRaises(aifc.Error, aifc.open, b)
198+
199+
def test_read_wrong_compression_type(self):
200+
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
201+
b += b'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0)
202+
b += b'WRNG' + struct.pack('B', 0)
203+
self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b))
204+
205+
def test_read_wrong_marks(self):
206+
b = b'FORM' + struct.pack('>L', 4) + b'AIFF'
207+
b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
208+
b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
209+
b += b'MARK' + struct.pack('>LhB', 3, 1, 1)
210+
with self.assertWarns(UserWarning):
211+
f = aifc.open(io.BytesIO(b))
212+
self.assertEqual(f.getmarkers(), None)
213+
214+
def test_read_comm_kludge_compname_even(self):
215+
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
216+
b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
217+
b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00'
218+
b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
219+
with self.assertWarns(UserWarning):
220+
f = aifc.open(io.BytesIO(b))
221+
self.assertEqual(f.getcompname(), b'even')
222+
223+
def test_read_comm_kludge_compname_odd(self):
224+
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
225+
b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
226+
b += b'NONE' + struct.pack('B', 3) + b'odd'
227+
b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
228+
with self.assertWarns(UserWarning):
229+
f = aifc.open(io.BytesIO(b))
230+
self.assertEqual(f.getcompname(), b'odd')
231+
232+
def test_write_params_raises(self):
233+
fout = aifc.open(io.BytesIO(), 'wb')
234+
wrong_params = (0, 0, 0, 0, b'WRNG', '')
235+
self.assertRaises(aifc.Error, fout.setparams, wrong_params)
236+
self.assertRaises(aifc.Error, fout.getparams)
237+
self.assertRaises(aifc.Error, fout.setnchannels, 0)
238+
self.assertRaises(aifc.Error, fout.getnchannels)
239+
self.assertRaises(aifc.Error, fout.setsampwidth, 0)
240+
self.assertRaises(aifc.Error, fout.getsampwidth)
241+
self.assertRaises(aifc.Error, fout.setframerate, 0)
242+
self.assertRaises(aifc.Error, fout.getframerate)
243+
self.assertRaises(aifc.Error, fout.setcomptype, b'WRNG', '')
244+
fout.aiff()
245+
fout.setnchannels(1)
246+
fout.setsampwidth(1)
247+
fout.setframerate(1)
248+
fout.setnframes(1)
249+
fout.writeframes(b'\x00')
250+
self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1))
251+
self.assertRaises(aifc.Error, fout.setnchannels, 1)
252+
self.assertRaises(aifc.Error, fout.setsampwidth, 1)
253+
self.assertRaises(aifc.Error, fout.setframerate, 1)
254+
self.assertRaises(aifc.Error, fout.setnframes, 1)
255+
self.assertRaises(aifc.Error, fout.setcomptype, b'NONE', '')
256+
self.assertRaises(aifc.Error, fout.aiff)
257+
self.assertRaises(aifc.Error, fout.aifc)
258+
259+
def test_write_params_singles(self):
260+
fout = aifc.open(io.BytesIO(), 'wb')
261+
fout.aifc()
262+
fout.setnchannels(1)
263+
fout.setsampwidth(2)
264+
fout.setframerate(3)
265+
fout.setnframes(4)
266+
fout.setcomptype(b'NONE', b'name')
267+
self.assertEqual(fout.getnchannels(), 1)
268+
self.assertEqual(fout.getsampwidth(), 2)
269+
self.assertEqual(fout.getframerate(), 3)
270+
self.assertEqual(fout.getnframes(), 0)
271+
self.assertEqual(fout.tell(), 0)
272+
self.assertEqual(fout.getcomptype(), b'NONE')
273+
self.assertEqual(fout.getcompname(), b'name')
274+
fout.writeframes(b'\x00' * 4 * fout.getsampwidth() * fout.getnchannels())
275+
self.assertEqual(fout.getnframes(), 4)
276+
self.assertEqual(fout.tell(), 4)
277+
278+
def test_write_params_bunch(self):
279+
fout = aifc.open(io.BytesIO(), 'wb')
280+
fout.aifc()
281+
p = (1, 2, 3, 4, b'NONE', b'name')
282+
fout.setparams(p)
283+
self.assertEqual(fout.getparams(), p)
284+
fout.initfp(None)
285+
286+
def test_write_header_raises(self):
287+
fout = aifc.open(io.BytesIO(), 'wb')
288+
self.assertRaises(aifc.Error, fout.close)
289+
fout.setnchannels(1)
290+
self.assertRaises(aifc.Error, fout.close)
291+
fout.setsampwidth(1)
292+
self.assertRaises(aifc.Error, fout.close)
293+
fout.initfp(None)
294+
295+
def test_write_header_comptype_raises(self):
296+
for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
297+
fout = aifc.open(io.BytesIO(), 'wb')
298+
fout.setsampwidth(1)
299+
fout.setcomptype(comptype, b'')
300+
self.assertRaises(aifc.Error, fout.close)
301+
fout.initfp(None)
302+
303+
def test_write_markers_raises(self):
304+
fout = aifc.open(io.BytesIO(), 'wb')
305+
self.assertRaises(aifc.Error, fout.setmark, 0, 0, b'')
306+
self.assertRaises(aifc.Error, fout.setmark, 1, -1, b'')
307+
self.assertRaises(aifc.Error, fout.setmark, 1, 0, None)
308+
self.assertRaises(aifc.Error, fout.getmark, 1)
309+
fout.initfp(None)
310+
311+
def test_write_aiff_by_extension(self):
312+
sampwidth = 2
313+
fout = self.fout = aifc.open(TESTFN + '.aiff', 'wb')
314+
fout.setparams((1, sampwidth, 1, 1, b'ULAW', b''))
315+
frames = b'\x00' * fout.getnchannels() * sampwidth
316+
fout.writeframes(frames)
317+
fout.close()
318+
f = self.f = aifc.open(TESTFN + '.aiff', 'rb')
319+
self.assertEqual(f.getcomptype(), b'NONE')
320+
f.close()
321+
182322

183323
def test_main():
184324
run_unittest(AIFCTest)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #13394: the aifc module now uses warnings.warn() to signal warnings.
28+
2729
- Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under
2830
Windows when the child process has already exited.
2931

0 commit comments

Comments
 (0)