Skip to content

Fix context handling, stale test dirs, and resource leak #37

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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added fixtures/testfile.tar.gz
Binary file not shown.
16 changes: 9 additions & 7 deletions libarchive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import pathlib
import stat
import sys
import time
Expand Down Expand Up @@ -200,7 +201,7 @@ def __enter__(self):
return self

def __exit__(self, *args):
return
self.close()

def __iter__(self):
if self.closed:
Expand Down Expand Up @@ -438,17 +439,20 @@ def __init__(
self.encoding = encoding
self.blocksize = blocksize
self.password = password
if isinstance(f, pathlib.PurePath):
f = str(f)
if isinstance(f, str):
self.filename = f
f = open(f, mode)
# Only close it if we opened it...
self._defer_close = True
self._doclose = True
elif hasattr(f, 'fileno'):
self.filename = getattr(f, 'name', None)
# Leave the fd alone, caller should manage it...
self._defer_close = False
self._doclose = False
else:
raise Exception('Provided file is not path or open file.')
self._defer_close = False
self.f = f
self.mode = mode
# Guess the format/filter from file name (if not provided)
Expand Down Expand Up @@ -493,7 +497,7 @@ def __enter__(self):
return self

def __exit__(self, type, value, traceback):
self.denit()
self.close()

def __del__(self):
self.close()
Expand Down Expand Up @@ -563,7 +567,7 @@ def close(self, _defer=False):
if hasattr(self.f, "fileno"):
os.fsync(self.f.fileno())
# and then close it, if we opened it...
if getattr(self, '_close', None):
if self._doclose and getattr(self.f, 'close', None):
self.f.close()

@property
Expand Down Expand Up @@ -665,8 +669,6 @@ def __init__(self, f, **kwargs):
self._stream = None
# Convert file to open file. We need this to reopen the archive.
mode = kwargs.setdefault('mode', 'r')
if isinstance(f, str):
f = open(f, mode)
super(SeekableArchive, self).__init__(f, **kwargs)
self.entries = []
self.eof = False
Expand Down
Loading