Skip to content

Commit

Permalink
Add __del__ handling to SubTextIO
Browse files Browse the repository at this point in the history
  • Loading branch information
Donaim committed Aug 7, 2024
1 parent 85409f3 commit 2cc6bd2
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/multicsv/subtextio.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ class allows for convenient and isolated operations within a given
"""

def __init__(self, base_io: TextIO, start: int, end: int):
self._initialized = False
self._base_io = base_io
self._start = start
self._end = end
self._position = 0 # Position within the SubTextIO
self._closed = base_io.closed
self._buffer = ""

if end < start or start < 0:
raise InvalidSubtextCoordinates(
f"Invalid range [{start},{end}] passed to SubTextIO.")
Expand All @@ -116,13 +124,9 @@ def __init__(self, base_io: TextIO, start: int, end: int):
raise BaseMustBeReadable("Base io must be readable"
" if existing content is to be modified.")

self._base_io = base_io
self._start = start
self._end = end
self._position = 0 # Position within the SubTextIO
self._closed = base_io.closed
self._load()
self._initial_length = self.buffer_length
self._initialized = True

def _load(self) -> None:
"""
Expand Down Expand Up @@ -350,3 +354,10 @@ def __exit__(self,
exc_val: Optional[BaseException],
exc_tb: Optional[object]) -> None:
self.close()

def __del__(self) -> None:
if self._initialized:
try:
self.close()
except BaseIOClosed:
pass

0 comments on commit 2cc6bd2

Please sign in to comment.