mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
bpo-41718: runpy now imports pkgutil in functions (GH-24996)
Reduce the number of modules imported by "python3 -m module". The runpy module no longer imports at startup (in the module body), but only in functions using it: _get_code_from_file() and run_path().
This commit is contained in:
parent
d72e8d4875
commit
bd9154a495
1 changed files with 2 additions and 1 deletions
|
@ -16,7 +16,6 @@ import importlib.util
|
||||||
import io
|
import io
|
||||||
import types
|
import types
|
||||||
import os
|
import os
|
||||||
from pkgutil import read_code, get_importer
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"run_module", "run_path",
|
"run_module", "run_path",
|
||||||
|
@ -233,6 +232,7 @@ def _get_main_module_details(error=ImportError):
|
||||||
|
|
||||||
def _get_code_from_file(run_name, fname):
|
def _get_code_from_file(run_name, fname):
|
||||||
# Check for a compiled file first
|
# Check for a compiled file first
|
||||||
|
from pkgutil import read_code
|
||||||
decoded_path = os.path.abspath(os.fsdecode(fname))
|
decoded_path = os.path.abspath(os.fsdecode(fname))
|
||||||
with io.open_code(decoded_path) as f:
|
with io.open_code(decoded_path) as f:
|
||||||
code = read_code(f)
|
code = read_code(f)
|
||||||
|
@ -255,6 +255,7 @@ def run_path(path_name, init_globals=None, run_name=None):
|
||||||
if run_name is None:
|
if run_name is None:
|
||||||
run_name = "<run_path>"
|
run_name = "<run_path>"
|
||||||
pkg_name = run_name.rpartition(".")[0]
|
pkg_name = run_name.rpartition(".")[0]
|
||||||
|
from pkgutil import get_importer
|
||||||
importer = get_importer(path_name)
|
importer = get_importer(path_name)
|
||||||
# Trying to avoid importing imp so as to not consume the deprecation warning.
|
# Trying to avoid importing imp so as to not consume the deprecation warning.
|
||||||
is_NullImporter = False
|
is_NullImporter = False
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue