gh-117865: Speedup import of `inspect` module (#144756) · python/cpython@ea90b03

GitHub

Original file line numberDiff line numberDiff line change@@ -153,19 +153,19 @@

153153importitertools

154154importlinecache

155155importos

156-importre

156+lazyimportre

157157importsys

158-importtokenize

158+lazyimporttokenize

159159importtoken

160160importtypes

161161importfunctools

162162importbuiltins

163163fromkeywordimportiskeyword

164164fromoperatorimportattrgetter

165165fromcollectionsimportnamedtuple, OrderedDict

166-fromweakrefimportrefasmake_weakref

166+from_weakrefimportrefasmake_weakref

167167168-# Create constants for the compiler flags in Include/code.h

168+# Create constants for the compiler flags in Include/cpython/code.h

169169# We try to get them from dis to avoid duplication

170170mod_dict=globals()

171171fork, vindis.COMPILER_FLAG_NAMES.items():

Original file line numberDiff line numberDiff line change@@ -173,6 +173,15 @@ def __get__(self, instance, owner):

173173returnself.func.__get__(instance, owner)

174174175175176+classTestImportTime(unittest.TestCase):

177+178+@cpython_only

179+deftest_lazy_import(self):

180+import_helper.ensure_lazy_imports(

181+"inspect", {"re", "tokenize"}

182+ )

183+184+176185classTestPredicates(IsTestBase):

177186178187deftest_excluding_predicates(self):

Original file line numberDiff line numberDiff line change@@ -0,0 +1 @@

1+Reduce the import time of :mod:`inspect` module by ~20%.