cpython: bbaab666e6c7 (original) (raw)

Mercurial > cpython

changeset 75048:bbaab666e6c7

Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a new importlib.invalidate_caches() function. importlib is now often faster than imp.find_module() at finding modules. [#14043]

Antoine Pitrou solipsis@pitrou.net
date Mon, 20 Feb 2012 01:48:16 +0100
parents 5b4b70bd2b6f
children dee36bfa3f8f
files Doc/library/importlib.rst Lib/importlib/__init__.py Lib/importlib/_bootstrap.py Lib/importlib/test/import_/test_path.py Lib/test/test_import.py Lib/test/test_reprlib.py Misc/NEWS
diffstat 7 files changed, 90 insertions(+), 50 deletions(-)[+] [-] Doc/library/importlib.rst 8 Lib/importlib/__init__.py 4 Lib/importlib/_bootstrap.py 111 Lib/importlib/test/import_/test_path.py 4 Lib/test/test_import.py 8 Lib/test/test_reprlib.py 2 Misc/NEWS 3

line wrap: on

line diff

--- a/Doc/library/importlib.rst +++ b/Doc/library/importlib.rst @@ -86,6 +86,14 @@ Functions that was imported (e.g. pkg.mod), while :func:__import__ returns the top-level package or module (e.g. pkg). +.. function:: invalidate_caches() +

:mod:importlib.abc -- Abstract base classes related to import ---------------------------------------------------------------

--- a/Lib/importlib/init.py +++ b/Lib/importlib/init.py @@ -18,7 +18,7 @@ References on import: http://www.python.org/dev/peps/pep-0328[](#l2.4) """ -all = ['import', 'import_module'] +all = ['import', 'import_module', 'invalidate_caches'] from . import _bootstrap @@ -37,7 +37,7 @@ import sys

Public API #########################################################

-from ._bootstrap import import +from ._bootstrap import import, invalidate_caches def import_module(name, package=None):

--- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -21,31 +21,16 @@ work. One should use importlib as the pu CASE_INSENSITIVE_PLATFORMS = 'win', 'cygwin', 'darwin' -def _case_insensitive_ok(directory, check):

-

+def _relax_case():

- -def _case_sensitive_ok(directory, check):

-

-

- -_case_ok = None

TODO: Expose from marshal

@@ -172,6 +157,18 @@ code_type = type(_wrap.code)

Finder/loader utility code ##################################################

+_cache_refresh = 0 + +def invalidate_caches():

+

+ + def set_package(fxn): """Set package on the returned module.""" def set_package_wrapper(*args, **kwargs): @@ -708,7 +705,7 @@ class PathFinder: """ if path == '':

@@ -760,29 +757,55 @@ class _FileFinder: for suffix in detail.suffixes) self.packages = packages self.modules = modules

def find_module(self, fullname): """Try to find a loader for the specified module.""" tail_module = fullname.rpartition('.')[2]

+ + class _SourceFinderDetails: loader = _SourceFileLoader @@ -1060,7 +1083,7 @@ def _setup(sys_module, imp_module): modules, those two modules must be explicitly passed in. """

def _install(sys_module, imp_module):

--- a/Lib/importlib/test/import_/test_path.py +++ b/Lib/importlib/test/import_/test_path.py @@ -78,11 +78,11 @@ class FinderTests(unittest.TestCase): path = '' module = '' importer = util.mock_modules(module)

class DefaultPathFinderTests(unittest.TestCase):

--- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -2,6 +2,7 @@ import builtins import imp from importlib.test.import_ import test_relative_imports from importlib.test.import_ import util as importlib_util +import importlib import marshal import os import platform @@ -34,6 +35,7 @@ class ImportTests(unittest.TestCase): def setUp(self): remove_files(TESTFN)

def tearDown(self): unload(TESTFN) @@ -107,6 +109,7 @@ class ImportTests(unittest.TestCase): create_empty_file(fname) fn = imp.cache_from_source(fname) unlink(fn)

@@ -260,6 +263,7 @@ class ImportTests(unittest.TestCase): os.remove(source) del sys.modules[TESTFN] make_legacy_pyc(source)

@@ -358,6 +362,7 @@ func_filename = func.code.co_filenam with open(self.file_name, "w") as f: f.write(self.module_source) sys.path.insert(0, self.dir_name)

def tearDown(self): sys.path[:] = self.sys_path @@ -552,6 +557,7 @@ class PycacheTests(unittest.TestCase): with open(self.source, 'w') as fp: print('# This is a test file written by test_import.py', file=fp) sys.path.insert(0, os.curdir)

def tearDown(self): assert sys.path[0] == os.curdir, 'Unexpected sys.path[0]' @@ -599,6 +605,7 @@ class PycacheTests(unittest.TestCase): pyc_file = make_legacy_pyc(self.source) os.remove(self.source) unload(TESTFN)

@@ -619,6 +626,7 @@ class PycacheTests(unittest.TestCase): pyc_file = make_legacy_pyc(self.source) os.remove(self.source) unload(TESTFN)

--- a/Lib/test/test_reprlib.py +++ b/Lib/test/test_reprlib.py @@ -6,6 +6,7 @@ import sys import os import shutil +import importlib import unittest from test.support import run_unittest, create_empty_file @@ -212,6 +213,7 @@ class LongReprTest(unittest.TestCase): # Remember where we are self.here = os.getcwd() sys.path.insert(0, self.here)

def tearDown(self): actions = []

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -469,6 +469,9 @@ Core and Builtins Library ------- +- Issue #14043: Speed up importlib's _FileFinder by at least 8x, and add a