Skip to content

Commit 5dc33ee

Browse files
authored
bpo-29110: add test for Aifc_write. (GH-293)
follow up of GH-162
1 parent 5aa3856 commit 5dc33ee

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

Lib/aifc.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ class Aifc_read:
303303
# _ssnd_chunk -- instantiation of a chunk class for the SSND chunk
304304
# _framesize -- size of one frame in the file
305305

306+
_file = None # Set here since __del__ checks it
307+
306308
def initfp(self, file):
307309
self._version = 0
308310
self._convert = None
@@ -547,6 +549,8 @@ class Aifc_write:
547549
# _datalength -- the size of the audio samples written to the header
548550
# _datawritten -- the size of the audio samples actually written
549551

552+
_file = None # Set here since __del__ checks it
553+
550554
def __init__(self, f):
551555
if isinstance(f, str):
552556
file_object = builtins.open(f, 'wb')

Lib/test/test_aifc.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from test.support import check_no_resource_warning, findfile, TESTFN, unlink
22
import unittest
3+
from unittest import mock
34
from test import audiotests
45
from audioop import byteswap
56
import io
@@ -155,7 +156,14 @@ def test_close_opened_files_on_error(self):
155156
with self.assertRaises(aifc.Error):
156157
# Try opening a non-AIFC file, with the expectation that
157158
# `aifc.open` will fail (without raising a ResourceWarning)
158-
f = self.f = aifc.open(non_aifc_file, 'rb')
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')
159167

160168
def test_params_added(self):
161169
f = self.f = aifc.open(TESTFN, 'wb')

0 commit comments

Comments
 (0)