diff --git a/nrrd/reader.py b/nrrd/reader.py index 4be406b..6692f5b 100644 --- a/nrrd/reader.py +++ b/nrrd/reader.py @@ -387,7 +387,8 @@ def read_data(header: NRRDHeader, fh: Optional[IO] = None, filename: Optional[st else: # Must close the file because if the file was opened above from detached filename, there is no "with" block to # close it for us - fh.close() + if data_filename is not None: + fh.close() raise NRRDError('Invalid lineskip, allowed values are greater than or equal to 0') @@ -395,7 +396,8 @@ def read_data(header: NRRDHeader, fh: Optional[IO] = None, filename: Optional[st if byte_skip < -1: # Must close the file because if the file was opened above from detached filename, there is no "with" block to # close it for us - fh.close() + if data_filename is not None: + fh.close() raise NRRDError('Invalid byteskip, allowed values are greater than or equal to -1') elif byte_skip >= 0: @@ -428,7 +430,8 @@ def read_data(header: NRRDHeader, fh: Optional[IO] = None, filename: Optional[st else: # Must close the file because if the file was opened above from detached filename, there is no "with" block # to close it for us - fh.close() + if data_filename is not None: + fh.close() raise NRRDError(f'Unsupported encoding: {header["encoding"]}') @@ -461,8 +464,9 @@ def read_data(header: NRRDHeader, fh: Optional[IO] = None, filename: Optional[st # NumPy data = np.frombuffer(decompressed_data[byte_skip:], dtype) - # Close the file, even if opened using "with" block, closing it manually does not hurt - fh.close() + # Close the file if we opened it + if data_filename is not None: + fh.close() if total_data_points != data.size: raise NRRDError(f'Size of the data does not equal the product of all the dimensions: ' diff --git a/nrrd/tests/test_reading.py b/nrrd/tests/test_reading.py index ba3b9da..4721458 100644 --- a/nrrd/tests/test_reading.py +++ b/nrrd/tests/test_reading.py @@ -517,6 +517,12 @@ def test_read_space_directions_list(self): finally: nrrd.SPACE_DIRECTIONS_TYPE = 'double matrix' + def test_file_parameter_not_closed(self): + with open(RAW_NRRD_FILE_PATH, 'rb') as fh: + header = nrrd.read_header(fh) + nrrd.read_data(header, fh) + self.assertFalse(fh.closed) + class TestReadingFunctionsFortran(Abstract.TestReadingFunctions): index_order = 'F'