Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
4e244250
Commit
4e244250
authored
Mar 11, 2018
by
Serhiy Storchaka
Committed by
GitHub
Mar 11, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32946: Speed up "from ... import ..." from non-packages. (GH-5873)
parent
b931bd0a
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
302 additions
and
288 deletions
+302
-288
Lib/importlib/_bootstrap.py
Lib/importlib/_bootstrap.py
+27
-26
Misc/NEWS.d/next/Core and Builtins/2018-02-25-10-52-40.bpo-32946.Lo09rG.rst
...ore and Builtins/2018-02-25-10-52-40.bpo-32946.Lo09rG.rst
+2
-0
Python/import.c
Python/import.c
+15
-4
Python/importlib.h
Python/importlib.h
+258
-258
No files found.
Lib/importlib/_bootstrap.py
View file @
4e244250
...
...
@@ -1016,31 +1016,30 @@ def _handle_fromlist(module, fromlist, import_, *, recursive=False):
"""
# The hell that is fromlist ...
# If a package was imported, try to import stuff from fromlist.
if
hasattr
(
module
,
'__path__'
):
for
x
in
fromlist
:
if
not
isinstance
(
x
,
str
):
if
recursive
:
where
=
module
.
__name__
+
'.__all__'
else
:
where
=
"``from list''"
raise
TypeError
(
f"Item in
{
where
}
must be str, "
f"not
{
type
(
x
).
__name__
}
"
)
elif
x
==
'*'
:
if
not
recursive
and
hasattr
(
module
,
'__all__'
):
_handle_fromlist
(
module
,
module
.
__all__
,
import_
,
recursive
=
True
)
elif
not
hasattr
(
module
,
x
):
from_name
=
'{}.{}'
.
format
(
module
.
__name__
,
x
)
try
:
_call_with_frames_removed
(
import_
,
from_name
)
except
ModuleNotFoundError
as
exc
:
# Backwards-compatibility dictates we ignore failed
# imports triggered by fromlist for modules that don't
# exist.
if
(
exc
.
name
==
from_name
and
sys
.
modules
.
get
(
from_name
,
_NEEDS_LOADING
)
is
not
None
):
continue
raise
for
x
in
fromlist
:
if
not
isinstance
(
x
,
str
):
if
recursive
:
where
=
module
.
__name__
+
'.__all__'
else
:
where
=
"``from list''"
raise
TypeError
(
f"Item in
{
where
}
must be str, "
f"not
{
type
(
x
).
__name__
}
"
)
elif
x
==
'*'
:
if
not
recursive
and
hasattr
(
module
,
'__all__'
):
_handle_fromlist
(
module
,
module
.
__all__
,
import_
,
recursive
=
True
)
elif
not
hasattr
(
module
,
x
):
from_name
=
'{}.{}'
.
format
(
module
.
__name__
,
x
)
try
:
_call_with_frames_removed
(
import_
,
from_name
)
except
ModuleNotFoundError
as
exc
:
# Backwards-compatibility dictates we ignore failed
# imports triggered by fromlist for modules that don't
# exist.
if
(
exc
.
name
==
from_name
and
sys
.
modules
.
get
(
from_name
,
_NEEDS_LOADING
)
is
not
None
):
continue
raise
return
module
...
...
@@ -1102,8 +1101,10 @@ def __import__(name, globals=None, locals=None, fromlist=(), level=0):
# Slice end needs to be positive to alleviate need to special-case
# when ``'.' not in name``.
return
sys
.
modules
[
module
.
__name__
[:
len
(
module
.
__name__
)
-
cut_off
]]
el
se
:
el
if
hasattr
(
module
,
'__path__'
)
:
return
_handle_fromlist
(
module
,
fromlist
,
_gcd_import
)
else
:
return
module
def
_builtin_from_name
(
name
):
...
...
Misc/NEWS.d/next/Core and Builtins/2018-02-25-10-52-40.bpo-32946.Lo09rG.rst
0 → 100644
View file @
4e244250
Importing names from already imported module with "from ... import ..." is
now 30% faster if the module is not a package.
Python/import.c
View file @
4e244250
...
...
@@ -1800,10 +1800,21 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
}
}
else
{
final_mod
=
_PyObject_CallMethodIdObjArgs
(
interp
->
importlib
,
&
PyId__handle_fromlist
,
mod
,
fromlist
,
interp
->
import_func
,
NULL
);
_Py_IDENTIFIER
(
__path__
);
PyObject
*
path
;
if
(
_PyObject_LookupAttrId
(
mod
,
&
PyId___path__
,
&
path
)
<
0
)
{
goto
error
;
}
if
(
path
)
{
Py_DECREF
(
path
);
final_mod
=
_PyObject_CallMethodIdObjArgs
(
interp
->
importlib
,
&
PyId__handle_fromlist
,
mod
,
fromlist
,
interp
->
import_func
,
NULL
);
}
else
{
final_mod
=
mod
;
Py_INCREF
(
mod
);
}
}
error:
...
...
Python/importlib.h
View file @
4e244250
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment