
On Sun, Aug 23, 2020 at 11:02 AM Gustav O <gustav@odinger.se> wrote:
It seems like most of us agree that the speed drawbacks wouldn’t be worth it — which I find understandable.
If my understanding of excepthooks is correct, and that it’s the place to put code that should be executed once an AttributeError is raised, it sounds like a great solution.
Not quite - it happens if such an error isn't caught. It's the place where exceptions get printed to the console. That's the perfect place for this sort of thing.
However, AttributeErrors aren’t the only exceptions raised when a shadowing module is imported. A broad range of exceptions could be raised as a result of an unintended module being imported.
One option would be to implement a similar search whenever any exception is raised and the conditions that you mentioned apply. Limiting it to AttributeError would be odd.
Yep, you can handle whatever errors you like, although I think ImportError and AttributeError will cover pretty much everything you need. As an extra advantage, the exception hook is pure Python code, so you don't have to worry about messing around in C. You should be able to make use of importlib to do most of the work for you. ChrisA