def clear_finalizers(cls, clear_all=False): """Removes all registered finalizers. :param clear_all: If `True`, all finalizers are deleted. Otherwise, only the finalizers from an empty weak reference are deleted (i.e., weak references pointing to inexistent objects). """ if clear_all: cls.finalizers.clear() else: for id, ref in items(cls.finalizers): if ref() is None: cls.finalizers.pop(id, None)