MCPcopy Index your code
hub / github.com/python/cpython / recover_unterminated_string

Function recover_unterminated_string

Lib/_pyrepl/utils.py:126–164  ·  view source on GitHub ↗
(
    exc: tokenize.TokenError,
    line_lengths: list[int],
    last_emitted: ColorSpan | None,
    buffer: str,
)

Source from the content-addressed store, hash-verified

124
125
126def recover_unterminated_string(
127 exc: tokenize.TokenError,
128 line_lengths: list[int],
129 last_emitted: ColorSpan | None,
130 buffer: str,
131) -> Iterator[ColorSpan]:
132 msg, loc = exc.args
133 if loc is None:
134 return
135
136 line_no, column = loc
137
138 if msg.startswith(
139 (
140 "unterminated string literal",
141 "unterminated f-string literal",
142 "unterminated t-string literal",
143 "EOF in multi-line string",
144 "unterminated triple-quoted f-string literal",
145 "unterminated triple-quoted t-string literal",
146 )
147 ):
148 start = line_lengths[line_no - 1] + column - 1
149 end = line_lengths[-1] - 1
150
151 # in case FSTRING_START was already emitted
152 if last_emitted and start <= last_emitted.span.start:
153 trace("before last emitted = {s}", s=start)
154 start = last_emitted.span.end + 1
155
156 span = Span(start, end)
157 trace("yielding span {a} -> {b}", a=span.start, b=span.end)
158 yield ColorSpan(span, "string")
159 else:
160 trace(
161 "unhandled token error({buffer}) = {te}",
162 buffer=repr(buffer),
163 te=str(exc),
164 )
165
166
167def gen_colors_from_token_stream(

Callers 1

gen_colorsFunction · 0.85

Calls 5

traceFunction · 0.90
SpanClass · 0.85
ColorSpanClass · 0.85
strFunction · 0.85
startswithMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…