This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author azhang
Recipients azhang
Date 2016-12-30.00:55:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>

This shows up as two closely-related issues:

* ```` leaks file object when invalid AIFF file encountered. This is probably a bug.
* ``aifc.close`` closes file object even when it didn't open the file object to begin with. While this is technically documented behaviour [1], it's inconsistent with how similar modules like ``wave`` work [2].

I have confirmed the issue is present in all the selected Python versions.

Steps to reproduce

For the first issue, run this code:

    #!/usr/bin/env python3

    # 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

    # try to open the non-AIFF file as an AIFF file, which should fail, but shouldn't give any ResourceWarnings
    import aifc
    try:, "rb")
    except: pass

For the second issue, run this code:

    #!/usr/bin/env python3

    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 ````
    import aifc
    with open(FILE, "rb") as f:
        assert not f.closed, "Before opening with AIFC"
        with, "rb"):
        assert not f.closed, "After opening with AIFC"

Expected result

For the first code sample, code should give no output - ```` should throw an exception due to the passed filename being an invalid AIFF file, but that exception should be caught and suppressed. by the ``try``/``except`` statements.

For the second code sample, all assertions should pass - the file should be opened with ``open``, "opened" again with ````, and should not be closed when the inner context manager exits.

Actual result

For the first code sample:

    $ python3
    /home/anthony/Desktop/ ResourceWarning: unclosed file <_io.BufferedReader name='/home/anthony/Desktop/'>
      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/"
    Traceback (most recent call last):
      File "/home/anthony/Desktop/", 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 ``aifc``.


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:

* The first patch, "fix_aifc_leak.patch", makes no functionality changes, so there shouldn't be any backwards compatibility concerns.
* The second patch, "fix_aifc_leak_and_file_object_close.patch", slightly changes the behaviour of `aifc_instance.close()` so that it closes in only a subset of cases it would originally. While it is possible for this to lead to leaks in certain cases (example below), the impact shoul be low, as existing codebases seem to use context managers and similar constructs that clean everything up properly.

    #!/usr/bin/env python3

    from os import path
    FILE = path.expanduser("~/cpython/Lib/test/audiodata/pluck-pcm8.aiff")
    import aifc
    f = open(FILE, "rb")
    with, "rb"):
    # with the patch applied, `f` is not closed at this point anymore

Date User Action Args
2016-12-30 00:55:17azhangsetrecipients: + azhang
2016-12-30 00:55:17azhangsetmessageid: <>
2016-12-30 00:55:17azhanglinkissue29110 messages
2016-12-30 00:55:16azhangcreate