|
145 | 145 | import abc |
146 | 146 | from annotationlib import Format, ForwardRef |
147 | 147 | from annotationlib import get_annotations # re-exported |
148 | | -import ast |
149 | 148 | import dis |
150 | 149 | import collections.abc |
151 | 150 | import enum |
152 | 151 | import importlib.machinery |
153 | 152 | import itertools |
154 | 153 | import linecache |
155 | 154 | import os |
156 | | -import re |
157 | 155 | import sys |
158 | | -import tokenize |
159 | 156 | import token |
160 | 157 | import types |
161 | 158 | import functools |
162 | 159 | import builtins |
163 | 160 | from keyword import iskeyword |
164 | 161 | from operator import attrgetter |
165 | 162 | from collections import namedtuple, OrderedDict |
166 | | -from weakref import ref as make_weakref |
| 163 | +from _weakref import ref as make_weakref |
167 | 164 |
|
168 | 165 | # Create constants for the compiler flags in Include/code.h |
169 | 166 | # We try to get them from dis to avoid duplication |
@@ -1089,6 +1086,8 @@ def __init__(self): |
1089 | 1086 | self.body_col0 = None |
1090 | 1087 |
|
1091 | 1088 | def tokeneater(self, type, token, srowcol, erowcol, line): |
| 1089 | + import tokenize |
| 1090 | + |
1092 | 1091 | if not self.started and not self.indecorator: |
1093 | 1092 | if type in (tokenize.INDENT, tokenize.COMMENT, tokenize.NL): |
1094 | 1093 | pass |
@@ -1139,6 +1138,8 @@ def tokeneater(self, type, token, srowcol, erowcol, line): |
1139 | 1138 | def getblock(lines): |
1140 | 1139 | """Extract the block of code at the top of the given list of lines.""" |
1141 | 1140 | blockfinder = BlockFinder() |
| 1141 | + import tokenize |
| 1142 | + |
1142 | 1143 | try: |
1143 | 1144 | tokens = tokenize.generate_tokens(iter(lines).__next__) |
1144 | 1145 | for _token in tokens: |
@@ -1367,6 +1368,7 @@ def formatannotation(annotation, base_module=None, *, quote_annotation_strings=T |
1367 | 1368 | def repl(match): |
1368 | 1369 | text = match.group() |
1369 | 1370 | return text.removeprefix('typing.') |
| 1371 | + import re |
1370 | 1372 | return re.sub(r'[\w\.]+', repl, repr(annotation)) |
1371 | 1373 | if isinstance(annotation, types.GenericAlias): |
1372 | 1374 | return str(annotation) |
@@ -2116,6 +2118,8 @@ def _signature_strip_non_python_syntax(signature): |
2116 | 2118 |
|
2117 | 2119 | lines = [l.encode('ascii') for l in signature.split('\n') if l] |
2118 | 2120 | generator = iter(lines).__next__ |
| 2121 | + |
| 2122 | + import tokenize |
2119 | 2123 | token_stream = tokenize.tokenize(generator) |
2120 | 2124 |
|
2121 | 2125 | text = [] |
@@ -2151,10 +2155,10 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True): |
2151 | 2155 | """Private helper to parse content of '__text_signature__' |
2152 | 2156 | and return a Signature based on it. |
2153 | 2157 | """ |
2154 | | - Parameter = cls._parameter_cls |
| 2158 | + import ast |
2155 | 2159 |
|
| 2160 | + Parameter = cls._parameter_cls |
2156 | 2161 | clean_signature, self_parameter = _signature_strip_non_python_syntax(s) |
2157 | | - |
2158 | 2162 | program = "def foo" + clean_signature + ": pass" |
2159 | 2163 |
|
2160 | 2164 | try: |
|
0 commit comments