Skip to content

Commit ea90b03

Browse files
pythongh-117865: Speedup import of inspect module (python#144756)
Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
1 parent 46c5c57 commit ea90b03

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

Lib/inspect.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,19 @@
153153
import itertools
154154
import linecache
155155
import os
156-
import re
156+
lazy import re
157157
import sys
158-
import tokenize
158+
lazy import tokenize
159159
import token
160160
import types
161161
import functools
162162
import builtins
163163
from keyword import iskeyword
164164
from operator import attrgetter
165165
from collections import namedtuple, OrderedDict
166-
from weakref import ref as make_weakref
166+
from _weakref import ref as make_weakref
167167

168-
# 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
170170
mod_dict = globals()
171171
for k, v in dis.COMPILER_FLAG_NAMES.items():

Lib/test/test_inspect/test_inspect.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ def __get__(self, instance, owner):
173173
return self.func.__get__(instance, owner)
174174

175175

176+
class TestImportTime(unittest.TestCase):
177+
178+
@cpython_only
179+
def test_lazy_import(self):
180+
import_helper.ensure_lazy_imports(
181+
"inspect", {"re", "tokenize"}
182+
)
183+
184+
176185
class TestPredicates(IsTestBase):
177186

178187
def test_excluding_predicates(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reduce the import time of :mod:`inspect` module by ~20%.

0 commit comments

Comments
 (0)