| 270 | return "".join(self.tokens) |
| 271 | |
| 272 | def compat(self, token, iterable): |
| 273 | indents = [] |
| 274 | toks_append = self.tokens.append |
| 275 | startline = token[0] in (NEWLINE, NL) |
| 276 | prevstring = False |
| 277 | in_fstring_or_tstring = 0 |
| 278 | |
| 279 | for tok in _itertools.chain([token], iterable): |
| 280 | toknum, tokval = tok[:2] |
| 281 | if toknum == ENCODING: |
| 282 | self.encoding = tokval |
| 283 | continue |
| 284 | |
| 285 | if toknum in (NAME, NUMBER): |
| 286 | tokval += ' ' |
| 287 | |
| 288 | # Insert a space between two consecutive strings |
| 289 | if toknum == STRING: |
| 290 | if prevstring: |
| 291 | tokval = ' ' + tokval |
| 292 | prevstring = True |
| 293 | else: |
| 294 | prevstring = False |
| 295 | |
| 296 | if toknum in {FSTRING_START, TSTRING_START}: |
| 297 | in_fstring_or_tstring += 1 |
| 298 | elif toknum in {FSTRING_END, TSTRING_END}: |
| 299 | in_fstring_or_tstring -= 1 |
| 300 | if toknum == INDENT: |
| 301 | indents.append(tokval) |
| 302 | continue |
| 303 | elif toknum == DEDENT: |
| 304 | indents.pop() |
| 305 | continue |
| 306 | elif toknum in (NEWLINE, NL): |
| 307 | startline = True |
| 308 | elif startline and indents: |
| 309 | toks_append(indents[-1]) |
| 310 | startline = False |
| 311 | elif toknum in {FSTRING_MIDDLE, TSTRING_MIDDLE}: |
| 312 | tokval = self.escape_brackets(tokval) |
| 313 | |
| 314 | # Insert a space between two consecutive brackets if we are in an f-string or t-string |
| 315 | if tokval in {"{", "}"} and self.tokens and self.tokens[-1] == tokval and in_fstring_or_tstring: |
| 316 | tokval = ' ' + tokval |
| 317 | |
| 318 | # Insert a space between two consecutive f-strings |
| 319 | if toknum in (STRING, FSTRING_START) and self.prev_type in (STRING, FSTRING_END): |
| 320 | self.tokens.append(" ") |
| 321 | |
| 322 | toks_append(tokval) |
| 323 | self.prev_type = toknum |
| 324 | |
| 325 | |
| 326 | def untokenize(iterable): |