Skip to content

zipfile's readline() drops data in universal newline mode #64247

Closed

Description

@abalkin
BPO 20048
Nosy @abalkin, @serhiy-storchaka
Dependencies
  • bpo-14371: Add support for bzip2 compression to the zipfile module
  • Files
  • zipfile_peek.patch
  • 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 2013-12-21.21:55:42.140>
    created_at = <Date 2013-12-21.20:58:59.324>
    labels = ['type-bug', 'library']
    title = "zipfile's readline() drops data in universal newline mode"
    updated_at = <Date 2013-12-21.21:55:42.139>
    user = 'https://github.com/abalkin'

    bugs.python.org fields:

    activity = <Date 2013-12-21.21:55:42.139>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-12-21.21:55:42.140>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2013-12-21.20:58:59.324>
    creator = 'belopolsky'
    dependencies = ['14371']
    files = ['33253']
    hgrepos = []
    issue_num = 20048
    keywords = ['patch']
    message_count = 5.0
    messages = ['206779', '206784', '206785', '206788', '206789']
    nosy_count = 4.0
    nosy_names = ['belopolsky', 'alanmcintyre', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue20048'
    versions = ['Python 2.7']

    Activity

    1. abalkin commented on Dec 21, 2013

      @abalkin
      MemberAuthor

      This problem happens when I unpack a file from a 200+ MB zip archive as follows:

      with zipfile.ZipFile(archive) as z:
          data = b''
          with z.open(filename, 'rU') as f:
              for line in f:
            	    data += line

      I cannot reduce it to a test case suitable for posting here, but the culprit is the following code in zipfile.py:

          def peek(self, n=1):
              """Returns buffered bytes without advancing the position."""
              if n > len(self._readbuffer) - self._offset:
                  chunk = self.read(n)
                  self._offset -= len(chunk)

      See http://hg.python.org/cpython/file/81f8375e60ce/Lib/zipfile.py#l605

      The problem occurs when peek() is called on the boundary of the uncompress buffer and read() goes through more than one readbuffer. The result is that self._offset is smaller than len(chunk) leading to a non-sensical negative self._offset upon return from peek().

      This problem does not seem to appear in 3.x since 028e8e0b03e8.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Dec 21, 2013
    3. serhiy-storchaka commented on Dec 21, 2013

      @serhiy-storchaka
      Member

      Does this patch fix a bug?

    4. abalkin commented on Dec 21, 2013

      @abalkin
      MemberAuthor

      It does!

    5. python-dev commented on Dec 21, 2013

      python-devmannequin
      Mannequin

      New changeset 8b097d07488d by Serhiy Storchaka in branch '2.7':
      Issue bpo-20048: Fixed ZipExtFile.peek() when it is called on the boundary of
      http://hg.python.org/cpython/rev/8b097d07488d

    6. serhiy-storchaka commented on Dec 21, 2013

      @serhiy-storchaka
      Member

      Than you for your report and irrefragable analysis.

    7. 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-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions