From: Ezio Melotti Date: Mon, 12 Mar 2012 21:57:18 +0000 (+0200) Subject: #13394: add more tests for the aifc module and use warnings.warn instead of print... X-Git-Tag: v3.3.0a2~234^2~8^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=48d578c02a98dfd339afc1f8c86dafde31a7e994;p=thirdparty%2FPython%2Fcpython.git #13394: add more tests for the aifc module and use warnings.warn instead of print. Patch by Oleg Plakhotnyuk. --- 48d578c02a98dfd339afc1f8c86dafde31a7e994 diff --cc Lib/aifc.py index 775f39c74fb5,775f39c74fb5..ec4f8223aa40 --- a/Lib/aifc.py +++ b/Lib/aifc.py @@@ -136,6 -136,6 +136,7 @@@ writeframesraw import struct import builtins ++import warnings __all__ = ["Error", "open", "openfp"] @@@ -440,7 -440,7 +441,7 @@@ class Aifc_read kludge = 0 if chunk.chunksize == 18: kludge = 1 -- print('Warning: bad COMM chunk size') ++ warnings.warn('Warning: bad COMM chunk size') chunk.chunksize = 23 #DEBUG end self._comptype = chunk.read(4) @@@ -484,11 -484,11 +485,10 @@@ # a position 0 and name '' self._markers.append((id, pos, name)) except EOFError: -- print('Warning: MARK chunk contains only', end=' ') -- print(len(self._markers), end=' ') -- if len(self._markers) == 1: print('marker', end=' ') -- else: print('markers', end=' ') -- print('instead of', nmarkers) ++ w = ('Warning: MARK chunk contains only %s marker%s instead of %s' % ++ (len(self._markers), '' if len(self._markers) == 1 else 's', ++ nmarkers)) ++ warnings.warn(w) class Aifc_write: # Variables used in this class: diff --cc Lib/test/test_aifc.py index 236f9b661076,ee4ad6b0a32d..ad6f6105ecde --- a/Lib/test/test_aifc.py +++ b/Lib/test/test_aifc.py @@@ -1,4 -1,4 +1,4 @@@ - from test.support import findfile, run_unittest, TESTFN -from test.support import findfile, run_unittest, TESTFN, captured_stdout, unlink ++from test.support import findfile, run_unittest, TESTFN, unlink import unittest import os import io @@@ -179,6 -182,148 +182,143 @@@ class AIFCLowLevelTest(unittest.TestCas with self.assertRaises(ValueError): aifc._write_string(f, b'too long' * 255) + def test_wrong_open_mode(self): + with self.assertRaises(aifc.Error): + aifc.open(TESTFN, 'wrong_mode') + + def test_read_wrong_form(self): + b1 = io.BytesIO(b'WRNG' + struct.pack('>L', 0)) + b2 = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'WRNG') + self.assertRaises(aifc.Error, aifc.open, b1) + self.assertRaises(aifc.Error, aifc.open, b2) + + def test_read_no_comm_chunk(self): + b = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'AIFF') + self.assertRaises(aifc.Error, aifc.open, b) + + def test_read_wrong_compression_type(self): + b = b'FORM' + struct.pack('>L', 4) + b'AIFC' + b += b'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0) + b += b'WRNG' + struct.pack('B', 0) + self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b)) + + def test_read_wrong_marks(self): + b = b'FORM' + struct.pack('>L', 4) + b'AIFF' + b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) + b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 + b += b'MARK' + struct.pack('>LhB', 3, 1, 1) - with captured_stdout() as s: ++ with self.assertWarns(UserWarning): + f = aifc.open(io.BytesIO(b)) - self.assertEqual( - s.getvalue(), - 'Warning: MARK chunk contains only 0 markers instead of 1\n') + self.assertEqual(f.getmarkers(), None) + + def test_read_comm_kludge_compname_even(self): + b = b'FORM' + struct.pack('>L', 4) + b'AIFC' + b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) + b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00' + b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 - with captured_stdout() as s: ++ with self.assertWarns(UserWarning): + f = aifc.open(io.BytesIO(b)) - self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n') + self.assertEqual(f.getcompname(), b'even') + + def test_read_comm_kludge_compname_odd(self): + b = b'FORM' + struct.pack('>L', 4) + b'AIFC' + b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) + b += b'NONE' + struct.pack('B', 3) + b'odd' + b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 - with captured_stdout() as s: ++ with self.assertWarns(UserWarning): + f = aifc.open(io.BytesIO(b)) - self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n') + self.assertEqual(f.getcompname(), b'odd') + + def test_write_params_raises(self): + fout = aifc.open(io.BytesIO(), 'wb') + wrong_params = (0, 0, 0, 0, b'WRNG', '') + self.assertRaises(aifc.Error, fout.setparams, wrong_params) + self.assertRaises(aifc.Error, fout.getparams) + self.assertRaises(aifc.Error, fout.setnchannels, 0) + self.assertRaises(aifc.Error, fout.getnchannels) + self.assertRaises(aifc.Error, fout.setsampwidth, 0) + self.assertRaises(aifc.Error, fout.getsampwidth) + self.assertRaises(aifc.Error, fout.setframerate, 0) + self.assertRaises(aifc.Error, fout.getframerate) + self.assertRaises(aifc.Error, fout.setcomptype, b'WRNG', '') + fout.aiff() + fout.setnchannels(1) + fout.setsampwidth(1) + fout.setframerate(1) + fout.setnframes(1) + fout.writeframes(b'\x00') + self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1)) + self.assertRaises(aifc.Error, fout.setnchannels, 1) + self.assertRaises(aifc.Error, fout.setsampwidth, 1) + self.assertRaises(aifc.Error, fout.setframerate, 1) + self.assertRaises(aifc.Error, fout.setnframes, 1) + self.assertRaises(aifc.Error, fout.setcomptype, b'NONE', '') + self.assertRaises(aifc.Error, fout.aiff) + self.assertRaises(aifc.Error, fout.aifc) + + def test_write_params_singles(self): + fout = aifc.open(io.BytesIO(), 'wb') + fout.aifc() + fout.setnchannels(1) + fout.setsampwidth(2) + fout.setframerate(3) + fout.setnframes(4) + fout.setcomptype(b'NONE', b'name') + self.assertEqual(fout.getnchannels(), 1) + self.assertEqual(fout.getsampwidth(), 2) + self.assertEqual(fout.getframerate(), 3) + self.assertEqual(fout.getnframes(), 0) + self.assertEqual(fout.tell(), 0) + self.assertEqual(fout.getcomptype(), b'NONE') + self.assertEqual(fout.getcompname(), b'name') + fout.writeframes(b'\x00' * 4 * fout.getsampwidth() * fout.getnchannels()) + self.assertEqual(fout.getnframes(), 4) + self.assertEqual(fout.tell(), 4) + + def test_write_params_bunch(self): + fout = aifc.open(io.BytesIO(), 'wb') + fout.aifc() + p = (1, 2, 3, 4, b'NONE', b'name') + fout.setparams(p) + self.assertEqual(fout.getparams(), p) + fout.initfp(None) + + def test_write_header_raises(self): + fout = aifc.open(io.BytesIO(), 'wb') + self.assertRaises(aifc.Error, fout.close) + fout.setnchannels(1) + self.assertRaises(aifc.Error, fout.close) + fout.setsampwidth(1) + self.assertRaises(aifc.Error, fout.close) + fout.initfp(None) + + def test_write_header_comptype_raises(self): + for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): + fout = aifc.open(io.BytesIO(), 'wb') + fout.setsampwidth(1) + fout.setcomptype(comptype, b'') + self.assertRaises(aifc.Error, fout.close) + fout.initfp(None) + + def test_write_markers_raises(self): + fout = aifc.open(io.BytesIO(), 'wb') + self.assertRaises(aifc.Error, fout.setmark, 0, 0, b'') + self.assertRaises(aifc.Error, fout.setmark, 1, -1, b'') + self.assertRaises(aifc.Error, fout.setmark, 1, 0, None) + self.assertRaises(aifc.Error, fout.getmark, 1) + fout.initfp(None) + + def test_write_aiff_by_extension(self): + sampwidth = 2 + fout = self.fout = aifc.open(TESTFN + '.aiff', 'wb') + fout.setparams((1, sampwidth, 1, 1, b'ULAW', b'')) + frames = b'\x00' * fout.getnchannels() * sampwidth + fout.writeframes(frames) + fout.close() + f = self.f = aifc.open(TESTFN + '.aiff', 'rb') + self.assertEqual(f.getcomptype(), b'NONE') + f.close() + def test_main(): run_unittest(AIFCTest) diff --cc Misc/NEWS index cd522dc6e5ff,5d03fee8e74c..5cf2e7e55970 --- a/Misc/NEWS +++ b/Misc/NEWS @@@ -24,6 -22,6 +24,8 @@@ Core and Builtin Library ------- ++- Issue #13394: the aifc module now uses warnings.warn() to signal warnings. ++ - Issue #14252: Fix subprocess.Popen.terminate() to not raise an error under Windows when the child process has already exited.