Fix code to restore sys.modules after running a handler#26111
Fix code to restore sys.modules after running a handler#26111
Conversation
Replacing the whole dict doesn't work as expected, but we can remove entries that weren't in the dict before the call. This fixes the code to work in simple cases, but doesn't address possible race conditions between handlers.
|
This seems to at least fix the original issue in Python 3:
now works without errors |
|
@stephenmcgruer informed me that the issue doesn't reproduce reliably, so I tried a few more times, and sure enough after ~5 tries the issue reappeared... In other words, this doesn't really work :( |
|
FWIW, this does fix the other issue that earlier relative imports shadow later ones with the same name (the new infra tests would fail without this fix), so it's probably still nice to have. Tangent: the new infra tests fail consistently in Python 3 both with and without the change when doing
Might as well fix this by the way. |
| # https://docs.python.org/3.3/whatsnew/3.3.html#a-finer-grained-import-lock | ||
| if PY3: | ||
| for mod_name in list(sys.modules.keys()): | ||
| with importlib._bootstrap._ModuleLockManager(mod_name): |
There was a problem hiding this comment.
This module is NOT meant to be directly imported! It has been designed such
that it can be bootstrapped into Python as the implementation of import. As
such it requires the injection of specific modules and attributes in order to
work. One should use importlib as the public-facing version of this module.
|
FWIW, b7fa472 seems to fix the race condition when importing |
Replacing the whole dict doesn't work as expected, but we can remove
entries that weren't in the dict before the call. This fixes the code
to work in simple cases, but doesn't address possible race conditions
between handlers.