| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -294,6 +294,8 @@ class Aifc_read: | |||
| 294 | 294 | # _ssnd_chunk -- instantiation of a chunk class for the SSND chunk | |
| 295 | 295 | # _framesize -- size of one frame in the file | |
| 296 | 296 | ||
| 297 | + _file = None # Set here since __del__ checks it | ||
| 298 | + | ||
| 297 | 299 | def initfp(self, file): | |
| 298 | 300 | self._version = 0 | |
| 299 | 301 | self._convert = None | |
@@ -335,9 +337,15 @@ def initfp(self, file): | |||
| 335 | 337 | ||
| 336 | 338 | def __init__(self, f): | |
| 337 | 339 | if isinstance(f, str): | |
| 338 | - f = builtins.open(f, 'rb') | ||
| 339 | - # else, assume it is an open file object already | ||
| 340 | - self.initfp(f) | ||
| 340 | + file_object = builtins.open(f, 'rb') | ||
| 341 | + try: | ||
| 342 | + self.initfp(file_object) | ||
| 343 | + except: | ||
| 344 | + file_object.close() | ||
| 345 | + raise | ||
| 346 | + else: | ||
| 347 | + # assume it is an open file object already | ||
| 348 | + self.initfp(f) | ||
| 341 | 349 | ||
| 342 | 350 | def __enter__(self): | |
| 343 | 351 | return self | |
@@ -532,18 +540,23 @@ class Aifc_write: | |||
| 532 | 540 | # _datalength -- the size of the audio samples written to the header | |
| 533 | 541 | # _datawritten -- the size of the audio samples actually written | |
| 534 | 542 | ||
| 543 | + _file = None # Set here since __del__ checks it | ||
| 544 | + | ||
| 535 | 545 | def __init__(self, f): | |
| 536 | 546 | if isinstance(f, str): | |
| 537 | - filename = f | ||
| 538 | - f = builtins.open(f, 'wb') | ||
| 539 | - else: | ||
| 540 | - # else, assume it is an open file object already | ||
| 541 | - filename = '???' | ||
| 542 | - self.initfp(f) | ||
| 543 | - if filename[-5:] == '.aiff': | ||
| 544 | - self._aifc = 0 | ||
| 547 | + file_object = builtins.open(f, 'wb') | ||
| 548 | + try: | ||
| 549 | + self.initfp(file_object) | ||
| 550 | + except: | ||
| 551 | + file_object.close() | ||
| 552 | + raise | ||
| 553 | + | ||
| 554 | + # treat .aiff file extensions as non-compressed audio | ||
| 555 | + if f.endswith('.aiff'): | ||
| 556 | + self._aifc = 0 | ||
| 545 | 557 | else: | |
| 546 | - self._aifc = 1 | ||
| 558 | + # assume it is an open file object already | ||
| 559 | + self.initfp(f) | ||
| 547 | 560 | ||
| 548 | 561 | def initfp(self, file): | |
| 549 | 562 | self._file = file | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 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() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,9 @@ Extension Modules | |||
| 32 | 32 | Library | |
| 33 | 33 | ------- | |
| 34 | 34 | ||
| 35 | + - bpo-29110: Fix file object leak in aifc.open() when file is given as a | ||
| 36 | + filesystem path and is not in valid AIFF format. Patch by Anthony Zhang. | ||
| 37 | + | ||
| 35 | 38 | - Issue #28961: Fix unittest.mock._Call helper: don't ignore the name parameter | |
| 36 | 39 | anymore. Patch written by Jiajun Huang. | |
| 37 | 40 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments