Skip to content

Commit

Permalink
fix some extra warnings in ZipStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
luizirber committed May 10, 2021
1 parent de03213 commit c58302e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/sourmash/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ def index(args):

notify('loaded {} sigs; saving SBT under "{}"', n, args.sbt_name)
tree.save(args.sbt_name, sparseness=args.sparseness)
if tree.storage:
tree.storage.close()


def search(args):
Expand Down
15 changes: 9 additions & 6 deletions src/sourmash/sbt_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,10 @@ def init_args(self):
return {'path': self.path}

def close(self):
self.flush()
if self.zipfile is not None:
# TODO: this is not ideal; checking for zipfile.fp is looking at
# internal implementation details from CPython...
if self.zipfile is not None and self.zipfile.fp:
self.flush()
self.zipfile.close()
self.zipfile = None

Expand All @@ -189,9 +191,10 @@ def flush(self):

if self.bufferzip is None:
# The easy case: close (to force flushing) and reopen the zipfile
self.zipfile.close()
self.zipfile = zipfile.ZipFile(self.path, mode='a',
compression=zipfile.ZIP_STORED)
if self.zipfile is not None:
self.zipfile.close()
self.zipfile = zipfile.ZipFile(self.path, mode='a',
compression=zipfile.ZIP_STORED)
else:
# The complicated one. Need to consider:
# - Is there data in the buffer?
Expand All @@ -208,7 +211,7 @@ def flush(self):
# bad news, need to create new file...
# create a temporary file to write the final version,
# which will be copied to the right place later.
tempfile = NamedTemporaryFile()
tempfile = NamedTemporaryFile(delete=False)
final_file = zipfile.ZipFile(tempfile, mode="w")
all_data = buffer_names.union(zf_names)

Expand Down

0 comments on commit c58302e

Please sign in to comment.