Skip to content

Commit 9006af2

Browse files
AlexWaygooddanielhollas
authored andcommitted
Speedup import inspect
1 parent e66f4a5 commit 9006af2

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Lib/inspect.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,25 +145,22 @@
145145
import abc
146146
from annotationlib import Format, ForwardRef
147147
from annotationlib import get_annotations # re-exported
148-
import ast
149148
import dis
150149
import collections.abc
151150
import enum
152151
import importlib.machinery
153152
import itertools
154153
import linecache
155154
import os
156-
import re
157155
import sys
158-
import tokenize
159156
import token
160157
import types
161158
import functools
162159
import builtins
163160
from keyword import iskeyword
164161
from operator import attrgetter
165162
from collections import namedtuple, OrderedDict
166-
from weakref import ref as make_weakref
163+
from _weakref import ref as make_weakref
167164

168165
# Create constants for the compiler flags in Include/code.h
169166
# We try to get them from dis to avoid duplication
@@ -1089,6 +1086,8 @@ def __init__(self):
10891086
self.body_col0 = None
10901087

10911088
def tokeneater(self, type, token, srowcol, erowcol, line):
1089+
import tokenize
1090+
10921091
if not self.started and not self.indecorator:
10931092
if type in (tokenize.INDENT, tokenize.COMMENT, tokenize.NL):
10941093
pass
@@ -1139,6 +1138,8 @@ def tokeneater(self, type, token, srowcol, erowcol, line):
11391138
def getblock(lines):
11401139
"""Extract the block of code at the top of the given list of lines."""
11411140
blockfinder = BlockFinder()
1141+
import tokenize
1142+
11421143
try:
11431144
tokens = tokenize.generate_tokens(iter(lines).__next__)
11441145
for _token in tokens:
@@ -1367,6 +1368,7 @@ def formatannotation(annotation, base_module=None, *, quote_annotation_strings=T
13671368
def repl(match):
13681369
text = match.group()
13691370
return text.removeprefix('typing.')
1371+
import re
13701372
return re.sub(r'[\w\.]+', repl, repr(annotation))
13711373
if isinstance(annotation, types.GenericAlias):
13721374
return str(annotation)
@@ -2116,6 +2118,8 @@ def _signature_strip_non_python_syntax(signature):
21162118

21172119
lines = [l.encode('ascii') for l in signature.split('\n') if l]
21182120
generator = iter(lines).__next__
2121+
2122+
import tokenize
21192123
token_stream = tokenize.tokenize(generator)
21202124

21212125
text = []
@@ -2151,10 +2155,10 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
21512155
"""Private helper to parse content of '__text_signature__'
21522156
and return a Signature based on it.
21532157
"""
2154-
Parameter = cls._parameter_cls
2158+
import ast
21552159

2160+
Parameter = cls._parameter_cls
21562161
clean_signature, self_parameter = _signature_strip_non_python_syntax(s)
2157-
21582162
program = "def foo" + clean_signature + ": pass"
21592163

21602164
try:

0 commit comments

Comments
 (0)