Skip to content

Commit

Permalink
check file pointer reset
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Sep 15, 2024
1 parent 6102263 commit e9c22ed
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/monty/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def _get_line_ending(
TODO:
- Read the last N chars instead of the entire line?
- Unit test assert file.tell() is at start of file
"""
if isinstance(file, (str, Path)):
with zopen(file, "rb") as f:
Expand Down Expand Up @@ -200,7 +199,7 @@ def reverse_readline(
Args:
m_file: File stream to read (backwards).
blk_size (int): The block size to read each time in bytes.
Defaults to 4096. # TODO: unclear what this actually controls?
Defaults to 4096.
max_mem (int): Threshold to determine when to reverse a file
in-memory versus reading blocks of a file each time.
For bz2 files, this sets the block size.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ def test_get_line_ending(self, l_end):

# Test text mode
with open(test_file, "r", encoding="utf-8") as f:
start_pot = f.tell()
assert _get_line_ending(f) == l_end
assert f.tell() == start_pot

# Test binary mode
with open(test_file, "rb") as f:
start_pot = f.tell()
assert _get_line_ending(f) == l_end
assert f.tell() == start_pot

# Test gzip file
gzip_filename = f"{test_file}.gz"
Expand All @@ -56,7 +60,9 @@ def test_get_line_ending(self, l_end):

# Opened file stream
with gzip.open(gzip_filename, "rb") as f:
start_pot = f.tell()
assert _get_line_ending(f) == l_end
assert f.tell() == start_pot

# Filename directly
assert _get_line_ending(gzip_filename) == l_end
Expand All @@ -68,7 +74,9 @@ def test_get_line_ending(self, l_end):

# Opened file stream
with bz2.open(bz2_filename, "rb") as f:
start_pot = f.tell()
assert _get_line_ending(f) == l_end
assert f.tell() == start_pot

# Filename directly
assert _get_line_ending(bz2_filename) == l_end
Expand Down

0 comments on commit e9c22ed

Please sign in to comment.