Skip to content

Commit

Permalink
Fix closing file after read
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreeve committed May 19, 2020
1 parent 4a5c9df commit edf17f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nptdms/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def __init__(self, tdms_file):
self._index_file_path = index_file_path

def close(self):
if self._file is None:
# Already closed
return

if self._file_path is not None:
# File path was provided so we opened the file and
# should close it.
Expand Down
23 changes: 23 additions & 0 deletions nptdms/test/test_tdms_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,3 +863,26 @@ def test_memory_released_when_tdms_file_out_of_scope():
assert raw_data_ref() is None
assert data_ref() is None
assert chan_ref() is None


def test_close_after_read():
test_file, _ = scenarios.single_segment_with_one_channel().values
temp_file = test_file.get_tempfile(delete=False)
try:
temp_file.file.close()
tdms_data = TdmsFile.read(temp_file.name)
tdms_data.close()
finally:
os.remove(temp_file.name)


def test_multiple_close_after_open():
test_file, _ = scenarios.single_segment_with_one_channel().values
temp_file = test_file.get_tempfile(delete=False)
try:
temp_file.file.close()
with TdmsFile.open(temp_file.name) as tdms_data:
tdms_data.close()
tdms_data.close()
finally:
os.remove(temp_file.name)

0 comments on commit edf17f4

Please sign in to comment.