New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
importlib: better error message when import fail during Python shutdown #70824
Comments
Example of script.py: class Bla:
def __del__(self):
try:
import xxxx
except Exception as exc:
print("import error: [%s] %r" % (type(exc), exc))
bla = Bla() Running this example logs a strange error: $ python3.5 script.py
import error: [<class 'TypeError'>] TypeError("'NoneType' object is not iterable",) The error comes from importlib._bootstrap._find_spec() which tries to iterator on sys.meta_path, whereas PyImport_Cleanup() was called and this function setted sys.meta_path to None. Attached patch enhances _find_spec() to handle this case to return None. Error with the patch: $ python3.5 script.py
import error: [<class 'ImportError'>] ImportError('sys.meta_path is None, Python is likely shutting down',) |
Updated patch with a dot in the comment :-) Is ImportError the best exception in this case? |
Either ImportError or TypeError. ImportError makes sense since this is directly related to import semantics while TypeError makes sense as we are saying None specifically is not allowed. |
I will reply to my own question: in fact, ImportError is more convenient. The "try/except ImportError" pattern is common, in some cases, it's acceptable to skip a whole module. For a direct example on import during Python shutdown, see the issue bpo-21925: "ResourceWarning sometimes doesn't display" which adds "try/except ImportError" to be able to log warnings. @brett, Eric: So, do you like importlib_shutdown-2.patch? |
Oh. And is it ok to apply this change to Python 2.7 and 3.5? |
It's technically a change in semantics as it shifts what exception is raised, so I wouldn't backport it. |
Ok, I'm fine with only changing Python 3.6. |
New changeset 14c56f697a85 by Victor Stinner in branch 'default': |
New changeset e2c482b60776 by Victor Stinner in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: