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

importlib.__import__ does not fail for invalid relative import #70555

Closed
manueljacob mannequin opened this issue Feb 16, 2016 · 11 comments
Closed

importlib.__import__ does not fail for invalid relative import #70555

manueljacob mannequin opened this issue Feb 16, 2016 · 11 comments
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@manueljacob
Copy link
Mannequin

manueljacob mannequin commented Feb 16, 2016

BPO 26367
Nosy @brettcannon, @terryjreedy, @ncoghlan, @ericsnowcurrently, @serhiy-storchaka, @manueljacob
Files
  • relimport-3.5.patch: Patch against 3.5 branch (without changes in Python/importlib.h)
  • relimport-3.6.patch: Patch against default branch (without changes in Python/importlib.h)
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/brettcannon'
    closed_at = <Date 2016-02-20.21:00:13.507>
    created_at = <Date 2016-02-16.00:59:01.193>
    labels = ['type-bug']
    title = 'importlib.__import__ does not fail for invalid relative import'
    updated_at = <Date 2017-07-23.06:44:07.815>
    user = 'https://github.com/manueljacob'

    bugs.python.org fields:

    activity = <Date 2017-07-23.06:44:07.815>
    actor = 'serhiy.storchaka'
    assignee = 'brett.cannon'
    closed = True
    closed_date = <Date 2016-02-20.21:00:13.507>
    closer = 'brett.cannon'
    components = []
    creation = <Date 2016-02-16.00:59:01.193>
    creator = 'mjacob'
    dependencies = []
    files = ['41937', '41938']
    hgrepos = []
    issue_num = 26367
    keywords = ['patch']
    message_count = 11.0
    messages = ['260338', '260461', '260484', '260486', '260491', '260540', '260575', '260576', '260599', '260623', '298891']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'terry.reedy', 'ncoghlan', 'python-dev', 'eric.snow', 'serhiy.storchaka', 'mjacob']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26367'
    versions = ['Python 3.3', 'Python 3.4', 'Python 3.5', 'Python 3.6']

    @manueljacob
    Copy link
    Mannequin Author

    manueljacob mannequin commented Feb 16, 2016

    Python 3.6.0a0 (default:6c6f7dff597b, Feb 16 2016, 01:24:51) 
    [GCC 5.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import importlib
    >>> importlib.__import__('array', globals(), locals(), level=1)
    <module '.array' from '/home/manu/vcs/cpython/build/lib.linux-x86_64-3.6/array.cpython-36m-x86_64-linux-gnu.so'>
    >>> __import__('array', globals(), locals(), level=1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: attempted relative import with no known parent package

    Instead of failing, importlib.__import__ returns a module with a wrong name. This happens with both built-in modules and pure python modules. However it fails when replacing 'array' with 'time' (this seems to be related to whether the module is in Modules/Setup.dist).

    @manueljacob manueljacob mannequin added the type-bug An unexpected behavior, bug, or error label Feb 16, 2016
    @brettcannon brettcannon self-assigned this Feb 17, 2016
    @manueljacob
    Copy link
    Mannequin Author

    manueljacob mannequin commented Feb 18, 2016

    (For some reason, I forgot to submit the previous comment).

    The attached patches fix the issue (one for the 3.5 branch, one for the default branch) by bringing importlib.__import__ closer to the builtin __import__.

    The extra code in the default / 3.6 branch is because of the changes from bpo-18018.

    @brettcannon
    Copy link
    Member

    The corresponding code in C that actually does the check: https://hg.python.org/cpython/file/default/Python/import.c#l1498

    Manuel, can you sign the CLA at https://www.python.org/psf/contrib/contrib-form/ ? Once you sign it I can officially review your patch, otherwise I technically can't touch your code.

    @manueljacob
    Copy link
    Mannequin Author

    manueljacob mannequin commented Feb 18, 2016

    Done. I'm a bit surprised this wasn't necessary for my previous two patches, but they were even more trival than this one. ;)

    Do we have to wait until my tracker profile is updated?

    @brettcannon
    Copy link
    Member

    If a contribution is insignificant enough then technically a CLA isn't necessary, but I just make it a habit to ask for one regardless.

    And your profile will probably get updated fairly quickly, so I wouldn't worry about it holding anything up (I'm hoping to commit the code tomorrow; I already did a review and it LGTM).

    @terryjreedy
    Copy link
    Member

    Manuel, welcome to the tracker and thank you for submitting patches. I agree that 5 lines is past the threshhold. Your profile has been CLA-updated, so we are good to go.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Feb 20, 2016

    New changeset e523efd47418 by Brett Cannon in branch '3.5':
    Issue bpo-26367: Have importlib.__init__() raise RuntimeError when
    https://hg.python.org/cpython/rev/e523efd47418

    New changeset 8f72bf88f471 by Brett Cannon in branch 'default':
    Merge for issue bpo-26367
    https://hg.python.org/cpython/rev/8f72bf88f471

    @brettcannon
    Copy link
    Member

    Thanks for the patch, Manuel!

    @manueljacob
    Copy link
    Mannequin Author

    manueljacob mannequin commented Feb 21, 2016

    I think the "What's New" entry has two typos. It should be importlib.__import__() instead of importlib.__init__() and SystemError instead of RuntimeError.

    @brettcannon
    Copy link
    Member

    Thanks for spotting those! And just to clarify on terminology, the typos were in the NEWS file, not "What's New" which is a different thing (https://hg.python.org/cpython/file/default/Doc/whatsnew).

    @serhiy-storchaka
    Copy link
    Member

    New changeset c824cc8 by Serhiy Storchaka in branch '3.5':
    [3.5] Backport bpo-30876 (GH-2639), bpo-18018 and bpo-26367. (bpo-2677)
    c824cc8

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    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

    No branches or pull requests

    3 participants