@@ -361,21 +361,36 @@ tok_concatenate_interactive_new_line(struct tok_state *tok, const char *line) {
361
361
return 0 ;
362
362
}
363
363
364
-
365
- /* Traverse and update all f-string buffers with the value */
364
+ /* Traverse and remember all f-string buffers, in order to be able to restore
365
+ them after reallocating tok->buf */
366
366
static void
367
- update_fstring_buffers (struct tok_state * tok , char value , int regular , int multiline )
367
+ remember_fstring_buffers (struct tok_state * tok )
368
368
{
369
369
int index ;
370
370
tokenizer_mode * mode ;
371
371
372
372
for (index = tok -> tok_mode_stack_index ; index >= 0 ; -- index ) {
373
373
mode = & (tok -> tok_mode_stack [index ]);
374
- if (regular && mode -> f_string_start != NULL ) {
375
- mode -> f_string_start += value ;
374
+ if (mode -> kind == TOK_FSTRING_MODE ) {
375
+ mode -> f_string_start_offset = mode -> f_string_start - tok -> buf ;
376
+ mode -> f_string_multi_line_start_offset = mode -> f_string_multi_line_start - tok -> buf ;
376
377
}
377
- if (multiline && mode -> f_string_multi_line_start != NULL ) {
378
- mode -> f_string_multi_line_start += value ;
378
+ }
379
+ }
380
+
381
+
382
+ /* Traverse and restore all f-string buffers after reallocating tok->buf */
383
+ static void
384
+ restore_fstring_buffers (struct tok_state * tok )
385
+ {
386
+ int index ;
387
+ tokenizer_mode * mode ;
388
+
389
+ for (index = tok -> tok_mode_stack_index ; index >= 0 ; -- index ) {
390
+ mode = & (tok -> tok_mode_stack [index ]);
391
+ if (mode -> kind == TOK_FSTRING_MODE ) {
392
+ mode -> f_string_start = tok -> buf + mode -> f_string_start_offset ;
393
+ mode -> f_string_multi_line_start = tok -> buf + mode -> f_string_multi_line_start_offset ;
379
394
}
380
395
}
381
396
}
@@ -476,7 +491,7 @@ tok_reserve_buf(struct tok_state *tok, Py_ssize_t size)
476
491
Py_ssize_t start = tok -> start == NULL ? -1 : tok -> start - tok -> buf ;
477
492
Py_ssize_t line_start = tok -> start == NULL ? -1 : tok -> line_start - tok -> buf ;
478
493
Py_ssize_t multi_line_start = tok -> multi_line_start - tok -> buf ;
479
- update_fstring_buffers (tok , - * tok -> buf , /*regular=*/ 1 , /*multiline=*/ 1 );
494
+ remember_fstring_buffers (tok );
480
495
newbuf = (char * )PyMem_Realloc (newbuf , newsize );
481
496
if (newbuf == NULL ) {
482
497
tok -> done = E_NOMEM ;
@@ -489,7 +504,7 @@ tok_reserve_buf(struct tok_state *tok, Py_ssize_t size)
489
504
tok -> start = start < 0 ? NULL : tok -> buf + start ;
490
505
tok -> line_start = line_start < 0 ? NULL : tok -> buf + line_start ;
491
506
tok -> multi_line_start = multi_line_start < 0 ? NULL : tok -> buf + multi_line_start ;
492
- update_fstring_buffers (tok , * tok -> buf , /*regular=*/ 1 , /*multiline=*/ 1 );
507
+ restore_fstring_buffers (tok );
493
508
}
494
509
return 1 ;
495
510
}
@@ -1051,7 +1066,7 @@ tok_underflow_interactive(struct tok_state *tok) {
1051
1066
}
1052
1067
else if (tok -> start != NULL ) {
1053
1068
Py_ssize_t cur_multi_line_start = tok -> multi_line_start - tok -> buf ;
1054
- update_fstring_buffers (tok , - * tok -> buf , /*regular=*/ 0 , /*multiline=*/ 1 );
1069
+ remember_fstring_buffers (tok );
1055
1070
size_t size = strlen (newtok );
1056
1071
ADVANCE_LINENO ();
1057
1072
if (!tok_reserve_buf (tok , size + 1 )) {
@@ -1064,7 +1079,7 @@ tok_underflow_interactive(struct tok_state *tok) {
1064
1079
PyMem_Free (newtok );
1065
1080
tok -> inp += size ;
1066
1081
tok -> multi_line_start = tok -> buf + cur_multi_line_start ;
1067
- update_fstring_buffers (tok , * tok -> buf , /*regular=*/ 0 , /*multiline=*/ 1 );
1082
+ restore_fstring_buffers (tok );
1068
1083
}
1069
1084
else {
1070
1085
ADVANCE_LINENO ();
@@ -2207,6 +2222,8 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
2207
2222
the_current_tok -> f_string_quote_size = quote_size ;
2208
2223
the_current_tok -> f_string_start = tok -> start ;
2209
2224
the_current_tok -> f_string_multi_line_start = tok -> line_start ;
2225
+ the_current_tok -> f_string_start_offset = -1 ;
2226
+ the_current_tok -> f_string_multi_line_start_offset = -1 ;
2210
2227
the_current_tok -> last_expr_buffer = NULL ;
2211
2228
the_current_tok -> last_expr_size = 0 ;
2212
2229
the_current_tok -> last_expr_end = -1 ;
0 commit comments