1- from test .support import findfile , run_unittest , TESTFN
1+ from test .support import findfile , run_unittest , TESTFN , captured_stdout , unlink
22import unittest
33import os
44import io
5+ import struct
56
67import 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'\x17 t\x17 t"\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,148 @@ 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 captured_stdout () as s :
211+ f = aifc .open (io .BytesIO (b ))
212+ self .assertEqual (
213+ s .getvalue (),
214+ 'Warning: MARK chunk contains only 0 markers instead of 1\n ' )
215+ self .assertEqual (f .getmarkers (), None )
216+
217+ def test_read_comm_kludge_compname_even (self ):
218+ b = b'FORM' + struct .pack ('>L' , 4 ) + b'AIFC'
219+ b += b'COMM' + struct .pack ('>LhlhhLL' , 18 , 0 , 0 , 0 , 0 , 0 , 0 )
220+ b += b'NONE' + struct .pack ('B' , 4 ) + b'even' + b'\x00 '
221+ b += b'SSND' + struct .pack ('>L' , 8 ) + b'\x00 ' * 8
222+ with captured_stdout () as s :
223+ f = aifc .open (io .BytesIO (b ))
224+ self .assertEqual (s .getvalue (), 'Warning: bad COMM chunk size\n ' )
225+ self .assertEqual (f .getcompname (), b'even' )
226+
227+ def test_read_comm_kludge_compname_odd (self ):
228+ b = b'FORM' + struct .pack ('>L' , 4 ) + b'AIFC'
229+ b += b'COMM' + struct .pack ('>LhlhhLL' , 18 , 0 , 0 , 0 , 0 , 0 , 0 )
230+ b += b'NONE' + struct .pack ('B' , 3 ) + b'odd'
231+ b += b'SSND' + struct .pack ('>L' , 8 ) + b'\x00 ' * 8
232+ with captured_stdout () as s :
233+ f = aifc .open (io .BytesIO (b ))
234+ self .assertEqual (s .getvalue (), 'Warning: bad COMM chunk size\n ' )
235+ self .assertEqual (f .getcompname (), b'odd' )
236+
237+ def test_write_params_raises (self ):
238+ fout = aifc .open (io .BytesIO (), 'wb' )
239+ wrong_params = (0 , 0 , 0 , 0 , b'WRNG' , '' )
240+ self .assertRaises (aifc .Error , fout .setparams , wrong_params )
241+ self .assertRaises (aifc .Error , fout .getparams )
242+ self .assertRaises (aifc .Error , fout .setnchannels , 0 )
243+ self .assertRaises (aifc .Error , fout .getnchannels )
244+ self .assertRaises (aifc .Error , fout .setsampwidth , 0 )
245+ self .assertRaises (aifc .Error , fout .getsampwidth )
246+ self .assertRaises (aifc .Error , fout .setframerate , 0 )
247+ self .assertRaises (aifc .Error , fout .getframerate )
248+ self .assertRaises (aifc .Error , fout .setcomptype , b'WRNG' , '' )
249+ fout .aiff ()
250+ fout .setnchannels (1 )
251+ fout .setsampwidth (1 )
252+ fout .setframerate (1 )
253+ fout .setnframes (1 )
254+ fout .writeframes (b'\x00 ' )
255+ self .assertRaises (aifc .Error , fout .setparams , (1 , 1 , 1 , 1 , 1 , 1 ))
256+ self .assertRaises (aifc .Error , fout .setnchannels , 1 )
257+ self .assertRaises (aifc .Error , fout .setsampwidth , 1 )
258+ self .assertRaises (aifc .Error , fout .setframerate , 1 )
259+ self .assertRaises (aifc .Error , fout .setnframes , 1 )
260+ self .assertRaises (aifc .Error , fout .setcomptype , b'NONE' , '' )
261+ self .assertRaises (aifc .Error , fout .aiff )
262+ self .assertRaises (aifc .Error , fout .aifc )
263+
264+ def test_write_params_singles (self ):
265+ fout = aifc .open (io .BytesIO (), 'wb' )
266+ fout .aifc ()
267+ fout .setnchannels (1 )
268+ fout .setsampwidth (2 )
269+ fout .setframerate (3 )
270+ fout .setnframes (4 )
271+ fout .setcomptype (b'NONE' , b'name' )
272+ self .assertEqual (fout .getnchannels (), 1 )
273+ self .assertEqual (fout .getsampwidth (), 2 )
274+ self .assertEqual (fout .getframerate (), 3 )
275+ self .assertEqual (fout .getnframes (), 0 )
276+ self .assertEqual (fout .tell (), 0 )
277+ self .assertEqual (fout .getcomptype (), b'NONE' )
278+ self .assertEqual (fout .getcompname (), b'name' )
279+ fout .writeframes (b'\x00 ' * 4 * fout .getsampwidth () * fout .getnchannels ())
280+ self .assertEqual (fout .getnframes (), 4 )
281+ self .assertEqual (fout .tell (), 4 )
282+
283+ def test_write_params_bunch (self ):
284+ fout = aifc .open (io .BytesIO (), 'wb' )
285+ fout .aifc ()
286+ p = (1 , 2 , 3 , 4 , b'NONE' , b'name' )
287+ fout .setparams (p )
288+ self .assertEqual (fout .getparams (), p )
289+ fout .initfp (None )
290+
291+ def test_write_header_raises (self ):
292+ fout = aifc .open (io .BytesIO (), 'wb' )
293+ self .assertRaises (aifc .Error , fout .close )
294+ fout .setnchannels (1 )
295+ self .assertRaises (aifc .Error , fout .close )
296+ fout .setsampwidth (1 )
297+ self .assertRaises (aifc .Error , fout .close )
298+ fout .initfp (None )
299+
300+ def test_write_header_comptype_raises (self ):
301+ for comptype in (b'ULAW' , b'ulaw' , b'ALAW' , b'alaw' , b'G722' ):
302+ fout = aifc .open (io .BytesIO (), 'wb' )
303+ fout .setsampwidth (1 )
304+ fout .setcomptype (comptype , b'' )
305+ self .assertRaises (aifc .Error , fout .close )
306+ fout .initfp (None )
307+
308+ def test_write_markers_raises (self ):
309+ fout = aifc .open (io .BytesIO (), 'wb' )
310+ self .assertRaises (aifc .Error , fout .setmark , 0 , 0 , b'' )
311+ self .assertRaises (aifc .Error , fout .setmark , 1 , - 1 , b'' )
312+ self .assertRaises (aifc .Error , fout .setmark , 1 , 0 , None )
313+ self .assertRaises (aifc .Error , fout .getmark , 1 )
314+ fout .initfp (None )
315+
316+ def test_write_aiff_by_extension (self ):
317+ sampwidth = 2
318+ fout = self .fout = aifc .open (TESTFN + '.aiff' , 'wb' )
319+ fout .setparams ((1 , sampwidth , 1 , 1 , b'ULAW' , b'' ))
320+ frames = b'\x00 ' * fout .getnchannels () * sampwidth
321+ fout .writeframes (frames )
322+ fout .close ()
323+ f = self .f = aifc .open (TESTFN + '.aiff' , 'rb' )
324+ self .assertEqual (f .getcomptype (), b'NONE' )
325+ f .close ()
326+
182327
183328def test_main ():
184329 run_unittest (AIFCTest )
0 commit comments