Skip to content

Commit

Permalink
explicitly clear linecache cache once parsing is finished
Browse files Browse the repository at this point in the history
  • Loading branch information
alisterburt committed May 21, 2024
1 parent 0606cf4 commit d7ea2e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/starfile/parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import linecache
from collections import deque
from io import StringIO
from linecache import getline
Expand Down Expand Up @@ -45,6 +46,7 @@ def __init__(
# parse file
self.current_line_number = 0
self.parse_file()
linecache.clearcache()

@property
def current_line(self) -> str:
Expand Down
18 changes: 17 additions & 1 deletion tests/test_read_write_round_trip.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import time

import pandas.testing

from .constants import two_single_line_loop_blocks, postprocess

import starfile
Expand Down Expand Up @@ -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
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)

0 comments on commit d7ea2e1

Please sign in to comment.