From e9ebde48cf5d7892794eacaeba683b308ce5a94b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 13 Jul 2010 23:04:56 +0000 Subject: [PATCH] Issue #9243: Fix sndhdr module and add unit tests, contributed by James Lee. --- Lib/sndhdr.py | 8 +++--- Lib/test/sndhdrdata/README | 12 ++++++++ Lib/test/sndhdrdata/sndhdr.8svx | Bin 0 -> 110 bytes Lib/test/sndhdrdata/sndhdr.aifc | Bin 0 -> 106 bytes Lib/test/sndhdrdata/sndhdr.aiff | Bin 0 -> 108 bytes Lib/test/sndhdrdata/sndhdr.au | Bin 0 -> 64 bytes Lib/test/sndhdrdata/sndhdr.hcom | Bin 0 -> 256 bytes Lib/test/sndhdrdata/sndhdr.sndt | Bin 0 -> 129 bytes Lib/test/sndhdrdata/sndhdr.voc | Bin 0 -> 63 bytes Lib/test/sndhdrdata/sndhdr.wav | Bin 0 -> 64 bytes Lib/test/test_sndhdr.py | 47 ++++++++++++++++++++++++++++++++ Misc/ACKS | 1 + Misc/NEWS | 2 ++ 13 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 Lib/test/sndhdrdata/README create mode 100644 Lib/test/sndhdrdata/sndhdr.8svx create mode 100644 Lib/test/sndhdrdata/sndhdr.aifc create mode 100644 Lib/test/sndhdrdata/sndhdr.aiff create mode 100644 Lib/test/sndhdrdata/sndhdr.au create mode 100644 Lib/test/sndhdrdata/sndhdr.hcom create mode 100644 Lib/test/sndhdrdata/sndhdr.sndt create mode 100644 Lib/test/sndhdrdata/sndhdr.voc create mode 100644 Lib/test/sndhdrdata/sndhdr.wav create mode 100644 Lib/test/test_sndhdr.py diff --git a/Lib/sndhdr.py b/Lib/sndhdr.py index a8e0a051666..9f5dcc90d43 100644 --- a/Lib/sndhdr.py +++ b/Lib/sndhdr.py @@ -57,12 +57,12 @@ def whathdr(filename): def test_aifc(h, f): import aifc - if h.startswith(b'FORM'): + if not h.startswith(b'FORM'): return None if h[8:12] == b'AIFC': fmt = 'aifc' elif h[8:12] == b'AIFF': - fmt = b'aiff' + fmt = 'aiff' else: return None f.seek(0) @@ -123,7 +123,7 @@ def test_hcom(h, f): def test_voc(h, f): - if h.startswith(b'Creative Voice File\032'): + if not h.startswith(b'Creative Voice File\032'): return None sbseek = get_short_le(h[20:22]) rate = 0 @@ -150,7 +150,7 @@ def test_wav(h, f): def test_8svx(h, f): - if h.startswith(b'FORM') or h[8:12] != b'8SVX': + if not h.startswith(b'FORM') or h[8:12] != b'8SVX': return None # Should decode it to get #channels -- assume always 1 return '8svx', 0, 1, 0, 8 diff --git a/Lib/test/sndhdrdata/README b/Lib/test/sndhdrdata/README new file mode 100644 index 00000000000..8a17c0041a4 --- /dev/null +++ b/Lib/test/sndhdrdata/README @@ -0,0 +1,12 @@ +Sound file samples used by Lib/test/test_sndhdr.py and generated using the +following commands: + + dd if=/dev/zero of=sndhdr.raw bs=20 count=1 + sox -s -2 -c 2 -r 44100 sndhdr.raw sndhdr. + +Sound file samples used by Lib/test/test_sndhdr.py and generated using the +following commands: + + dd if=/dev/zero of=sndhdr.raw bs=20 count=1 + sox -s -2 -c 2 -r 44100 sndhdr.raw sndhdr. + diff --git a/Lib/test/sndhdrdata/sndhdr.8svx b/Lib/test/sndhdrdata/sndhdr.8svx new file mode 100644 index 0000000000000000000000000000000000000000..8cd6cde5e09f356d47b031db7bae33697a6f751b GIT binary patch literal 110 zcmZ?s5AtPTU`VqF4vPr$a0vpkMSz$Uh{0fu3nK#q5IOq!`2z(M+%j`g6_Sfm6H8K4 y6p|_xg7ZuBQWRV(k~0$X(o+=_oIM=ks0T>0|y bOB9mxa|?=6i;GiJ7=nZSK(aDm1vm%*H%bt5 literal 0 HcmV?d00001 diff --git a/Lib/test/sndhdrdata/sndhdr.aiff b/Lib/test/sndhdrdata/sndhdr.aiff new file mode 100644 index 0000000000000000000000000000000000000000..8c279a762f1c7074f9e91356b7b81b3da420db03 GIT binary patch literal 108 zcmZ?s5AtPTU`TQFbaQj|_YDEEr5G4bB$=NDvIGK(@{?1Gi&Ik+k}4H~^CN&NK&pip bn1CcJgMb6y8W$i72!ezCKr9(B2L}NFoskd! literal 0 HcmV?d00001 diff --git a/Lib/test/sndhdrdata/sndhdr.au b/Lib/test/sndhdrdata/sndhdr.au new file mode 100644 index 0000000000000000000000000000000000000000..67c9e8fdd995efb47517e64f0f0dc98301f111ec GIT binary patch literal 64 ucmdNZ&P!onV9)_#5g=w}U|8bG2SA%aW5!`a^#D8~$yX9UuW42=K(1L+2+7)>bv&sPOH literal 0 HcmV?d00001 diff --git a/Lib/test/sndhdrdata/sndhdr.sndt b/Lib/test/sndhdrdata/sndhdr.sndt new file mode 100644 index 0000000000000000000000000000000000000000..e1ca9cb185d157fa27853c90ffa8c420f755e297 GIT binary patch literal 129 zcmWIc5A}1AVqjopfB+EfvW9_yi-Cn9peR2%wYWGnMIotDAviw*s)TA(eM5ai0|0H< B3kLuI literal 0 HcmV?d00001 diff --git a/Lib/test/sndhdrdata/sndhdr.voc b/Lib/test/sndhdrdata/sndhdr.voc new file mode 100644 index 0000000000000000000000000000000000000000..53a91fd1eae30b3e57105092831a49d26787a5c3 GIT binary patch literal 63 vcmZ=RN=+=uEK5}g%g;zB;s=5N literal 0 HcmV?d00001 diff --git a/Lib/test/test_sndhdr.py b/Lib/test/test_sndhdr.py new file mode 100644 index 00000000000..4b4c8573b5b --- /dev/null +++ b/Lib/test/test_sndhdr.py @@ -0,0 +1,47 @@ +import sndhdr +import unittest +from test.support import findfile + +class TestFormats(unittest.TestCase): + def test_data(self): + for filename, expected in ( + ('sndhdr.8svx', ('8svx', 0, 1, 0, 8)), + ('sndhdr.aifc', ('aifc', 44100, 2, 5, 16)), + ('sndhdr.aiff', ('aiff', 44100, 2, 5, 16)), + ('sndhdr.au', ('au', 44100, 2, 5.0, 16)), + ('sndhdr.hcom', ('hcom', 22050.0, 1, -1, 8)), + ('sndhdr.sndt', ('sndt', 44100, 1, 5, 8)), + ('sndhdr.voc', ('voc', 0, 1, -1, 8)), + ('sndhdr.wav', ('wav', 44100, 2, -1, 16)), + ): + filename = findfile(filename, subdir="sndhdrdata") + what = sndhdr.what(filename) + self.assertNotEqual(what, None, filename) + self.assertSequenceEqual(what, expected) + +if __name__ == '__main__': + unittest.main() + +import sndhdr +import unittest +from test.support import findfile + +class TestFormats(unittest.TestCase): + def test_data(self): + for filename, expected in ( + ('sndhdr.8svx', ('8svx', 0, 1, 0, 8)), + ('sndhdr.aifc', ('aifc', 44100, 2, 5, 16)), + ('sndhdr.aiff', ('aiff', 44100, 2, 5, 16)), + ('sndhdr.au', ('au', 44100, 2, 5.0, 16)), + ('sndhdr.hcom', ('hcom', 22050.0, 1, -1, 8)), + ('sndhdr.sndt', ('sndt', 44100, 1, 5, 8)), + ('sndhdr.voc', ('voc', 0, 1, -1, 8)), + ('sndhdr.wav', ('wav', 44100, 2, -1, 16)), + ): + filename = findfile(filename, subdir="sndhdrdata") + what = sndhdr.what(filename) + self.assertNotEqual(what, None, filename) + self.assertSequenceEqual(what, expected) + +if __name__ == '__main__': + unittest.main() diff --git a/Misc/ACKS b/Misc/ACKS index b340795d765..282378949f5 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -454,6 +454,7 @@ Ben Laurie Simon Law Chris Lawrence Brian Leair +James Lee John J. Lee Inyeol Lee Thomas Lee diff --git a/Misc/NEWS b/Misc/NEWS index 86e8d16a26d..6579ae1153e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -473,6 +473,8 @@ C-API Library ------- +- Issue #9243: Fix sndhdr module and add unit tests, contributed by James Lee. + - ``ast.literal_eval()`` now allows byte literals. - Issue #9137: Fix issue in MutableMapping.update, which incorrectly