From dc4b5c33e4b68cf4f1dc0cf639eac188f9b83f57 Mon Sep 17 00:00:00 2001 From: Ruben Vorderman Date: Sun, 24 Dec 2023 10:36:41 +0100 Subject: [PATCH] Prevent operations on raw file before it is successfully opened if error is thrown during __init__ --- src/isal/igzip_threaded.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/isal/igzip_threaded.py b/src/isal/igzip_threaded.py index 99eafc17..adb2c24d 100644 --- a/src/isal/igzip_threaded.py +++ b/src/isal/igzip_threaded.py @@ -204,6 +204,9 @@ def __init__(self, queue_size: int = 1, block_size: int = 1024 * 1024, ): + # File should be closed during init, so __exit__ method does not + # touch the self.raw value before it is initialized. + self._closed = True if "t" in mode or "r" in mode: raise ValueError("Only binary writing is supported") if "b" not in mode: @@ -243,8 +246,8 @@ def __init__(self, self._crc = 0 self.running = False self._size = 0 - self._closed = False self.raw = open_as_binary_stream(filename, mode) + self._closed = False self._write_gzip_header() self.start()