|
1 |
| -from test.support import findfile, TESTFN, unlink |
| 1 | +from test.support import check_no_resource_warning, findfile, TESTFN, unlink |
2 | 2 | import unittest
|
| 3 | +from unittest import mock |
3 | 4 | from test import audiotests
|
4 | 5 | from audioop import byteswap
|
5 | 6 | import os
|
@@ -150,6 +151,21 @@ def test_skipunknown(self):
|
150 | 151 | #This file contains chunk types aifc doesn't recognize.
|
151 | 152 | self.f = aifc.open(findfile('Sine-1000Hz-300ms.aif'))
|
152 | 153 |
|
| 154 | + def test_close_opened_files_on_error(self): |
| 155 | + non_aifc_file = findfile('pluck-pcm8.wav', subdir='audiodata') |
| 156 | + with check_no_resource_warning(self): |
| 157 | + with self.assertRaises(aifc.Error): |
| 158 | + # Try opening a non-AIFC file, with the expectation that |
| 159 | + # `aifc.open` will fail (without raising a ResourceWarning) |
| 160 | + self.f = aifc.open(non_aifc_file, 'rb') |
| 161 | + |
| 162 | + # Aifc_write.initfp() won't raise in normal case. But some errors |
| 163 | + # (e.g. MemoryError, KeyboardInterrupt, etc..) can happen. |
| 164 | + with mock.patch.object(aifc.Aifc_write, 'initfp', |
| 165 | + side_effect=RuntimeError): |
| 166 | + with self.assertRaises(RuntimeError): |
| 167 | + self.fout = aifc.open(TESTFN, 'wb') |
| 168 | + |
153 | 169 | def test_params_added(self):
|
154 | 170 | f = self.f = aifc.open(TESTFN, 'wb')
|
155 | 171 | f.aiff()
|
|
0 commit comments