Skip to content

Commit f97fa03

Browse files
authored
MNT: Move constructors to a direct style (#773)
1 parent 47b246b commit f97fa03

File tree

5 files changed

+306
-486
lines changed

5 files changed

+306
-486
lines changed

sparse/mlir_backend/_common.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import abc
2+
import ctypes
23
import functools
4+
import weakref
35

46
from mlir import ir
57

@@ -12,3 +14,23 @@ def get_mlir_type(cls) -> ir.Type: ...
1214

1315
def fn_cache(f, maxsize: int | None = None):
1416
return functools.wraps(f)(functools.lru_cache(maxsize=maxsize)(f))
17+
18+
19+
def _hold_self_ref_in_ret(fn):
20+
@functools.wraps(fn)
21+
def wrapped(self, *a, **kw):
22+
ret = fn(self, *a, **kw)
23+
_take_owneship(ret, self)
24+
return ret
25+
26+
return wrapped
27+
28+
29+
def _take_owneship(owner, obj):
30+
ptr = ctypes.py_object(obj)
31+
ctypes.pythonapi.Py_IncRef(ptr)
32+
33+
def finalizer(ptr):
34+
ctypes.pythonapi.Py_DecRef(ptr)
35+
36+
weakref.finalize(owner, finalizer, ptr)

0 commit comments

Comments
 (0)