Skip to content

Commit

Permalink
Add missing methods to SubTextIO
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Aug 6, 2024
1 parent 7420192 commit 1e63277
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/multicsv/subtextio.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from typing import TextIO, List, Optional, Type, Iterable
import os

Expand Down Expand Up @@ -116,6 +115,26 @@ def __init__(self, base_io: TextIO, start: int, end: int):
def mode(self) -> str:
return self.base_io.mode

@property
def closed(self) -> bool:
return self._closed

@property
def name(self) -> str:
return self.base_io.name

@property
def encoding(self) -> str:
return self.base_io.encoding

@property
def errors(self) -> Optional[str]:
return None

@property
def line_buffering(self) -> int:
return self.base_io.line_buffering

def read(self, size: int = -1) -> str:
if self._closed:
raise ValueError("I/O operation on closed file.")
Expand Down Expand Up @@ -241,6 +260,21 @@ def flush(self) -> None:
self.base_io.write(content_before + self._buffer + content_after)
self.base_io.flush()

def isatty(self) -> bool:
return False

def fileno(self) -> int:
return self.base_io.fileno()

def readable(self) -> bool:
return self.base_io.readable()

def writable(self) -> bool:
return self.base_io.writable()

def seekable(self) -> bool:
return self.base_io.seekable()

def __iter__(self) -> 'SubTextIO':
return self

Expand Down
48 changes: 48 additions & 0 deletions tests/test_subtextio.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ def test_long_write(base_textio):
sub_text.seek(0)
assert sub_text.read(len(long_text)) == long_text

def test_closed(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.closed is False
sub_text.close()
assert sub_text.closed is True

def test_closed_after_context(base_textio):
with SubTextIO(base_textio, start=6, end=21) as sub_text:
assert sub_text.closed is False
assert sub_text.closed is True

# Test seek and read after close
def test_operations_after_close(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
Expand Down Expand Up @@ -298,3 +309,40 @@ def test_readlines_with_huge_hint(base_textio):
def test_readlines_with_zero_hint(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.readlines(hint=0) == ["World,\n"]

def test_encoding(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.encoding == base_textio.encoding

def test_errors(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.errors is None

def test_line_buffering(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert not sub_text.line_buffering

def test_newlines(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.newlines is None

def test_isatty(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert not sub_text.isatty()

def test_fileno(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
with pytest.raises(io.UnsupportedOperation):
sub_text.fileno()

def test_readable(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.readable()

def test_writable(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.writable()

def test_seekable(base_textio):
sub_text = SubTextIO(base_textio, start=6, end=21)
assert sub_text.seekable()

0 comments on commit 1e63277

Please sign in to comment.