gh-91058: Add error suggestions to 'import from' import errors by pablogsal · Pull Request #98305 · python/cpython (original) (raw)

PyImport_Import goes through the cache if for whatever reason the module is already imported, no? (And indeed, the module is imported as part of raising the exception).

Exactly, but consider the scenario where it isn't already imported and the module itself has some side effects (like it prints something at the top level). Importing it would then might produce unexpected side effects that only appear when the traceback is printed, so I thought whether we could limit this hint when the module is already cached.

if module not in sys.modules: return None # give up if it isn't already in the cache
module = PyImport_Import(mod_name)
...