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