New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[patch] Fix file object leak in aifc.open
when given invalid AIFF file.
#73296
Comments
Summary This shows up as two closely-related issues:
I have confirmed the issue is present in all the selected Python versions. Steps to reproduce For the first issue, run this code:
# to simplify this example, use this script itself as a random non-AIFF file (though any non-AIFF file will work fine)
FILE = __file__
# print warnings on stderr
import warnings
warnings.resetwarnings()
# try to open the non-AIFF file as an AIFF file, which should fail, but shouldn't give any ResourceWarnings
import aifc
try: aifc.open(FILE, "rb")
except: pass For the second issue, run this code:
from os import path
FILE = path.expanduser("~/cpython/Lib/test/audiodata/pluck-pcm8.aiff")
# open the file as a file-like object, then open it again with ``aifc.open``
import aifc
with open(FILE, "rb") as f:
assert not f.closed, "Before opening with AIFC"
with aifc.open(f, "rb"):
pass
assert not f.closed, "After opening with AIFC" Expected result For the first code sample, code should give no output - For the second code sample, all assertions should pass - the file should be opened with Actual result For the first code sample: $ python3 test.py
/home/anthony/Desktop/test.py:13: ResourceWarning: unclosed file <_io.BufferedReader name='/home/anthony/Desktop/test.py'>
except: pass In other words, code executes as described in "Expected result", but also prints a warning to stderr about the file object not being closed. For the second code sample: $ python3 "/home/anthony/Desktop/test.py"
Traceback (most recent call last):
File "/home/anthony/Desktop/test.py", line 11, in <module>
assert not f.closed, "After opening with AIFC"
AssertionError: After opening with AIFC In other words, code runs normally until the inner context manager exits, and the file object gets closed, even though the file object wasn't opened by Solution Attached are patches that fix each issue separately - the first patch fixes the first mentioned issue, while the second patch fixes both at once. Backwards compatibility:
from os import path
FILE = path.expanduser("~/cpython/Lib/test/audiodata/pluck-pcm8.aiff")
import aifc
f = open(FILE, "rb")
with aifc.open(f, "rb"):
pass
# with the patch applied, `f` is not closed at this point anymore |
Thank you for your patches Anthony. Could you please create a unittest for the first issue? The second issue is well known. Since the behavior is documented, I think it can be changed only in Python 3.7. The patch needs the "versionchanged" directives in the documentation and documenting the change in What's New (in the section "Porting to Python 3.7" or like). This change can cause reference leaks in existing code. This is not the only inconsistency between aifc and similar modules. |
Anyone mind Python 2.7? Since Python 2.7 doesn't have mock and check_no_resource_warning context manager, I can't cherry-pick easily. Backporting is bother to me, and the issue doesn't seem so critical. |
test.support.swap_attr() provides the functionality similar to mock. check_no_resource_warning() can be backported to 2.7. But if there are problems with testing in 2.7 you can backport without tests. |
OK, since Aifc_write is harder to test, and the exception is very unlikely happens, I fixed only Aifc_read. |
aifc.open
. #162aifc.open
#310aifc.open
#311Misc/NEWS
so that it is managed by towncrier #552Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: