From d7ea2e16c92d1335285e85857a172b21335e9090 Mon Sep 17 00:00:00 2001 From: Alister Burt Date: Tue, 21 May 2024 14:06:41 -0700 Subject: [PATCH] explicitly clear linecache cache once parsing is finished --- src/starfile/parser.py | 2 ++ tests/test_read_write_round_trip.py | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/starfile/parser.py b/src/starfile/parser.py index c21a8c2..0febed0 100644 --- a/src/starfile/parser.py +++ b/src/starfile/parser.py @@ -1,5 +1,6 @@ from __future__ import annotations +import linecache from collections import deque from io import StringIO from linecache import getline @@ -45,6 +46,7 @@ def __init__( # parse file self.current_line_number = 0 self.parse_file() + linecache.clearcache() @property def current_line(self) -> str: diff --git a/tests/test_read_write_round_trip.py b/tests/test_read_write_round_trip.py index d28bfb3..a86544b 100644 --- a/tests/test_read_write_round_trip.py +++ b/tests/test_read_write_round_trip.py @@ -1,3 +1,7 @@ +import time + +import pandas.testing + from .constants import two_single_line_loop_blocks, postprocess import starfile @@ -35,4 +39,16 @@ def test_round_trip_postprocess(tmp_path): if isinstance(_actual, pd.DataFrame): pd.testing.assert_frame_equal(_actual, _expected, atol=1e-6) else: - assert _actual == _expected \ No newline at end of file + assert _actual == _expected + + +def test_write_read_write_read(): + filename = 'tmp.star' + df_a = pd.DataFrame({'a': [0, 1], 'b': [2, 3]}) + starfile.write(df_a, filename) + + df_b = pd.DataFrame({'c': [0, 1], 'd': [2, 3]}) + starfile.write(df_b, filename) + + df_b_read = starfile.read(filename) + pandas.testing.assert_frame_equal(df_b, df_b_read)