Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix for dask 2024.8 #240

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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