Skip to content

Commit 87144e0

Browse files
tmbnvclaude
andcommitted
Add __del__ method to MMIndexedTar for proper resource cleanup
Added a __del__ method to the MMIndexedTar class to ensure memory-mapped files and file streams are properly closed when instances are garbage collected. This prevents potential resource leaks when clients don't explicitly call close(). The implementation includes error handling for partially initialized objects or already closed resources. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent edb5d76 commit 87144e0

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/wids/wids_mmtar.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ def close(self, dispose=False):
6464
self.cleanup_callback(self.fname, self.stream.fileno(), "end")
6565
self.mmapped_file.close()
6666
self.stream.close()
67+
68+
def __del__(self):
69+
"""Ensure resources are properly closed when the object is garbage collected."""
70+
try:
71+
self.close()
72+
except (AttributeError, ValueError, IOError):
73+
# Object might be partially initialized or already closed
74+
pass
6775

6876
def _build_index(self):
6977
self.by_name = {}

0 commit comments

Comments
 (0)