Skip to content
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

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

Merged
merged 1 commit into from Sep 11, 2019

Conversation

@brettcannon
Copy link
Member

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>
@brettcannon brettcannon self-assigned this Sep 11, 2019
@brettcannon brettcannon merged commit 0a6693a into python:3.8 Sep 11, 2019
6 checks passed
6 checks passed
Azure Pipelines PR #20190911.59 succeeded
Details
bedevere/issue-number Issue number 37409 found
Details
bedevere/maintenance-branch-pr Valid maintenance branch PR title.
bedevere/news News entry found in Misc/NEWS.d
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@brettcannon brettcannon deleted the brettcannon:backport-92420b3-3.8 branch Sep 11, 2019
@miss-islington

This comment has been minimized.

Copy link

miss-islington commented Sep 11, 2019

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 added 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

This comment has been minimized.

Copy link

bedevere-bot commented Sep 11, 2019

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
Projects
None yet
5 participants
You can’t perform that action at this time.