Skip to content

ZipExtFile provides no mechanism for closing the underlying file object #54055

Closed

Description

@johnadmanski
BPO 9846
Nosy @jcea, @pitrou, @merwok, @bitdancer, @briancurtin, @ambv
Files
  • issue9846.diff: ZipExtFile now can optionally close the passed in fileobj as well
  • Note: 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:

    assignee = None
    closed_at = <Date 2010-11-22.23:35:02.166>
    created_at = <Date 2010-09-13.16:17:15.714>
    labels = ['type-feature', 'library']
    title = 'ZipExtFile provides no mechanism for closing the underlying file object'
    updated_at = <Date 2012-11-04.01:33:45.455>
    user = 'https://bugs.python.org/johnadmanski'

    bugs.python.org fields:

    activity = <Date 2012-11-04.01:33:45.455>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-11-22.23:35:02.166>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2010-09-13.16:17:15.714>
    creator = 'john.admanski'
    dependencies = []
    files = ['19778']
    hgrepos = []
    issue_num = 9846
    keywords = ['patch']
    message_count = 10.0
    messages = ['116322', '116329', '116356', '116361', '116395', '120075', '122176', '122177', '174733', '174736']
    nosy_count = 8.0
    nosy_names = ['jcea', 'pitrou', 'eric.araujo', 'r.david.murray', 'brian.curtin', 'lukasz.langa', 'john.admanski', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'accepted'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue9846'
    versions = ['Python 3.2']

    Activity

    1. johnadmanski commented on Sep 13, 2010

      johnadmanskimannequin
      MannequinAuthor

      When creating a ZipExtFile through ZipFile.open, the if the original ZipFile object was created with a filename then a new file object will be opened and given to the ZipExtFile to use for its file operations. There is no explicit mechanism that will allow you to release this file.

      ZipExtFile does have an (undocumented?) close method, but this won't actually close the file object that underlies it; probably because it has no way of knowing if it actually owns the file object or if it just has a reference to a file object that belongs to the ZipFile.

      You can work around this by destroying references to the ZipExtFile and letting the file's __del__ handle the close for you but this relies on implementation details of ZipExtFile.

      Found in Python 3.1 but a look at svn.python.org suggests that this problem is still there in 2.7 and 3.2; however, I haven't actually tried it with them.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Sep 13, 2010
    3. pitrou commented on Sep 13, 2010

      @pitrou
      Member

      Indeed. However, the ZipFile itself knows whether to close the file (self._filePassed). By adding a constructor argument to ZipExtFile and a __del__ method, it would solve the issue.

    4. johnadmanski commented on Sep 14, 2010

      johnadmanskimannequin
      MannequinAuthor

      I'm not sure that's much of an improvement on the existing behavior, though; as I mentioned you can already work around it by killing all the references to the ZipExtFile and the underlying file object will get automatically closed if the ZipExtFile is the only object using it, so adding an explicit __del__ that does the same thing isn't much of an improvement.

      Since ZipExtFile is supposed to be a file-like object (albeit a read-only one) it would make sense to be able to release the underlying resources the same way you normally do with a file...by closing it.

    5. bitdancer commented on Sep 14, 2010

      @bitdancer
      Member

      Antoine, any reason not to put the close in the ZipFileExt close method instead of a __del__ method? (And document it, of course).

    6. pitrou commented on Sep 14, 2010

      @pitrou
      Member

      Antoine, any reason not to put the close in the ZipFileExt close method
      instead of a __del__ method? (And document it, of course).

      You are right, a close() method would be enough.
      Furthermore, ZipExtFile already supports the context manager protocol (implicitly calling BufferedIOBase.close()), so you will simply be able to write:

      with myzipfile.open("README", "r") as f:
          # ...
    7. briancurtin commented on Oct 31, 2010

      @briancurtin
      Member

      A fix to this would help silence a number of ResourceWarning messages coming out of the test suite.

    8. ambv commented on Nov 22, 2010

      @ambv
      Contributor

      Adding a patch implementing the discussed functionality, removing almost all of the ResourceWarnings raised by zipfile.

    9. ambv commented on Nov 22, 2010

      @ambv
      Contributor

      Committed in rev 86699.

    10. jcea commented on Nov 4, 2012

      @jcea
      Member

      This code was not backported to python 2.7. See Issue bpo-16183

    11. python-dev commented on Nov 4, 2012

      python-devmannequin
      Mannequin

      New changeset 6a14f692df1c by Jesus Cea in branch '2.7':
      Closes bpo-16183: ZipExtFile object close without file handle closed (backporting of Issue bpo-9846)
      http://hg.python.org/cpython/rev/6a14f692df1c

    12. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions