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

None value in sys.modules no longer blocks import #75823

Closed
tiran opened this issue Sep 29, 2017 · 5 comments
Closed

None value in sys.modules no longer blocks import #75823

tiran opened this issue Sep 29, 2017 · 5 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@tiran
Copy link
Member

tiran commented Sep 29, 2017

BPO 31642
Nosy @warsaw, @brettcannon, @ncoghlan, @tiran, @ericsnowcurrently, @serhiy-storchaka
PRs
  • bpo-31642: Restore blocking "from" import by setting None in sys.modules. #3834
  • [3.6] bpo-31642: Restore blocking "from" import by setting None in sys.modules. (GH-3834). #3923
  • 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 = None
    closed_at = <Date 2017-10-08.09:55:13.546>
    created_at = <Date 2017-09-29.20:21:12.164>
    labels = ['interpreter-core', 'type-bug', '3.7']
    title = 'None value in sys.modules no longer blocks import'
    updated_at = <Date 2017-10-08.09:55:13.493>
    user = 'https://github.com/tiran'

    bugs.python.org fields:

    activity = <Date 2017-10-08.09:55:13.493>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-10-08.09:55:13.546>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2017-09-29.20:21:12.164>
    creator = 'christian.heimes'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 31642
    keywords = ['patch', '3.6regression']
    message_count = 5.0
    messages = ['303356', '303374', '303396', '303900', '303904']
    nosy_count = 6.0
    nosy_names = ['barry', 'brett.cannon', 'ncoghlan', 'christian.heimes', 'eric.snow', 'serhiy.storchaka']
    pr_nums = ['3834', '3923']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31642'
    versions = ['Python 3.6', 'Python 3.7']

    @tiran
    Copy link
    Member Author

    tiran commented Sep 29, 2017

    Since Python 3.6, the blocking of imports is broken for 'from package import module' imports.

    $ mkdir a
    $ touch a/__init__.py a/b.py
    >>> import sys
    >>> sys.modules['a.b'] = None
    >>> from a import b
    >>> b is None
    True
    >>> import a.b
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ModuleNotFoundError: import of 'a.b' halted; None in sys.modules

    Tests with Python 2.7 to master:

    $ python2.7 -c "from a import b; print(b)"
    <module 'a.b' from 'a/b.py'>
    $ python2.7 -c "import sys; sys.modules['a.b'] = None; from a import b"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ImportError: cannot import name b
    $ python2.7 -c "import sys; sys.modules['a.b'] = None; from a import b"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ImportError: cannot import name b
    $ python3.4 -c "import sys; sys.modules['a.b'] = None; from a import b"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ImportError: import of 'a.b' halted; None in sys.modules
    $ python3.5 -c "import sys; sys.modules['a.b'] = None; from a import b"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ImportError: import of 'a.b' halted; None in sys.modules
    $ python3.6 -c "import sys; sys.modules['a.b'] = None; from a import b"
    $ ./python -c "import sys; sys.modules['a.b'] = None; from a import b"

    @tiran tiran added 3.8 only security fixes 3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Sep 29, 2017
    @serhiy-storchaka
    Copy link
    Member

    This is a side effect of bpo-15767. The exception is raised in _find_and_load, but it is always silenced in _handle_fromlist.

    @serhiy-storchaka
    Copy link
    Member

    PR 3834 provide possible solution of this issue.

    Initially import errors were silenced for solving bpo-15715. An import error raised when import was blocked by setting None in sys.modules was not silenced because it has different error message. I don't know whether this was intentional. In bpo-15767 (5fdb8c897023) the error message test was replaced by the type test, and that exception become silenced (I suppose it was not intentional).

    @serhiy-storchaka
    Copy link
    Member

    New changeset f07e2b6 by Serhiy Storchaka in branch 'master':
    bpo-31642: Restore blocking "from" import by setting None in sys.modules. (bpo-3834)
    f07e2b6

    @serhiy-storchaka
    Copy link
    Member

    New changeset 6f059ab by Serhiy Storchaka in branch '3.6':
    [3.6] bpo-31642: Restore blocking "from" import by setting None in sys.modules. (GH-3834). (bpo-3923)
    6f059ab

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants