Skip to content

Commit edae491

Browse files
t-reentsGeigerJ2
authored andcommitted
Fix UnboundLocalError in ZipfileBackendRepository (#7129)
If a file/key is missing when importing an archive, the `open` method in `ZipfileBackendRepository` fails with an `UnboundLocalError` because the `handle` variable is referenced before assignment. This was initially discovered when working on the following issue on discourse: https://aiida.discourse.group/t/error-archive-import/696 (cherry picked from commit 166d06c)
1 parent f0132dd commit edae491

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/aiida/storage/sqlite_zip/backend.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,13 +441,15 @@ def list_objects(self) -> Iterable[str]:
441441

442442
@contextmanager
443443
def open(self, key: str) -> Iterator[BinaryIO]:
444+
handle = None
444445
try:
445446
handle = self._zipfile.open(f'{self._folder}/{key}')
446447
yield cast(BinaryIO, handle)
447448
except KeyError:
448449
raise FileNotFoundError(f'object with key `{key}` does not exist.')
449450
finally:
450-
handle.close()
451+
if handle is not None:
452+
handle.close()
451453

452454

453455
class FolderBackendRepository(_RoBackendRepository):

0 commit comments

Comments
 (0)