|
7 | 7 | import sys |
8 | 8 | import struct |
9 | 9 | import aifc |
| 10 | +from test.test_support import check_warnings |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class AifcTest(audiotests.AudioWriteTests, |
@@ -216,47 +217,68 @@ def test_read_no_comm_chunk(self): |
216 | 217 |
|
217 | 218 | def test_read_no_ssnd_chunk(self): |
218 | 219 | b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
219 | | - b += b'COMM' + struct.pack('>LhlhhLL', 38, 0, 0, 0, 0, 0, 0) |
| 220 | + b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, 8, |
| 221 | + 0x4000 | 12, 11025<<18, 0) |
220 | 222 | b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00' |
221 | 223 | with self.assertRaisesRegexp(aifc.Error, 'COMM chunk and/or SSND chunk' |
222 | 224 | ' missing'): |
223 | 225 | aifc.open(io.BytesIO(b)) |
224 | 226 |
|
225 | 227 | def test_read_wrong_compression_type(self): |
226 | | - b = 'FORM' + struct.pack('>L', 4) + 'AIFC' |
227 | | - b += 'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0) |
228 | | - b += 'WRNG' + struct.pack('B', 0) |
| 228 | + b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 229 | + b += b'COMM' + struct.pack('>LhlhhLL', 23, 1, 0, 8, |
| 230 | + 0x4000 | 12, 11025<<18, 0) |
| 231 | + b += b'WRNG' + struct.pack('B', 0) |
229 | 232 | self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b)) |
230 | 233 |
|
| 234 | + def test_read_wrong_number_of_channels(self): |
| 235 | + for nchannels in 0, -1: |
| 236 | + b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 237 | + b += b'COMM' + struct.pack('>LhlhhLL', 38, nchannels, 0, 8, |
| 238 | + 0x4000 | 12, 11025<<18, 0) |
| 239 | + b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00' |
| 240 | + b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
| 241 | + with self.assertRaisesRegexp(aifc.Error, 'bad # of channels'): |
| 242 | + aifc.open(io.BytesIO(b)) |
| 243 | + |
| 244 | + def test_read_wrong_sample_width(self): |
| 245 | + for sampwidth in 0, -1: |
| 246 | + b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 247 | + b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, sampwidth, |
| 248 | + 0x4000 | 12, 11025<<18, 0) |
| 249 | + b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00' |
| 250 | + b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
| 251 | + with self.assertRaisesRegexp(aifc.Error, 'bad sample width'): |
| 252 | + aifc.open(io.BytesIO(b)) |
| 253 | + |
231 | 254 | def test_read_wrong_marks(self): |
232 | | - b = 'FORM' + struct.pack('>L', 4) + 'AIFF' |
233 | | - b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) |
234 | | - b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8 |
235 | | - b += 'MARK' + struct.pack('>LhB', 3, 1, 1) |
236 | | - with captured_stdout() as s: |
| 255 | + b = b'FORM' + struct.pack('>L', 4) + b'AIFF' |
| 256 | + b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8, |
| 257 | + 0x4000 | 12, 11025<<18, 0) |
| 258 | + b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
| 259 | + b += b'MARK' + struct.pack('>LhB', 3, 1, 1) |
| 260 | + with check_warnings(('MARK chunk contains only 0 markers instead of 1', UserWarning)): |
237 | 261 | f = aifc.open(io.BytesIO(b)) |
238 | | - self.assertEqual(s.getvalue(), 'Warning: MARK chunk contains ' |
239 | | - 'only 0 markers instead of 1\n') |
240 | | - self.assertEqual(f.getmarkers(), None) |
| 262 | + self.assertEqual(f.getmarkers(), None) |
241 | 263 |
|
242 | 264 | def test_read_comm_kludge_compname_even(self): |
243 | | - b = 'FORM' + struct.pack('>L', 4) + 'AIFC' |
244 | | - b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) |
245 | | - b += 'NONE' + struct.pack('B', 4) + 'even' + '\x00' |
246 | | - b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8 |
247 | | - with captured_stdout() as s: |
| 265 | + b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 266 | + b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8, |
| 267 | + 0x4000 | 12, 11025<<18, 0) |
| 268 | + b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00' |
| 269 | + b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
| 270 | + with check_warnings(('bad COMM chunk size', UserWarning)): |
248 | 271 | f = aifc.open(io.BytesIO(b)) |
249 | | - self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n') |
250 | 272 | self.assertEqual(f.getcompname(), 'even') |
251 | 273 |
|
252 | 274 | def test_read_comm_kludge_compname_odd(self): |
253 | | - b = 'FORM' + struct.pack('>L', 4) + 'AIFC' |
254 | | - b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) |
255 | | - b += 'NONE' + struct.pack('B', 3) + 'odd' |
256 | | - b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8 |
257 | | - with captured_stdout() as s: |
| 275 | + b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 276 | + b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8, |
| 277 | + 0x4000 | 12, 11025<<18, 0) |
| 278 | + b += b'NONE' + struct.pack('B', 3) + b'odd' |
| 279 | + b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
| 280 | + with check_warnings(('bad COMM chunk size', UserWarning)): |
258 | 281 | f = aifc.open(io.BytesIO(b)) |
259 | | - self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n') |
260 | 282 | self.assertEqual(f.getcompname(), 'odd') |
261 | 283 |
|
262 | 284 | def test_write_params_raises(self): |
|
0 commit comments