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

__import__ now raises with non-existing items in fromlist in 3.3 #59920

Closed
sfeltman mannequin opened this issue Aug 16, 2012 · 14 comments
Closed

__import__ now raises with non-existing items in fromlist in 3.3 #59920

sfeltman mannequin opened this issue Aug 16, 2012 · 14 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sfeltman
Copy link
Mannequin

sfeltman mannequin commented Aug 16, 2012

BPO 15715
Nosy @brettcannon, @birkenfeld, @pitrou, @ericsnowcurrently
Files
  • issue15715.diff
  • 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 2012-08-17.17:21:49.878>
    created_at = <Date 2012-08-16.22:42:08.209>
    labels = ['interpreter-core', 'type-bug', 'library', 'release-blocker']
    title = '__import__ now raises with non-existing items in fromlist in 3.3'
    updated_at = <Date 2012-08-18.03:38:49.647>
    user = 'https://bugs.python.org/sfeltman'

    bugs.python.org fields:

    activity = <Date 2012-08-18.03:38:49.647>
    actor = 'eric.snow'
    assignee = 'brett.cannon'
    closed = True
    closed_date = <Date 2012-08-17.17:21:49.878>
    closer = 'brett.cannon'
    components = ['Interpreter Core', 'Library (Lib)']
    creation = <Date 2012-08-16.22:42:08.209>
    creator = 'sfeltman'
    dependencies = []
    files = ['26876']
    hgrepos = []
    issue_num = 15715
    keywords = ['patch']
    message_count = 14.0
    messages = ['168423', '168424', '168425', '168426', '168427', '168432', '168433', '168435', '168436', '168447', '168448', '168450', '168453', '168494']
    nosy_count = 6.0
    nosy_names = ['brett.cannon', 'georg.brandl', 'pitrou', 'python-dev', 'eric.snow', 'sfeltman']
    pr_nums = []
    priority = 'release blocker'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue15715'
    versions = ['Python 3.3']

    @sfeltman
    Copy link
    Mannequin Author

    sfeltman mannequin commented Aug 16, 2012

    This came up while trying to build pygobject with Python 3.3. The problem is there are some erroneous imports in the fromlist with these bindings that can easily be fixed. But it is a behavioral change so I just wanted to raise awareness if it is not already known.

    $ python3.2 -c "__import__('http', fromlist=['blah'])"
    (works)
    $ python3.3 -c "__import__('http', fromlist=['blah'])"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ImportError: No module named 'http.blah'

    Note this is also the case when using the C API: PyImport_ImportModuleEx

    @sfeltman sfeltman mannequin added the type-bug An unexpected behavior, bug, or error label Aug 16, 2012
    @ericsnowcurrently
    Copy link
    Member

    While this is a regression, 3.3 definitely puts an increased emphasis on using importlib.import_module() instead of builtins.__import__(). Any reason why import_module() isn't used?

    As to the regression, while the current behavior makes more sense, it is contrary to a common idiom when using __import__(). However, given the push away from using __import__(), perhaps it can just stay this way. Oh backward compatibility...

    @sfeltman
    Copy link
    Mannequin Author

    sfeltman mannequin commented Aug 16, 2012

    I think pygobject still supports Python 2.5 which it look like importlib is available.

    I've submitted a patch to pygobject which will work regardless of if this regression is fixed or not: https://bugzilla.gnome.org/show_bug.cgi?id=682051

    @sfeltman
    Copy link
    Mannequin Author

    sfeltman mannequin commented Aug 16, 2012

    Should have been "...Python 2.5 which it looks like importlib is NOT available."

    @pitrou
    Copy link
    Member

    pitrou commented Aug 16, 2012

    Unless it is difficult to fix, I think this regression should be addressed before 3.3 final. Georg?

    @pitrou pitrou added interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir release-blocker labels Aug 16, 2012
    @ericsnowcurrently
    Copy link
    Member

    The failure output with -v:

    ...
    # /home/esnow/projects/cpython/Lib/http/__pycache__/__init__.cpython-33.pyc matches /home/esnow/projects/cpython/Lib/http/__init__.py
    # code object from /home/esnow/projects/cpython/Lib/http/__pycache__/__init__.cpython-33.pyc
    import 'http' # <_frozen_importlib.SourceFileLoader object at 0x7ff82770d450>
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "<frozen importlib._bootstrap>", line 1578, in _handle_fromlist
      File "<frozen importlib._bootstrap>", line 310, in _call_with_frames_removed
      File "<frozen importlib._bootstrap>", line 1530, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1494, in _find_and_load_unlocked
    ImportError: No module named 'http.blah'
    ...

    @ericsnowcurrently
    Copy link
    Member

    Here's a simple patch the allows bogus names in the fromlist. I'm going to verify that this matches the 3.2 semantics, which may not be so cut-and-dry.

    @ericsnowcurrently
    Copy link
    Member

    The following seems to indicate that an ImportError should be raised as expected. I'm guessing that somewhere along the line the exception gets silently eaten.

    ------------------

    (3.2) Python/import.c:ensure_fromlist() [1]

                submod = import_submodule(mod, subname, buf);
                Py_DECREF(item8);
                Py_XDECREF(submod);
                if (submod == NULL) {
                    Py_DECREF(item);
                    return 0;
                }

    [1] http://hg.python.org/cpython/file/3.2/Python/import.c#l2875

    @birkenfeld
    Copy link
    Member

    I agree that we should match 3.2 behavior here.

    @brettcannon
    Copy link
    Member

    And this is why we have said people should use the idiom of __import__('http.blah'); mod = sys.modules['http.blah'] if importlib is not used (which is on PyPI and works as far back as Python 2.3), else you will deal with an AttributeError later instead of an ImportError upfront. Darn backwards-compatibility.

    Eric's patch looks fine, so I will get it committed today.

    @brettcannon brettcannon self-assigned this Aug 17, 2012
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 17, 2012

    New changeset 0d52f125dd32 by Brett Cannon in branch 'default':
    Issue bpo-15715: Ignore failed imports triggered by the use of fromlist.
    http://hg.python.org/cpython/rev/0d52f125dd32

    @ericsnowcurrently
    Copy link
    Member

    When people want to import modules with "runtime" names, they regrettably turn to __import__() and likely will for a while. What a source of headaches!

    If it were less convenient to use __import__(), perhaps fewer people would use it. Could we remove it from <module>.__builtins__, so that it would not be found during the lookup chain? We could still expose it in the builtins module or move it to imp.

    @brettcannon
    Copy link
    Member

    We might be able to hide it in Python 3 since importlib.import_module() has been available since Python 3.1 (and 3.0 is dead anyway).

    @ericsnowcurrently
    Copy link
    Member

    I've taken the tanget over to bpo-15720.

    @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
    interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants