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
| if exc.name == from_name: | ||
| if (exc.name == from_name and | ||
| from_name not in sys.modules): | ||
| #sys.modules.get(from_name, _NEEDS_LOADING) is not None): |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I don't know what condition is better. Tests are passed in both cases.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
On further thoughts, perhaps the only reliable way is testing the error message. There is possible race condition with testing sys.modules.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I vote for the latter since it's more explicit what you're actually worrying about.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Isn't there a race condition? More precisely, is a race condition here bad?
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Is the import lock released at this point? @pitrou might have an opinion on how bad the race condition might be (I personally don't as I've avoided the import lock as much as possible
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We're definitely not holding any of the import locks here (not the global one, and not the one for from_name either), so there are two race conditions to consider:
In neither case do we actually care whether that state change was made before our import attempt, or by a different thread while our import attempt was running, but we do want to treat the cases differently: we want the first one to be considered a failed import, and we want the second one to be considered a successful import.
So I think the sys.modules.get(from_name, _NEEDS_LOADING) is not None check would be the right one to use.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
There is yet one possibility. Some other thread started importing the module. It added the module into sys.modules, but still not set the attribute in the parent module.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
That's fine, as whether it fails or not will then depend on whether subsequent code uses LOAD_ATTR or IMPORT_FROM, just as it does for circular imports.
| # If fromlist entry is None, let a ModuleNotFoundError propagate. | ||
| # issue31642 | ||
| mod = types.ModuleType(PKG_NAME) | ||
| mod.__path__ = ['XXX'] |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You can actually just set __path__ to [] and it will still count as a package.
| if exc.name == from_name: | ||
| if (exc.name == from_name and | ||
| from_name not in sys.modules): | ||
| #sys.modules.get(from_name, _NEEDS_LOADING) is not None): |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I vote for the latter since it's more explicit what you're actually worrying about.
|
Thanks @serhiy-storchaka for the PR |
|
Sorry, @serhiy-storchaka, I could not cleanly backport this to |
…s.modules. (pythonGH-3834). (cherry picked from commit f07e2b6)
|
GH-3923 is a backport of this pull request to the 3.6 branch. |
https://bugs.python.org/issue31642