Skip to content

[3.8] bpo-37409: fix relative import with no parent (GH-14956) #15913

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

Merged
merged 1 commit into from
Sep 11, 2019

Conversation

brettcannon
Copy link
Member

@brettcannon brettcannon commented Sep 11, 2019

Relative imports use resolve_name to get the absolute target name,
which first seeks the current module's absolute package name from the globals:
If package (and spec.parent) are missing then
import uses name, truncating the last segment if
the module is a submodule rather than a package init.py
(which it guesses from whether path is defined).

The name attempt should fail if there is no parent package (top level modules),
if name is 'main' (-m entry points), or both (scripts).
That is, if both name has no subcomponents and the module does not seem
to be a package init module then import should fail..
(cherry picked from commit 92420b3)

Co-authored-by: Ben Lewis benjimin@users.noreply.github.com

https://bugs.python.org/issue37409

Automerge-Triggered-By: @brettcannon

Relative imports use resolve_name to get the absolute target name,
which first seeks the current module's absolute package name from the globals:
If __package__ (and __spec__.parent) are missing then
import uses __name__, truncating the last segment if
the module is a submodule rather than a package __init__.py
(which it guesses from whether __path__ is defined).

The __name__ attempt should fail if there is no parent package (top level modules),
if __name__ is '__main__' (-m entry points), or both (scripts).
That is, if both __name__ has no subcomponents and the module does not seem
to be a package __init__ module then import should fail..
(cherry picked from commit 92420b3)

Co-authored-by: Ben Lewis <benjimin@users.noreply.github.com>
@miss-islington
Copy link
Contributor

Thanks @brettcannon for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 11, 2019
…ythonGH-15913)

Relative imports use resolve_name to get the absolute target name,
which first seeks the current module's absolute package name from the globals:
If __package__ (and __spec__.parent) are missing then
import uses __name__, truncating the last segment if
the module is a submodule rather than a package __init__.py
(which it guesses from whether __path__ is defined).

The __name__ attempt should fail if there is no parent package (top level modules),
if __name__ is '__main__' (-m entry points), or both (scripts).
That is, if both __name__ has no subcomponents and the module does not seem
to be a package __init__ module then import should fail..
(cherry picked from commit 92420b3)

Co-authored-by: Ben Lewis <benjimin@users.noreply.github.com>
(cherry picked from commit 0a6693a)

Co-authored-by: Brett Cannon <54418+brettcannon@users.noreply.github.com>
@bedevere-bot
Copy link

GH-15925 is a backport of this pull request to the 3.7 branch.

miss-islington added a commit that referenced this pull request Sep 11, 2019
)

Relative imports use resolve_name to get the absolute target name,
which first seeks the current module's absolute package name from the globals:
If __package__ (and __spec__.parent) are missing then
import uses __name__, truncating the last segment if
the module is a submodule rather than a package __init__.py
(which it guesses from whether __path__ is defined).

The __name__ attempt should fail if there is no parent package (top level modules),
if __name__ is '__main__' (-m entry points), or both (scripts).
That is, if both __name__ has no subcomponents and the module does not seem
to be a package __init__ module then import should fail..
(cherry picked from commit 92420b3)

Co-authored-by: Ben Lewis <benjimin@users.noreply.github.com>
(cherry picked from commit 0a6693a)

Co-authored-by: Brett Cannon <54418+brettcannon@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants