Skip to content

Commit

Permalink
break cyclic ref with weakref
Browse files Browse the repository at this point in the history
  • Loading branch information
pfackeldey committed Dec 16, 2024
1 parent dc4a7d6 commit d4893d3
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/awkward/_attrs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
from __future__ import annotations

import weakref
from collections.abc import Mapping
from types import MappingProxyType

Expand Down Expand Up @@ -47,14 +48,18 @@ def without_transient_attrs(attrs: dict[str, Any]) -> JSONMapping:

class Attrs(Mapping):
def __init__(self, ref, data: Mapping[str, Any]):
self._ref = ref
self._ref = weakref.ref(ref)
self._data = _freeze_attrs(data)

def __getitem__(self, key: str):
return self._data[key]

def __setitem__(self, key: str, value: Any):
self._ref._attrs = _unfreeze_attrs(self._data) | {key: value}
ref = self._ref()
if ref is None:
msg = "The reference array has been deleted. If you still need to set attributes, convert this 'Attrs' instance to a dict with '.to_dict()'."
raise ValueError(msg)
ref._attrs = _unfreeze_attrs(self._data) | {key: value}

def __iter__(self):
return iter(self._data)
Expand All @@ -63,7 +68,10 @@ def __len__(self):
return len(self._data)

def __repr__(self):
return repr(_unfreeze_attrs(self._data))
return f"Attrs({_unfreeze_attrs(self._data)!r})"

def to_dict(self):
return _unfreeze_attrs(self._data)


def _freeze_attrs(attrs: Mapping[str, Any]) -> Mapping[str, Any]:
Expand Down

0 comments on commit d4893d3

Please sign in to comment.