Skip to content
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

IndexError / Worker initialization sets wrong last_file_index #534

Open
baterflyrity opened this issue Sep 29, 2023 · 2 comments
Open

IndexError / Worker initialization sets wrong last_file_index #534

baterflyrity opened this issue Sep 29, 2023 · 2 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@baterflyrity
Copy link
Contributor

Describe the bug

During initialization Worker sets instance field self.last_file_index = len(self.files) which is totally wrong for non-empty list of files. Lately when working with archive this bug raises IndexError: list index out of range.

Such behavior is caused by side-effects while working with Worker hence on second and further initializations it becomes corrupted.

To Reproduce

import py7zr
with py7zr.SevenZipFile('test.7z', 'w') as archive:
	archive.writestr('1', '1.txt')
	archive.test()

Traceback (most recent call last):
File "1.py", line 99, in
with py7zr.SevenZipFile('test.7z', 'w') as archive:
File "C:\Python311\Lib\site-packages\py7zr\py7zr.py", line 417, in exit
self.close()
File "C:\Python311\Lib\site-packages\py7zr\py7zr.py", line 1110, in close
self._write_flush()
File "C:\Python311\Lib\site-packages\py7zr\py7zr.py", line 697, in _write_flush
self.worker.flush_archive(self.fp, folder)
File "C:\Python311\Lib\site-packages\py7zr\py7zr.py", line 1512, in flush_archive
if "maxsize" in self.header.files_info.files[self.last_file_index]:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
IndexError: list index out of range

Expected behavior

image

Environment:

  • OS: Windows 10
  • Python 3.11.5
  • py7zr version: 0.20.6

Current workaround

Just create new wrapper per each archive operation:

import py7zr
with py7zr.SevenZipFile('test.7z', 'w') as archive:
	archive.writestr('1', '1.txt')
with py7zr.SevenZipFile('test.7z', 'r') as archive:
	archive.test()

Possible fixes

  1. Correctly initialize worker.
self.last_file_index = len(self.files) - 1
  1. Alternative bad solution is to prevent Worker reinitialization. On of many approaches:
class SevenZipFile:
	def __init__(self, ...):
		self._worker = None
		...

	@property
	def worker(self):
		if not self._worker:
			raise NotInitializedError(...)
		return self._worker
	
	@worker.setter
	def worker(self, value):
		if self._worker:
			raise AlreadyInitializedError(...)
		self._worker = value
@miurahr
Copy link
Owner

miurahr commented Sep 30, 2023

@baterflyrity you are welcome raising pull-request to fix the issue.
I want to see reproducible test case and the proposal fix it.

@miurahr miurahr added bug Something isn't working help wanted Extra attention is needed labels Sep 30, 2023
baterflyrity added a commit to baterflyrity/py7zr that referenced this issue Sep 30, 2023
baterflyrity added a commit to baterflyrity/py7zr that referenced this issue Sep 30, 2023
@baterflyrity
Copy link
Contributor Author

@baterflyrity you are welcome raising pull-request to fix the issue. I want to see reproducible test case and the proposal fix it.

Sure, I will try.

@miurahr miurahr added the good first issue Good for newcomers label Sep 30, 2023
miurahr added a commit that referenced this issue Nov 2, 2023
…ex (#535)

* Run workflow

Add empty line

* Add test for issue 534

#534

* Fix test for multiple archive usages.

* Fix issue 534

#534

* Update tests/test_basic.py

Black style fix.

Co-authored-by: Hiroshi Miura <miurahr@linux.com>

* Update tests/test_basic.py

---------

Co-authored-by: Hiroshi Miura <miurahr@linux.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants