issue15110 introduced a new private function, _exec_module(), to the default loaders in Lib/importlib/_bootstrap.py. Doing so helped hide importlib frames from the traceback. A related fix is likely required for issue15425.
Should some general form of exec_module() be an official part of the loader API (in importlib.abc.Loader). If a loader subclasses one of the default ones, it already gets the behavior for free. Otherwise it does not benefit from the cleaner traceback (unless the author is savvy to the _exec_module() implementation detail).
Though the use of Loader._exec_module() to strip tracebacks is an implementation detail of CPython, having a public Loader.exec_module() would allow other implementations to perform a similar action. And if we found a way to move remove_importlib_frames to importlib then all the better. (Looking into traceback API improvements is on my to-do list.)
As a bonus, Loader.exec_module() would also allow loaders that wrap other loaders (yes, esoteric) to manage the execution portion of loading. I haven't given this part a lot of thought, so it may not be much of a bonus (or could even be a negative).
From the perspective of caution, the import machinery is already pretty complicated. It may not be advisable to add to that even in this little way.
|