diff --git a/docs/contributors.rst b/docs/contributors.rst index 08a828bf19..c2d0b0a698 100644 --- a/docs/contributors.rst +++ b/docs/contributors.rst @@ -32,3 +32,4 @@ Contributors include: - Nathan Simpson - Beojan Stanislaus - Daniel Werner +- Jonas Rembser diff --git a/src/pyhf/events.py b/src/pyhf/events.py index f28d0a7563..ae55e2d4f1 100644 --- a/src/pyhf/events.py +++ b/src/pyhf/events.py @@ -64,12 +64,20 @@ def _flush(self): self._callbacks = _callbacks def __call__(self, *args, **kwargs): - for func, arg in self.callbacks: + for func, arg in self._callbacks: # weakref: needs to be de-ref'd first before calling if arg is not None: - func()(arg(), *args, **kwargs) + arg_ref = arg() + if arg_ref is not None: + func()(arg_ref, *args, **kwargs) else: func()(*args, **kwargs) + # Flush after calling all the callbacks, not before, as callbacks in the + # beginning of the iteration might cause new dead arg weakrefs in + # callbacks that are iterated over later. + # Checking for dead weakrefs in each iteration and flushing at the end + # avoids redundant dead weakref checking in subsequent calls. + self._flush() def __iter__(self): return iter(self.callbacks)