1 | n/a | from test.support import findfile, TESTFN, unlink |
---|
2 | n/a | import array |
---|
3 | n/a | import io |
---|
4 | n/a | import pickle |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class UnseekableIO(io.FileIO): |
---|
8 | n/a | def tell(self): |
---|
9 | n/a | raise io.UnsupportedOperation |
---|
10 | n/a | |
---|
11 | n/a | def seek(self, *args, **kwargs): |
---|
12 | n/a | raise io.UnsupportedOperation |
---|
13 | n/a | |
---|
14 | n/a | |
---|
15 | n/a | class AudioTests: |
---|
16 | n/a | close_fd = False |
---|
17 | n/a | |
---|
18 | n/a | def setUp(self): |
---|
19 | n/a | self.f = self.fout = None |
---|
20 | n/a | |
---|
21 | n/a | def tearDown(self): |
---|
22 | n/a | if self.f is not None: |
---|
23 | n/a | self.f.close() |
---|
24 | n/a | if self.fout is not None: |
---|
25 | n/a | self.fout.close() |
---|
26 | n/a | unlink(TESTFN) |
---|
27 | n/a | |
---|
28 | n/a | def check_params(self, f, nchannels, sampwidth, framerate, nframes, |
---|
29 | n/a | comptype, compname): |
---|
30 | n/a | self.assertEqual(f.getnchannels(), nchannels) |
---|
31 | n/a | self.assertEqual(f.getsampwidth(), sampwidth) |
---|
32 | n/a | self.assertEqual(f.getframerate(), framerate) |
---|
33 | n/a | self.assertEqual(f.getnframes(), nframes) |
---|
34 | n/a | self.assertEqual(f.getcomptype(), comptype) |
---|
35 | n/a | self.assertEqual(f.getcompname(), compname) |
---|
36 | n/a | |
---|
37 | n/a | params = f.getparams() |
---|
38 | n/a | self.assertEqual(params, |
---|
39 | n/a | (nchannels, sampwidth, framerate, nframes, comptype, compname)) |
---|
40 | n/a | self.assertEqual(params.nchannels, nchannels) |
---|
41 | n/a | self.assertEqual(params.sampwidth, sampwidth) |
---|
42 | n/a | self.assertEqual(params.framerate, framerate) |
---|
43 | n/a | self.assertEqual(params.nframes, nframes) |
---|
44 | n/a | self.assertEqual(params.comptype, comptype) |
---|
45 | n/a | self.assertEqual(params.compname, compname) |
---|
46 | n/a | |
---|
47 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
48 | n/a | dump = pickle.dumps(params, proto) |
---|
49 | n/a | self.assertEqual(pickle.loads(dump), params) |
---|
50 | n/a | |
---|
51 | n/a | |
---|
52 | n/a | class AudioWriteTests(AudioTests): |
---|
53 | n/a | |
---|
54 | n/a | def create_file(self, testfile): |
---|
55 | n/a | f = self.fout = self.module.open(testfile, 'wb') |
---|
56 | n/a | f.setnchannels(self.nchannels) |
---|
57 | n/a | f.setsampwidth(self.sampwidth) |
---|
58 | n/a | f.setframerate(self.framerate) |
---|
59 | n/a | f.setcomptype(self.comptype, self.compname) |
---|
60 | n/a | return f |
---|
61 | n/a | |
---|
62 | n/a | def check_file(self, testfile, nframes, frames): |
---|
63 | n/a | with self.module.open(testfile, 'rb') as f: |
---|
64 | n/a | self.assertEqual(f.getnchannels(), self.nchannels) |
---|
65 | n/a | self.assertEqual(f.getsampwidth(), self.sampwidth) |
---|
66 | n/a | self.assertEqual(f.getframerate(), self.framerate) |
---|
67 | n/a | self.assertEqual(f.getnframes(), nframes) |
---|
68 | n/a | self.assertEqual(f.readframes(nframes), frames) |
---|
69 | n/a | |
---|
70 | n/a | def test_write_params(self): |
---|
71 | n/a | f = self.create_file(TESTFN) |
---|
72 | n/a | f.setnframes(self.nframes) |
---|
73 | n/a | f.writeframes(self.frames) |
---|
74 | n/a | self.check_params(f, self.nchannels, self.sampwidth, self.framerate, |
---|
75 | n/a | self.nframes, self.comptype, self.compname) |
---|
76 | n/a | f.close() |
---|
77 | n/a | |
---|
78 | n/a | def test_write_context_manager_calls_close(self): |
---|
79 | n/a | # Close checks for a minimum header and will raise an error |
---|
80 | n/a | # if it is not set, so this proves that close is called. |
---|
81 | n/a | with self.assertRaises(self.module.Error): |
---|
82 | n/a | with self.module.open(TESTFN, 'wb'): |
---|
83 | n/a | pass |
---|
84 | n/a | with self.assertRaises(self.module.Error): |
---|
85 | n/a | with open(TESTFN, 'wb') as testfile: |
---|
86 | n/a | with self.module.open(testfile): |
---|
87 | n/a | pass |
---|
88 | n/a | |
---|
89 | n/a | def test_context_manager_with_open_file(self): |
---|
90 | n/a | with open(TESTFN, 'wb') as testfile: |
---|
91 | n/a | with self.module.open(testfile) as f: |
---|
92 | n/a | f.setnchannels(self.nchannels) |
---|
93 | n/a | f.setsampwidth(self.sampwidth) |
---|
94 | n/a | f.setframerate(self.framerate) |
---|
95 | n/a | f.setcomptype(self.comptype, self.compname) |
---|
96 | n/a | self.assertEqual(testfile.closed, self.close_fd) |
---|
97 | n/a | with open(TESTFN, 'rb') as testfile: |
---|
98 | n/a | with self.module.open(testfile) as f: |
---|
99 | n/a | self.assertFalse(f.getfp().closed) |
---|
100 | n/a | params = f.getparams() |
---|
101 | n/a | self.assertEqual(params.nchannels, self.nchannels) |
---|
102 | n/a | self.assertEqual(params.sampwidth, self.sampwidth) |
---|
103 | n/a | self.assertEqual(params.framerate, self.framerate) |
---|
104 | n/a | if not self.close_fd: |
---|
105 | n/a | self.assertIsNone(f.getfp()) |
---|
106 | n/a | self.assertEqual(testfile.closed, self.close_fd) |
---|
107 | n/a | |
---|
108 | n/a | def test_context_manager_with_filename(self): |
---|
109 | n/a | # If the file doesn't get closed, this test won't fail, but it will |
---|
110 | n/a | # produce a resource leak warning. |
---|
111 | n/a | with self.module.open(TESTFN, 'wb') as f: |
---|
112 | n/a | f.setnchannels(self.nchannels) |
---|
113 | n/a | f.setsampwidth(self.sampwidth) |
---|
114 | n/a | f.setframerate(self.framerate) |
---|
115 | n/a | f.setcomptype(self.comptype, self.compname) |
---|
116 | n/a | with self.module.open(TESTFN) as f: |
---|
117 | n/a | self.assertFalse(f.getfp().closed) |
---|
118 | n/a | params = f.getparams() |
---|
119 | n/a | self.assertEqual(params.nchannels, self.nchannels) |
---|
120 | n/a | self.assertEqual(params.sampwidth, self.sampwidth) |
---|
121 | n/a | self.assertEqual(params.framerate, self.framerate) |
---|
122 | n/a | if not self.close_fd: |
---|
123 | n/a | self.assertIsNone(f.getfp()) |
---|
124 | n/a | |
---|
125 | n/a | def test_write(self): |
---|
126 | n/a | f = self.create_file(TESTFN) |
---|
127 | n/a | f.setnframes(self.nframes) |
---|
128 | n/a | f.writeframes(self.frames) |
---|
129 | n/a | f.close() |
---|
130 | n/a | |
---|
131 | n/a | self.check_file(TESTFN, self.nframes, self.frames) |
---|
132 | n/a | |
---|
133 | n/a | def test_write_bytearray(self): |
---|
134 | n/a | f = self.create_file(TESTFN) |
---|
135 | n/a | f.setnframes(self.nframes) |
---|
136 | n/a | f.writeframes(bytearray(self.frames)) |
---|
137 | n/a | f.close() |
---|
138 | n/a | |
---|
139 | n/a | self.check_file(TESTFN, self.nframes, self.frames) |
---|
140 | n/a | |
---|
141 | n/a | def test_write_array(self): |
---|
142 | n/a | f = self.create_file(TESTFN) |
---|
143 | n/a | f.setnframes(self.nframes) |
---|
144 | n/a | f.writeframes(array.array('h', self.frames)) |
---|
145 | n/a | f.close() |
---|
146 | n/a | |
---|
147 | n/a | self.check_file(TESTFN, self.nframes, self.frames) |
---|
148 | n/a | |
---|
149 | n/a | def test_write_memoryview(self): |
---|
150 | n/a | f = self.create_file(TESTFN) |
---|
151 | n/a | f.setnframes(self.nframes) |
---|
152 | n/a | f.writeframes(memoryview(self.frames)) |
---|
153 | n/a | f.close() |
---|
154 | n/a | |
---|
155 | n/a | self.check_file(TESTFN, self.nframes, self.frames) |
---|
156 | n/a | |
---|
157 | n/a | def test_incompleted_write(self): |
---|
158 | n/a | with open(TESTFN, 'wb') as testfile: |
---|
159 | n/a | testfile.write(b'ababagalamaga') |
---|
160 | n/a | f = self.create_file(testfile) |
---|
161 | n/a | f.setnframes(self.nframes + 1) |
---|
162 | n/a | f.writeframes(self.frames) |
---|
163 | n/a | f.close() |
---|
164 | n/a | |
---|
165 | n/a | with open(TESTFN, 'rb') as testfile: |
---|
166 | n/a | self.assertEqual(testfile.read(13), b'ababagalamaga') |
---|
167 | n/a | self.check_file(testfile, self.nframes, self.frames) |
---|
168 | n/a | |
---|
169 | n/a | def test_multiple_writes(self): |
---|
170 | n/a | with open(TESTFN, 'wb') as testfile: |
---|
171 | n/a | testfile.write(b'ababagalamaga') |
---|
172 | n/a | f = self.create_file(testfile) |
---|
173 | n/a | f.setnframes(self.nframes) |
---|
174 | n/a | framesize = self.nchannels * self.sampwidth |
---|
175 | n/a | f.writeframes(self.frames[:-framesize]) |
---|
176 | n/a | f.writeframes(self.frames[-framesize:]) |
---|
177 | n/a | f.close() |
---|
178 | n/a | |
---|
179 | n/a | with open(TESTFN, 'rb') as testfile: |
---|
180 | n/a | self.assertEqual(testfile.read(13), b'ababagalamaga') |
---|
181 | n/a | self.check_file(testfile, self.nframes, self.frames) |
---|
182 | n/a | |
---|
183 | n/a | def test_overflowed_write(self): |
---|
184 | n/a | with open(TESTFN, 'wb') as testfile: |
---|
185 | n/a | testfile.write(b'ababagalamaga') |
---|
186 | n/a | f = self.create_file(testfile) |
---|
187 | n/a | f.setnframes(self.nframes - 1) |
---|
188 | n/a | f.writeframes(self.frames) |
---|
189 | n/a | f.close() |
---|
190 | n/a | |
---|
191 | n/a | with open(TESTFN, 'rb') as testfile: |
---|
192 | n/a | self.assertEqual(testfile.read(13), b'ababagalamaga') |
---|
193 | n/a | self.check_file(testfile, self.nframes, self.frames) |
---|
194 | n/a | |
---|
195 | n/a | def test_unseekable_read(self): |
---|
196 | n/a | with self.create_file(TESTFN) as f: |
---|
197 | n/a | f.setnframes(self.nframes) |
---|
198 | n/a | f.writeframes(self.frames) |
---|
199 | n/a | |
---|
200 | n/a | with UnseekableIO(TESTFN, 'rb') as testfile: |
---|
201 | n/a | self.check_file(testfile, self.nframes, self.frames) |
---|
202 | n/a | |
---|
203 | n/a | def test_unseekable_write(self): |
---|
204 | n/a | with UnseekableIO(TESTFN, 'wb') as testfile: |
---|
205 | n/a | with self.create_file(testfile) as f: |
---|
206 | n/a | f.setnframes(self.nframes) |
---|
207 | n/a | f.writeframes(self.frames) |
---|
208 | n/a | |
---|
209 | n/a | self.check_file(TESTFN, self.nframes, self.frames) |
---|
210 | n/a | |
---|
211 | n/a | def test_unseekable_incompleted_write(self): |
---|
212 | n/a | with UnseekableIO(TESTFN, 'wb') as testfile: |
---|
213 | n/a | testfile.write(b'ababagalamaga') |
---|
214 | n/a | f = self.create_file(testfile) |
---|
215 | n/a | f.setnframes(self.nframes + 1) |
---|
216 | n/a | try: |
---|
217 | n/a | f.writeframes(self.frames) |
---|
218 | n/a | except OSError: |
---|
219 | n/a | pass |
---|
220 | n/a | try: |
---|
221 | n/a | f.close() |
---|
222 | n/a | except OSError: |
---|
223 | n/a | pass |
---|
224 | n/a | |
---|
225 | n/a | with open(TESTFN, 'rb') as testfile: |
---|
226 | n/a | self.assertEqual(testfile.read(13), b'ababagalamaga') |
---|
227 | n/a | self.check_file(testfile, self.nframes + 1, self.frames) |
---|
228 | n/a | |
---|
229 | n/a | def test_unseekable_overflowed_write(self): |
---|
230 | n/a | with UnseekableIO(TESTFN, 'wb') as testfile: |
---|
231 | n/a | testfile.write(b'ababagalamaga') |
---|
232 | n/a | f = self.create_file(testfile) |
---|
233 | n/a | f.setnframes(self.nframes - 1) |
---|
234 | n/a | try: |
---|
235 | n/a | f.writeframes(self.frames) |
---|
236 | n/a | except OSError: |
---|
237 | n/a | pass |
---|
238 | n/a | try: |
---|
239 | n/a | f.close() |
---|
240 | n/a | except OSError: |
---|
241 | n/a | pass |
---|
242 | n/a | |
---|
243 | n/a | with open(TESTFN, 'rb') as testfile: |
---|
244 | n/a | self.assertEqual(testfile.read(13), b'ababagalamaga') |
---|
245 | n/a | framesize = self.nchannels * self.sampwidth |
---|
246 | n/a | self.check_file(testfile, self.nframes - 1, self.frames[:-framesize]) |
---|
247 | n/a | |
---|
248 | n/a | |
---|
249 | n/a | class AudioTestsWithSourceFile(AudioTests): |
---|
250 | n/a | |
---|
251 | n/a | @classmethod |
---|
252 | n/a | def setUpClass(cls): |
---|
253 | n/a | cls.sndfilepath = findfile(cls.sndfilename, subdir='audiodata') |
---|
254 | n/a | |
---|
255 | n/a | def test_read_params(self): |
---|
256 | n/a | f = self.f = self.module.open(self.sndfilepath) |
---|
257 | n/a | #self.assertEqual(f.getfp().name, self.sndfilepath) |
---|
258 | n/a | self.check_params(f, self.nchannels, self.sampwidth, self.framerate, |
---|
259 | n/a | self.sndfilenframes, self.comptype, self.compname) |
---|
260 | n/a | |
---|
261 | n/a | def test_close(self): |
---|
262 | n/a | with open(self.sndfilepath, 'rb') as testfile: |
---|
263 | n/a | f = self.f = self.module.open(testfile) |
---|
264 | n/a | self.assertFalse(testfile.closed) |
---|
265 | n/a | f.close() |
---|
266 | n/a | self.assertEqual(testfile.closed, self.close_fd) |
---|
267 | n/a | with open(TESTFN, 'wb') as testfile: |
---|
268 | n/a | fout = self.fout = self.module.open(testfile, 'wb') |
---|
269 | n/a | self.assertFalse(testfile.closed) |
---|
270 | n/a | with self.assertRaises(self.module.Error): |
---|
271 | n/a | fout.close() |
---|
272 | n/a | self.assertEqual(testfile.closed, self.close_fd) |
---|
273 | n/a | fout.close() # do nothing |
---|
274 | n/a | |
---|
275 | n/a | def test_read(self): |
---|
276 | n/a | framesize = self.nchannels * self.sampwidth |
---|
277 | n/a | chunk1 = self.frames[:2 * framesize] |
---|
278 | n/a | chunk2 = self.frames[2 * framesize: 4 * framesize] |
---|
279 | n/a | f = self.f = self.module.open(self.sndfilepath) |
---|
280 | n/a | self.assertEqual(f.readframes(0), b'') |
---|
281 | n/a | self.assertEqual(f.tell(), 0) |
---|
282 | n/a | self.assertEqual(f.readframes(2), chunk1) |
---|
283 | n/a | f.rewind() |
---|
284 | n/a | pos0 = f.tell() |
---|
285 | n/a | self.assertEqual(pos0, 0) |
---|
286 | n/a | self.assertEqual(f.readframes(2), chunk1) |
---|
287 | n/a | pos2 = f.tell() |
---|
288 | n/a | self.assertEqual(pos2, 2) |
---|
289 | n/a | self.assertEqual(f.readframes(2), chunk2) |
---|
290 | n/a | f.setpos(pos2) |
---|
291 | n/a | self.assertEqual(f.readframes(2), chunk2) |
---|
292 | n/a | f.setpos(pos0) |
---|
293 | n/a | self.assertEqual(f.readframes(2), chunk1) |
---|
294 | n/a | with self.assertRaises(self.module.Error): |
---|
295 | n/a | f.setpos(-1) |
---|
296 | n/a | with self.assertRaises(self.module.Error): |
---|
297 | n/a | f.setpos(f.getnframes() + 1) |
---|
298 | n/a | |
---|
299 | n/a | def test_copy(self): |
---|
300 | n/a | f = self.f = self.module.open(self.sndfilepath) |
---|
301 | n/a | fout = self.fout = self.module.open(TESTFN, 'wb') |
---|
302 | n/a | fout.setparams(f.getparams()) |
---|
303 | n/a | i = 0 |
---|
304 | n/a | n = f.getnframes() |
---|
305 | n/a | while n > 0: |
---|
306 | n/a | i += 1 |
---|
307 | n/a | fout.writeframes(f.readframes(i)) |
---|
308 | n/a | n -= i |
---|
309 | n/a | fout.close() |
---|
310 | n/a | fout = self.fout = self.module.open(TESTFN, 'rb') |
---|
311 | n/a | f.rewind() |
---|
312 | n/a | self.assertEqual(f.getparams(), fout.getparams()) |
---|
313 | n/a | self.assertEqual(f.readframes(f.getnframes()), |
---|
314 | n/a | fout.readframes(fout.getnframes())) |
---|
315 | n/a | |
---|
316 | n/a | def test_read_not_from_start(self): |
---|
317 | n/a | with open(TESTFN, 'wb') as testfile: |
---|
318 | n/a | testfile.write(b'ababagalamaga') |
---|
319 | n/a | with open(self.sndfilepath, 'rb') as f: |
---|
320 | n/a | testfile.write(f.read()) |
---|
321 | n/a | |
---|
322 | n/a | with open(TESTFN, 'rb') as testfile: |
---|
323 | n/a | self.assertEqual(testfile.read(13), b'ababagalamaga') |
---|
324 | n/a | with self.module.open(testfile, 'rb') as f: |
---|
325 | n/a | self.assertEqual(f.getnchannels(), self.nchannels) |
---|
326 | n/a | self.assertEqual(f.getsampwidth(), self.sampwidth) |
---|
327 | n/a | self.assertEqual(f.getframerate(), self.framerate) |
---|
328 | n/a | self.assertEqual(f.getnframes(), self.sndfilenframes) |
---|
329 | n/a | self.assertEqual(f.readframes(self.nframes), self.frames) |
---|