- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 33.3k
 
gh-121639: Deduplicate reinitialization setup for ZipExtFile when seeking #121640
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
          
     Open
      
      
            danifus
  wants to merge
  1
  commit into
  python:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
danifus:gh-121639
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| 
          
            
          
           | 
    @@ -875,19 +875,12 @@ class ZipExtFile(io.BufferedIOBase): | |||||
| 
     | 
||||||
| def __init__(self, fileobj, mode, zipinfo, pwd=None, | ||||||
| close_fileobj=False): | ||||||
| self._zinfo = zipinfo | ||||||
| self._fileobj = fileobj | ||||||
| self._pwd = pwd | ||||||
| self._close_fileobj = close_fileobj | ||||||
| 
     | 
||||||
| self._compress_type = zipinfo.compress_type | ||||||
| self._compress_left = zipinfo.compress_size | ||||||
| self._left = zipinfo.file_size | ||||||
| 
     | 
||||||
| self._decompressor = _get_decompressor(self._compress_type) | ||||||
| 
     | 
||||||
| self._eof = False | ||||||
| self._readbuffer = b'' | ||||||
| self._offset = 0 | ||||||
| 
     | 
||||||
| self.newlines = None | ||||||
| 
     | 
||||||
| 
        
          
        
         | 
    @@ -896,34 +889,20 @@ def __init__(self, fileobj, mode, zipinfo, pwd=None, | |||||
| 
     | 
||||||
| if hasattr(zipinfo, 'CRC'): | ||||||
| self._expected_crc = zipinfo.CRC | ||||||
| self._running_crc = crc32(b'') | ||||||
| self._orig_start_crc = crc32(b'') | ||||||
| else: | ||||||
| self._expected_crc = None | ||||||
| self._orig_start_crc = None | ||||||
| 
     | 
||||||
| self._seekable = False | ||||||
| try: | ||||||
| if fileobj.seekable(): | ||||||
| self._orig_compress_start = fileobj.tell() | ||||||
| self._orig_compress_size = zipinfo.compress_size | ||||||
| self._orig_file_size = zipinfo.file_size | ||||||
| self._orig_start_crc = self._running_crc | ||||||
| self._orig_crc = self._expected_crc | ||||||
| self._seekable = True | ||||||
| except AttributeError: | ||||||
| pass | ||||||
| 
     | 
||||||
| self._decrypter = None | ||||||
| if pwd: | ||||||
| if zipinfo.flag_bits & _MASK_USE_DATA_DESCRIPTOR: | ||||||
| # compare against the file type from extended local headers | ||||||
| check_byte = (zipinfo._raw_time >> 8) & 0xff | ||||||
| else: | ||||||
| # compare against the CRC otherwise | ||||||
| check_byte = (zipinfo.CRC >> 24) & 0xff | ||||||
| h = self._init_decrypter() | ||||||
| if h != check_byte: | ||||||
| raise RuntimeError("Bad password for file %r" % zipinfo.orig_filename) | ||||||
| 
     | 
||||||
| self._init_read() | ||||||
| 
     | 
||||||
| def _init_decrypter(self): | ||||||
| self._decrypter = _ZipDecrypter(self._pwd) | ||||||
| 
        
          
        
         | 
    @@ -934,7 +913,30 @@ def _init_decrypter(self): | |||||
| # and is used to check the correctness of the password. | ||||||
| header = self._fileobj.read(12) | ||||||
| self._compress_left -= 12 | ||||||
| return self._decrypter(header)[11] | ||||||
| h = self._decrypter(header)[11] | ||||||
| 
     | 
||||||
| if self._zinfo.flag_bits & _MASK_USE_DATA_DESCRIPTOR: | ||||||
| # compare against the file type from extended local headers | ||||||
| check_byte = (self._zinfo._raw_time >> 8) & 0xff | ||||||
| else: | ||||||
| # compare against the CRC otherwise | ||||||
| check_byte = (self._zinfo.CRC >> 24) & 0xff | ||||||
| if h != check_byte: | ||||||
| raise RuntimeError("Bad password for file %r" % self._zinfo.orig_filename) | ||||||
| return h | ||||||
| 
     | 
||||||
| def _init_read(self): | ||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
  | 
||||||
| self._running_crc = self._orig_start_crc | ||||||
| self._compress_left = self._zinfo.compress_size | ||||||
| self._left = self._zinfo.file_size | ||||||
| self._readbuffer = b'' | ||||||
| self._offset = 0 | ||||||
| self._eof = False | ||||||
| if self._pwd: | ||||||
| self._init_decrypter() | ||||||
| else: | ||||||
| self._decrypter = None | ||||||
| self._decompressor = _get_decompressor(self._compress_type) | ||||||
| 
     | 
||||||
| def __repr__(self): | ||||||
| result = ['<%s.%s' % (self.__class__.__module__, | ||||||
| 
          
            
          
           | 
    @@ -1174,17 +1176,8 @@ def seek(self, offset, whence=os.SEEK_SET): | |||||
| elif read_offset < 0: | ||||||
| # Position is before the current position. Reset the ZipExtFile | ||||||
| self._fileobj.seek(self._orig_compress_start) | ||||||
| self._running_crc = self._orig_start_crc | ||||||
| self._expected_crc = self._orig_crc | ||||||
| self._compress_left = self._orig_compress_size | ||||||
| self._left = self._orig_file_size | ||||||
| self._readbuffer = b'' | ||||||
| self._offset = 0 | ||||||
| self._decompressor = _get_decompressor(self._compress_type) | ||||||
| self._eof = False | ||||||
| self._init_read() | ||||||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
  | 
||||||
| read_offset = new_pos | ||||||
| if self._decrypter is not None: | ||||||
| self._init_decrypter() | ||||||
| 
     | 
||||||
| while read_offset > 0: | ||||||
| read_len = min(self.MAX_SEEK_READ, read_offset) | ||||||
| 
          
            
          
           | 
    ||||||
        
          
          
            2 changes: 2 additions & 0 deletions
          
          2 
        
  Misc/NEWS.d/next/Library/2024-07-12-15-48-29.gh-issue-121639.PL_5Cg.rst
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add ``zipfile.ZipExtFile._init_read()`` to deduplicate read | ||
| (re)initialization in ``seek()``. | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"init" implies there is some one time setup/initialization going on to me. I'd suggest maybe
_reset_read_state