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

old-style (level=-1) importing broken after importlib changes #58797

Closed
scoder opened this issue Apr 16, 2012 · 12 comments
Closed

old-style (level=-1) importing broken after importlib changes #58797

scoder opened this issue Apr 16, 2012 · 12 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@scoder
Copy link
Contributor

scoder commented Apr 16, 2012

BPO 14592
Nosy @brettcannon, @scoder, @benjaminp, @ericsnowcurrently

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-04-17.23:05:59.931>
created_at = <Date 2012-04-16.11:33:01.334>
labels = ['interpreter-core', 'type-bug']
title = 'old-style (level=-1) importing broken after importlib changes'
updated_at = <Date 2012-04-17.23:05:59.930>
user = 'https://github.com/scoder'

bugs.python.org fields:

activity = <Date 2012-04-17.23:05:59.930>
actor = 'brett.cannon'
assignee = 'brett.cannon'
closed = True
closed_date = <Date 2012-04-17.23:05:59.931>
closer = 'brett.cannon'
components = ['Interpreter Core']
creation = <Date 2012-04-16.11:33:01.334>
creator = 'scoder'
dependencies = []
files = []
hgrepos = []
issue_num = 14592
keywords = []
message_count = 12.0
messages = ['158409', '158437', '158438', '158466', '158469', '158492', '158502', '158529', '158530', '158553', '158571', '158572']
nosy_count = 6.0
nosy_names = ['brett.cannon', 'scoder', 'benjamin.peterson', 'Arfrever', 'python-dev', 'eric.snow']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue14592'
versions = ['Python 3.3']

@scoder
Copy link
Contributor Author

scoder commented Apr 16, 2012

Up to the early Py3.3 developer versions, calling __import__() with a level of -1 worked as in Python 2, i.e. it tried a relative import first and then a global import.

This no longer seems to be available after the importlib rewrite (e.g. as of rev f341b99bb370).

@scoder scoder added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Apr 16, 2012
@benjaminp
Copy link
Contributor

Relative imports are no longer supported in python 3, so this makes sense.

@benjaminp
Copy link
Contributor

By "relative" I meant "sibling".

@brettcannon
Copy link
Member

What Benjamin said. PEP-328 should have done away with relative imports, but somehow the __import__() function itself was not updated even though its docs were changed to say that index defaulted to 0.

So this isn't actually a regressions because of importlib but actually just the implicit fixing of a bug. I was going to make sure to mention it in the "What's New" entry, but I will also add something to Misc/NEWS.

@brettcannon
Copy link
Member

I should also mention that the support for -1 indexing in Python 3 was weird because support was partially removed, but some 'if' checks only did < 1 and so negative indices didn't trigger an error.

@scoder
Copy link
Contributor Author

scoder commented Apr 16, 2012

It turns out that it wasn't all that hard to work around. Calling __import__ twice seems to do the trick. It's more overhead, but if people want speed, they can just be explicit about what kind of import they actually mean.

So I wouldn't mind considering this an accidental left-over, and given that this behaviour has officially been removed for source code imports in 3.0 and taken out of documentation since then, it should be enough to consider its (partial) support in __import__ a bug and document it as being fixed in 3.3.

@brettcannon
Copy link
Member

Yeah, the fix is dead-simple, import with level=1 and if that fails import with level=0.

I plan to cover this specific issue in the "What's New" for Python 3.3.

@scoder
Copy link
Contributor Author

scoder commented Apr 17, 2012

Yeah, the fix is dead-simple, import with level=1 and if that fails import with level=0.

With one caveat: relative imports don't work outside of packages, so the
importing code has to know when it's in a package or not. Otherwise, the
relative import would raise an exception (not an ImportError).

Interesting enough, I now get this when trying it at the prompt:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: error return without exception set

@scoder
Copy link
Contributor Author

scoder commented Apr 17, 2012

Hmm, interesting - it stripped the command from my e-mail. I was doing this:

>>> __import__("sys", level=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: error return without exception set

@brettcannon
Copy link
Member

Detecting if you are in a package is as simple as '.' in __name__ or hasattr(mod, '__path__') or alternatively for the last guard, __name__ == __package__.

As for the failure, I will have a look.

@python-dev
Copy link
Mannequin

python-dev mannequin commented Apr 17, 2012

New changeset 68f9ad6a3b13 by Brett Cannon in branch 'default':
Issue bpo-14592: A relative import will raise a KeyError if __package__
http://hg.python.org/cpython/rev/68f9ad6a3b13

@brettcannon
Copy link
Member

SystemError fixed (stemming from a misunderstanding of what PyDict_GetItemWithError() did).

@brettcannon brettcannon self-assigned this Apr 17, 2012
@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

3 participants