gh-117865: Speedup import of inspect module (#144756)

Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
This commit is contained in:
Daniel Hollas 2026-03-02 21:39:07 +00:00 committed by GitHub
parent 46c5c57226
commit ea90b032a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 4 deletions

View file

@ -153,9 +153,9 @@
import itertools
import linecache
import os
import re
lazy import re
import sys
import tokenize
lazy import tokenize
import token
import types
import functools
@ -163,9 +163,9 @@
from keyword import iskeyword
from operator import attrgetter
from collections import namedtuple, OrderedDict
from weakref import ref as make_weakref
from _weakref import ref as make_weakref
# Create constants for the compiler flags in Include/code.h
# Create constants for the compiler flags in Include/cpython/code.h
# We try to get them from dis to avoid duplication
mod_dict = globals()
for k, v in dis.COMPILER_FLAG_NAMES.items():

View file

@ -173,6 +173,15 @@ def __get__(self, instance, owner):
return self.func.__get__(instance, owner)
class TestImportTime(unittest.TestCase):
@cpython_only
def test_lazy_import(self):
import_helper.ensure_lazy_imports(
"inspect", {"re", "tokenize"}
)
class TestPredicates(IsTestBase):
def test_excluding_predicates(self):

View file

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