This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2016年03月24日 15:06 by vstinner, last changed 2022年04月11日 14:58 by admin. This issue is now closed.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| importlib_shutdown.patch | vstinner, 2016年03月24日 15:06 | review | ||
| importlib_shutdown-2.patch | vstinner, 2016年03月24日 16:57 | review | ||
| Messages (9) | |||
|---|---|---|---|
| msg262352 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2016年03月24日 15:06 | |
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',)
-------------------------
|
|||
| msg262367 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2016年03月24日 16:57 | |
Updated patch with a dot in the comment :-) Is ImportError the best exception in this case? |
|||
| msg262368 - (view) | Author: Brett Cannon (brett.cannon) * (Python committer) | Date: 2016年03月24日 17:00 | |
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. |
|||
| msg262375 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2016年03月24日 20:49 | |
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 #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? |
|||
| msg262376 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2016年03月24日 20:49 | |
Oh. And is it ok to apply this change to Python 2.7 and 3.5? |
|||
| msg262378 - (view) | Author: Brett Cannon (brett.cannon) * (Python committer) | Date: 2016年03月24日 21:56 | |
It's technically a change in semantics as it shifts what exception is raised, so I wouldn't backport it. |
|||
| msg262379 - (view) | Author: STINNER Victor (vstinner) * (Python committer) | Date: 2016年03月24日 21:59 | |
> 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. |
|||
| msg262387 - (view) | Author: Roundup Robot (python-dev) (Python triager) | Date: 2016年03月24日 23:41 | |
New changeset 14c56f697a85 by Victor Stinner in branch 'default': Fix bug in __import__ during Python shutdown https://hg.python.org/cpython/rev/14c56f697a85 |
|||
| msg262405 - (view) | Author: Roundup Robot (python-dev) (Python triager) | Date: 2016年03月25日 07:57 | |
New changeset e2c482b60776 by Victor Stinner in branch 'default': Issue #26637: Fix test_io https://hg.python.org/cpython/rev/e2c482b60776 |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:58:28 | admin | set | github: 70824 |
| 2016年03月25日 07:57:50 | python-dev | set | messages: + msg262405 |
| 2016年03月24日 23:42:05 | vstinner | set | status: open -> closed resolution: fixed |
| 2016年03月24日 23:41:57 | python-dev | set | nosy:
+ python-dev messages: + msg262387 |
| 2016年03月24日 21:59:26 | vstinner | set | messages: + msg262379 |
| 2016年03月24日 21:56:42 | brett.cannon | set | messages: + msg262378 |
| 2016年03月24日 20:49:29 | vstinner | set | messages: + msg262376 |
| 2016年03月24日 20:49:08 | vstinner | set | messages: + msg262375 |
| 2016年03月24日 17:00:11 | brett.cannon | set | messages: + msg262368 |
| 2016年03月24日 16:57:45 | vstinner | set | files:
+ importlib_shutdown-2.patch messages: + msg262367 |
| 2016年03月24日 15:06:33 | vstinner | create | |