annotate Lib/test/test_aifc.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 6c8b380db727
children b36cb4602e21
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
53607
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
1 from test.test_support import findfile, run_unittest, TESTFN
53595
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
2 import unittest
53607
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
3 import os
53595
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
4
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
5 import aifc
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
6
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
7
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
8 class AIFCTest(unittest.TestCase):
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
9
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
10 def setUp(self):
53607
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
11 self.f = self.fout = None
53595
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
12 self.sndfilepath = findfile('Sine-1000Hz-300ms.aif')
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
13
53607
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
14 def tearDown(self):
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
15 if self.f is not None:
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
16 self.f.close()
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
17 if self.fout is not None:
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
18 try:
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
19 self.fout.close()
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
20 except (aifc.Error, AttributeError):
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
21 pass
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
22 try:
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
23 os.remove(TESTFN)
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
24 except OSError:
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
25 pass
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
26
53595
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
27 def test_skipunknown(self):
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
28 #Issue 2245
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
29 #This file contains chunk types aifc doesn't recognize.
53626
79dc8d588eb3 make sure to close file
Benjamin Peterson <benjamin@python.org>
parents: 53607
diff changeset
30 self.f = aifc.open(self.sndfilepath)
53595
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
31
53596
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
32 def test_params(self):
53607
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
33 f = self.f = aifc.open(self.sndfilepath)
53596
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
34 self.assertEqual(f.getnchannels(), 2)
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
35 self.assertEqual(f.getsampwidth(), 2)
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
36 self.assertEqual(f.getframerate(), 48000)
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
37 self.assertEqual(f.getnframes(), 14400)
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
38 self.assertEqual(f.getcomptype(), 'NONE')
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
39 self.assertEqual(f.getcompname(), 'not compressed')
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
40 self.assertEqual(f.getparams(), (2, 2, 48000, 14400, 'NONE', 'not compressed'))
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
41
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
42 def test_read(self):
53607
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
43 f = self.f = aifc.open(self.sndfilepath)
53596
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
44 self.assertEqual(f.tell(), 0)
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
45 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
46 f.rewind()
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
47 pos0 = f.tell()
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
48 self.assertEqual(pos0, 0)
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
49 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
50 pos2 = f.tell()
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
51 self.assertEqual(pos2, 2)
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
52 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad')
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
53 f.setpos(pos2)
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
54 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad')
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
55 f.setpos(pos0)
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
56 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
53607
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
57
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
58 def test_write(self):
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
59 f = self.f = aifc.open(self.sndfilepath)
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
60 fout = self.fout = aifc.open(TESTFN, 'wb')
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
61 fout.aifc()
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
62 fout.setparams(f.getparams())
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
63 for frame in range(f.getnframes()):
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
64 fout.writeframes(f.readframes(1))
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
65 fout.close()
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
66 fout = self.fout = aifc.open(TESTFN, 'rb')
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
67 f.rewind()
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
68 self.assertEqual(f.getparams(), fout.getparams())
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
69 self.assertEqual(f.readframes(5), fout.readframes(5))
53596
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
70
53607
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
71 def test_compress(self):
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
72 f = self.f = aifc.open(self.sndfilepath)
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
73 fout = self.fout = aifc.open(TESTFN, 'wb')
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
74 fout.aifc()
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
75 fout.setnchannels(f.getnchannels())
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
76 fout.setsampwidth(f.getsampwidth())
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
77 fout.setframerate(f.getframerate())
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
78 fout.setcomptype('ULAW', 'foo')
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
79 for frame in range(f.getnframes()):
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
80 fout.writeframes(f.readframes(1))
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
81 fout.close()
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
82 self.assertLess(
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
83 os.stat(TESTFN).st_size,
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
84 os.stat(self.sndfilepath).st_size*0.75,
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
85 )
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
86 fout = self.fout = aifc.open(TESTFN, 'rb')
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
87 f.rewind()
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
88 self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3])
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
89 self.assertEqual(fout.getcomptype(), 'ULAW')
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
90 self.assertEqual(fout.getcompname(), 'foo')
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
91 # XXX: this test fails, not sure if it should succeed or not
3d6bc42646b5 More aifc tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53596
diff changeset
92 # self.assertEqual(f.readframes(5), fout.readframes(5))
53596
8617e81016b6 Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents: 53595
diff changeset
93
53872
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
94 def test_close(self):
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
95 class Wrapfile(object):
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
96 def __init__(self, file):
53877
6c8b380db727 Pre-opened test file needs to be opened in binary mode.
R. David Murray <rdmurray@bitdance.com>
parents: 53872
diff changeset
97 self.file = open(file, 'rb')
53872
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
98 self.closed = False
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
99 def close(self):
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
100 self.file.close()
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
101 self.closed = True
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
102 def __getattr__(self, attr): return getattr(self.file, attr)
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
103 testfile = Wrapfile(self.sndfilepath)
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
104 f = self.f = aifc.open(testfile)
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
105 self.assertEqual(testfile.closed, False)
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
106 f.close()
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
107 self.assertEqual(testfile.closed, True)
85b9df525e10 Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents: 53626
diff changeset
108
53595
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
109
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
110 def test_main():
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
111 run_unittest(AIFCTest)
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
112
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
113
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
114 if __name__ == "__main__":
4ef7f93d61a1 Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff changeset
115 unittest.main()