diff --git a/src/ifcopenshell-python/ifcopenshell/entity_instance.py b/src/ifcopenshell-python/ifcopenshell/entity_instance.py index f43e58bcb8..3c53a96cc1 100644 --- a/src/ifcopenshell-python/ifcopenshell/entity_instance.py +++ b/src/ifcopenshell-python/ifcopenshell/entity_instance.py @@ -160,7 +160,14 @@ class entity_instance: instance references prevents file gc, even with all instance refs deleted. This is a work-around for that. """ - self.wrapped_data.file = None + # Avoid infinite recursion if entity is failed to initialize + # and wrapped_data is unset. Hacky since we override + # both __dict__ and __dir__. + try: + wrapped_data = super().__getattribute__("wrapped_data") + wrapped_data.file = None + except AttributeError: + return @property def file(self): diff --git a/src/ifcopenshell-python/ifcopenshell/file.py b/src/ifcopenshell-python/ifcopenshell/file.py index fd08c36b77..cb50ca6ca1 100644 --- a/src/ifcopenshell-python/ifcopenshell/file.py +++ b/src/ifcopenshell-python/ifcopenshell/file.py @@ -277,6 +277,10 @@ class file: file_dict[self.file_pointer()] = weakref.ref(self) def __del__(self) -> None: + # Avoid infinite recursion if file is failed to initialize + # and wrapped_data is unset. + if "wrapped_data" not in dir(self): + return del file_dict[self.file_pointer()] def set_history_size(self, size: int) -> None: