Skip to content

Commit

Permalink
Somewhat address the previous fixme
Browse files Browse the repository at this point in the history
  • Loading branch information
mara004 committed Apr 3, 2023
1 parent 8732642 commit d494c25
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/pypdfium2/_helpers/_internal/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,11 @@ def __init__(self, close_func, *args, obj=None, needs_free=True, **kwargs):
# proactively prevent accidental double initialization
assert not hasattr(self, "_finalizer")

self._uuid = None
if DEBUG_AUTOCLOSE:
self._uuid = uuid.uuid4()

if obj is None:
obj = self

# FIXME `self.parent` will provide the value of the property at the current time - however, we want to use the value from finalization time, not init time
self._fin_args = (obj, AutoCloseable._close_template, self.raw, self._uuid, self.parent, close_func, *args)
self._fin_kwargs = kwargs
self._close_func = close_func
self._obj = self if obj is None else obj
self._uuid = uuid.uuid4() if DEBUG_AUTOCLOSE else None
self._ex_args = args
self._ex_kwargs = kwargs

self._finalizer = None
if needs_free:
Expand All @@ -51,7 +46,8 @@ def __init__(self, close_func, *args, obj=None, needs_free=True, **kwargs):

def _attach_finalizer(self):
assert self._finalizer is None
self._finalizer = weakref.finalize(*self._fin_args, **self._fin_kwargs)
# FIXME if `parent` may change after finalizer installation, we've got a problem...
self._finalizer = weakref.finalize(self._obj, AutoCloseable._close_template, self._close_func, self.raw, self._uuid, self.parent, *self._ex_args, **self._ex_kwargs)

def _detach_finalizer(self):
self._finalizer.detach()
Expand All @@ -66,7 +62,7 @@ def _tree_closed(self):


@staticmethod
def _close_template(raw, uuid, parent, close_func, *args, **kwargs):
def _close_template(close_func, raw, uuid, parent, *args, **kwargs):
# FIXME should we add context info (explicit/automatic) ?
if DEBUG_AUTOCLOSE:
print(f"Closing {raw} with UUID {uuid}", file=sys.stderr)
Expand Down

0 comments on commit d494c25

Please sign in to comment.