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

Aliasing import of sub-{module,package} from the package raises AttributeError on import. #67392

Closed
franck mannequin opened this issue Jan 9, 2015 · 8 comments
Closed
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@franck
Copy link
Mannequin

franck mannequin commented Jan 9, 2015

BPO 23203
Nosy @brettcannon, @ncoghlan, @ezio-melotti, @ericsnowcurrently, @serhiy-storchaka
Superseder
  • bpo-30024: Treat import a.b.c as m as m = sys.modules['a.b.c']
  • 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 2018-03-22.12:24:45.746>
    created_at = <Date 2015-01-09.06:44:42.751>
    labels = ['interpreter-core', 'type-bug']
    title = 'Aliasing import of sub-{module,package} from the package raises AttributeError on import.'
    updated_at = <Date 2018-03-22.12:24:45.745>
    user = 'https://bugs.python.org/franck'

    bugs.python.org fields:

    activity = <Date 2018-03-22.12:24:45.745>
    actor = 'ncoghlan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-03-22.12:24:45.746>
    closer = 'ncoghlan'
    components = ['Interpreter Core']
    creation = <Date 2015-01-09.06:44:42.751>
    creator = 'franck'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 23203
    keywords = []
    message_count = 8.0
    messages = ['233719', '233744', '233768', '233815', '233823', '297476', '314245', '314249']
    nosy_count = 7.0
    nosy_names = ['brett.cannon', 'ncoghlan', 'ezio.melotti', 'Arfrever', 'eric.snow', 'serhiy.storchaka', 'franck']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = '30024'
    type = 'behavior'
    url = 'https://bugs.python.org/issue23203'
    versions = []

    @franck
    Copy link
    Mannequin Author

    franck mannequin commented Jan 9, 2015

    Hi, for those of you that prefer to read an example, you can read that commented demonstration of the bug[1].

    Today I discovered what I think is a bug in the import system. Here is the basic setup:

    We have three nested packages: foo -> bar -> baz. The bar package imports foo.bar.baz. We try to import foo.bar. This works well unless we try to alias the foo.bar.baz import in foo.bar with the "import ... as ..." syntax. In that case the file will be read and executed but when we get out of the import statement, then python throws:

    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "foo_alias_mod/bar/__init__.py", line 1, in <module>
        import foo_alias_mod.bar.baz as name_does_not_matter
    AttributeError: 'module' object has no attribute 'bar'

    This works whether baz is a package or a module actually. It does not matter if it's from the interpreter, or in a file, ... I've seen it trigger with 2.7.5, 2.7.9, 3.4.5, tip, so I guess this has been here for some time.

    Please read the link below for a complete demo, and you can always download the tarball[2] to test yourself.

    [1]: Commented demonstration: http://98810f8c06.net/wtf_python.html
    [2]: Tarball for test: http://98810f8c06.net/wtf_python-demo.tar.bz2

    @franck franck mannequin added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jan 9, 2015
    @ezio-melotti ezio-melotti added the type-bug An unexpected behavior, bug, or error label Jan 9, 2015
    @ncoghlan
    Copy link
    Contributor

    ncoghlan commented Jan 9, 2015

    You can see the difference between the two cases in the bytecode:

    >>> dis.dis("import x.y.z")
      1           0 LOAD_CONST               0 (0)
                  3 LOAD_CONST               1 (None)
                  6 IMPORT_NAME              0 (x.y.z)
                  9 STORE_NAME               1 (x)
                 12 LOAD_CONST               1 (None)
                 15 RETURN_VALUE                         
    >>> dis.dis("import x.y.z as bar")            
      1           0 LOAD_CONST               0 (0)
                  3 LOAD_CONST               1 (None)
                  6 IMPORT_NAME              0 (x.y.z)
                  9 LOAD_ATTR                1 (y)
                 12 LOAD_ATTR                2 (z)
                 15 STORE_NAME               3 (bar)
                 18 LOAD_CONST               1 (None)
                 21 RETURN_VALUE

    The aliased version needs to bind the innermost object immediately, so it fails, since "foo.bar" doesn't get set until *after* the import is finished.

    The version without the alias succeeeds, as it doesn't attempt to eagerly access the attribute before it gets set by the interpreter.

    To better handle a similar situation with eager attribute lookups during import, bpo-17636 changed IMPORT_FROM to fall back to looking at sys.modules when a module attribute it is looking for is missing.

    Brett, Eric, perhaps it would be worth changing the bytecode emitted in the "import x.y.z as name" case to match that for "from x.y import x as name"?

    >>> dis.dis("from x.y import z as bar")       
      1           0 LOAD_CONST               0 (0)
                  3 LOAD_CONST               1 (('z',))
                  6 IMPORT_NAME              0 (x.y)
                  9 IMPORT_FROM              1 (z)
                 12 STORE_NAME               2 (bar)
                 15 POP_TOP
                 16 LOAD_CONST               2 (None)
                 19 RETURN_VALUE

    @brettcannon
    Copy link
    Member

    I think I would need to see exactly how you want the bytecode to change and then think over any backwards-compatibility issues since this is doesn't come up very often.

    @ncoghlan
    Copy link
    Contributor

    I'm suggesting we change this part of the bytecode emitted for "import x.y.z as bar":

              6 IMPORT_NAME              0 (x.y.z)
              9 LOAD_ATTR                1 (y)
             12 LOAD_ATTR                2 (z)
             15 STORE_NAME               3 (bar)
    

    to instead emit:

              6 IMPORT_NAME              0 (x.y)
              9 IMPORT_FROM              1 (z)
             12 STORE_NAME               2 (bar)
             15 POP_TOP
    

    The degenerate case of "import x as y" would be unchanged, only cases which currently emit LOAD_ATTR instructions would be modified.

    I haven't looked at the practical details yet, but the key would be to use IMPORT_FROM to do the name resolution, rather than LOAD_ATTR, thus enabling the fallback to sys.modules for the eager lookup.

    @brettcannon
    Copy link
    Member

    That seems reasonable. I guess first step is a patch and then seeing if it passes the test suite.

    @serhiy-storchaka
    Copy link
    Member

    Nick's suggestion was implemented in bpo-30024.

    @serhiy-storchaka
    Copy link
    Member

    Is anything left to do with this issue after bpo-30024?

    @ncoghlan
    Copy link
    Contributor

    Heh, apparently I forgot how IMPORT_FROM currently works some time between 2015 and 2017 :)

    I agree this is out of date now, as the requested behaviour was already implemented for 3.7

    @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) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants