Skip to content

bpo-32946: Speed up "from ... import ..." from non-packages.#5873

Merged
serhiy-storchaka merged 1 commit into
python:masterfrom
serhiy-storchaka:import-from-nonpackage
Mar 11, 2018
Merged

bpo-32946: Speed up "from ... import ..." from non-packages.#5873
serhiy-storchaka merged 1 commit into
python:masterfrom
serhiy-storchaka:import-from-nonpackage

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Feb 25, 2018

Copy link
Copy Markdown
Member

@serhiy-storchaka serhiy-storchaka added the performance Performance or resource usage label Feb 25, 2018
@serhiy-storchaka
serhiy-storchaka requested a review from a team February 25, 2018 09:05
_handle_fromlist(module, module.__all__, import_,
recursive=True)
elif not hasattr(module, x):
from_name = '{}.{}'.format(module.__name__, x)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attempt to import missing names as submodules should still be restricted to modules with a __path__ attribute. Something like try_import = hasattr(module, "__path__") before the loop, and then modify the condition on this branch to elif try_import and not hasattr(module, x):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Since we know at the point of calling whether module is a package or not, try_import could also be a parameter that defaults to False, with __import__ setting it to True as part of the call)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this issue is that this check is moved out from this function to calling places.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the issue for improving the error message for non-str objects in regular modules, calling this is one of the options for handling the enhancement. However, we only want to allow the first two branches for that use case, not the nested import one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I suggested this. But it would prevent the optimization made by this PR.

@ncoghlan ncoghlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 from me - we can consider the consequences for error reporting enhancements as part of https://bugs.python.org/issue32932

@serhiy-storchaka
serhiy-storchaka merged commit 4e24425 into python:master Mar 11, 2018
@serhiy-storchaka
serhiy-storchaka deleted the import-from-nonpackage branch March 11, 2018 08:52
jo2y pushed a commit to jo2y/cpython that referenced this pull request Mar 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants