@@ -295,7 +295,7 @@ def parse_to_binary_ast(
295295 platform = options .platform ,
296296 always_true = options .always_true ,
297297 always_false = options .always_false ,
298- cache_version = 4 ,
298+ cache_version = 5 ,
299299 )
300300 return (
301301 ast_bytes ,
@@ -1348,6 +1348,7 @@ def read_expression(state: State, data: ReadBuffer) -> Expression:
13481348 return ce
13491349 elif tag == nodes .STR_EXPR :
13501350 se = StrExpr (read_str (data ))
1351+ se .has_surrogates = read_bool (data )
13511352 read_loc (data , se )
13521353 expect_end_tag (data )
13531354 return se
@@ -1437,7 +1438,7 @@ def read_expression(state: State, data: ReadBuffer) -> Expression:
14371438 s = StrExpr (read_str (data ))
14381439 read_loc (data , s )
14391440 fitems .append (s )
1440- expr = build_fstring_join (data , fitems )
1441+ expr = build_fstring_join (data , fitems , set_has_surrogates = True )
14411442 expect_end_tag (data )
14421443 return expr
14431444 elif tag == nodes .LIST_COMPREHENSION :
@@ -1534,6 +1535,7 @@ def read_expression(state: State, data: ReadBuffer) -> Expression:
15341535 read_loc (data , s )
15351536 titems .append (s )
15361537 expr = TemplateStrExpr (titems )
1538+ expr .has_surrogates = read_bool (data )
15371539 read_loc (data , expr )
15381540 state .check_min_version (
15391541 "t-strings" , (3 , 14 ), expr .line , expr .column , enforce_in_stubs = True
@@ -1660,16 +1662,30 @@ def read_fstring_items(state: State, data: ReadBuffer) -> Expression:
16601662 return build_fstring_join (data , items )
16611663
16621664
1663- def build_fstring_join (data : ReadBuffer , items : list [Expression ]) -> Expression :
1665+ def build_fstring_join (
1666+ data : ReadBuffer , items : list [Expression ], set_has_surrogates : bool = False
1667+ ) -> Expression :
16641668 items = collapse_consecutive_str_items (items )
16651669 if len (items ) == 1 :
16661670 expr = items [0 ]
1671+ if set_has_surrogates :
1672+ if isinstance (expr , StrExpr ):
1673+ target = expr
1674+ else :
1675+ assert isinstance (expr , CallExpr ) and isinstance (expr .callee , MemberExpr )
1676+ # It doesn't really matter where to set the surrogates flag,
1677+ # so we set it on the outermost format string.
1678+ target = expr .callee .expr
1679+ assert isinstance (target , StrExpr )
1680+ target .has_surrogates = read_bool (data )
16671681 read_loc (data , expr )
16681682 return expr
16691683 args = ListExpr (items )
16701684 str_expr = StrExpr ("" )
16711685 member = MemberExpr (str_expr , "join" )
16721686 call = CallExpr (member , [args ], [ARG_POS ], [None ])
1687+ if set_has_surrogates :
1688+ str_expr .has_surrogates = read_bool (data )
16731689 read_loc (data , call )
16741690 set_line_column (args , call )
16751691 set_line_column (str_expr , call )
0 commit comments