Skip to content

Commit

Permalink
Use weakref for filemanager
Browse files Browse the repository at this point in the history
  • Loading branch information
ivirshup committed Sep 4, 2023
1 parent a9a10af commit ca0759a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion anndata/_core/file_backing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from typing import Optional, Union, Iterator, Literal
from collections.abc import Mapping
import weakref

import h5py

Expand All @@ -20,13 +21,26 @@ def __init__(
filename: Optional[PathLike] = None,
filemode: Optional[Literal["r", "r+"]] = None,
):
self._adata = adata
self._adata_ref = weakref.ref(adata)
self.filename = filename
self._filemode = filemode
self._file = None
if filename:
self.open()

def __getstate__(self):
state = self.__dict__.copy()
state["_adata_ref"] = state["_adata_ref"]()
return state

def __setstate__(self, state):
self.__dict__ = state.copy()
self.__dict__["_adata_ref"] = weakref.ref(state["_adata_ref"])

@property
def _adata(self):
return self._adata_ref()

def __repr__(self) -> str:
if self.filename is None:
return "Backing file manager: no file is set."
Expand Down

0 comments on commit ca0759a

Please sign in to comment.