Skip to content

Commit

Permalink
fix: fix for dask 2024.8 (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 authored Sep 11, 2024
1 parent a515efd commit d04df9b
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/nd2/nd2file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import inspect
import threading
import warnings
from itertools import product
Expand Down Expand Up @@ -189,11 +190,19 @@ def __del__(self) -> None:
"""Delete file handle on garbage collection."""
# if it came in as an open file handle, it's ok to remain open after deletion
if not getattr(self, "closed", True) and not self._rdr._was_open:
warnings.warn(
"ND2File file not closed before garbage collection. "
"Please use `with ND2File(...):` context or call `.close()`.",
stacklevel=2,
)
# this stack inspection is a hack to avoid an unnecessary warning/closure.
# when using the to_dask() method, calling dask map_blocks will greedily
# pickle/unpickle the object.
# that will trigger a call to __getstate__ which calls del state["_rdr"]
# which results in a call to __del__. We avoid that by checking this
# special case here... I'm not sure it's the correct approach, but it works.
stack = inspect.stack()
if len(stack) < 2 or stack[1].function != "_normalize_pickle":
warnings.warn(
"ND2File file not closed before garbage collection. "
"Please use `with ND2File(...):` context or call `.close()`.",
stacklevel=2,
)
self._rdr.close()

def __exit__(self, *_: Any) -> None:
Expand All @@ -215,7 +224,6 @@ def __setstate__(self, d: dict[str, Any]) -> None:
self.__dict__ = d
self._lock = threading.RLock()
self._rdr = ND2Reader.create(self._path, self._error_radius)

if _was_closed:
self._rdr.close()

Expand Down

0 comments on commit d04df9b

Please sign in to comment.