Coverage for trimesh/exceptions.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-24 04:40 +0000

1""" 

2exceptions.py 

3---------------- 

4 

5Wrap exceptions. 

6""" 

7 

8 

9class ExceptionWrapper: 

10 """ 

11 Create a dummy object which will raise an exception when attributes 

12 are accessed (i.e. when used as a module) or when called (i.e. 

13 when used like a function) 

14 

15 For soft dependencies we want to survive failing to import but 

16 we would like to raise an appropriate error when the functionality is 

17 actually requested so the user gets an easily debuggable message. 

18 """ 

19 

20 def __init__(self, exception: BaseException): 

21 # store the exception type and the args rather than the whole thing 

22 # this prevents the locals from the time of the exception 

23 # from being stored as well. 

24 self.exception = (type(exception), exception.args) 

25 

26 def __getattribute__(self, *args, **kwargs): 

27 # will raise when this object is accessed like an object 

28 # if it's asking for our class type return None 

29 # this allows isinstance() checks to not re-raise 

30 if args[0] == "__class__": 

31 return None.__class__ 

32 

33 # re-create our original exception from the type and arguments 

34 exc_type, exc_args = super().__getattribute__("exception") 

35 raise exc_type(*exc_args) 

36 

37 def __call__(self, *args, **kwargs): 

38 # behave the same when this object is called like a function 

39 # as when someone tries to access an attribute like a module 

40 self.__getattribute__("exception")