@@ -171,7 +171,24 @@ def tokenize_backtick_quoted_string(
171
171
return BACKTICK_QUOTED_STRING , source [string_start :string_end ]
172
172
173
173
174
- def split_by_backtick (s : str ) -> list [tuple [bool , str ]]:
174
+ def _split_by_backtick (s : str ) -> list [tuple [bool , str ]]:
175
+ """
176
+ Splits a str into substrings along backtick characters (`).
177
+
178
+ Disregards backticks inside quotes.
179
+
180
+ Parameters
181
+ ----------
182
+ s : str
183
+ The Python source code string.
184
+
185
+ Returns
186
+ -------
187
+ substrings: list[tuple[bool, str]]
188
+ List of tuples, where each tuple has two elements:
189
+ The first is a boolean indicating if the substring is backtick-quoted.
190
+ The second is the actual substring.
191
+ """
175
192
substrings = []
176
193
substring = ""
177
194
i = 0
@@ -249,27 +266,20 @@ def tokenize_string(source: str) -> Iterator[tuple[int, str]]:
249
266
An iterator yielding all tokens with only toknum and tokval (Tuple[ing, str]).
250
267
"""
251
268
# GH 59285
269
+ # Escape characters, including backticks
252
270
source = "" .join (
253
271
(
254
- f"` { create_valid_python_identifier (substring [1 :- 1 ])} `"
255
- if is_backticked
272
+ create_valid_python_identifier (substring [1 :- 1 ])
273
+ if is_backtick_quoted
256
274
else substring
257
275
)
258
- for is_backticked , substring in split_by_backtick (source )
276
+ for is_backtick_quoted , substring in _split_by_backtick (source )
259
277
)
260
278
261
279
line_reader = StringIO (source ).readline
262
280
token_generator = tokenize .generate_tokens (line_reader )
263
281
264
282
# Loop over all tokens till a backtick (`) is found.
265
283
# Then, take all tokens till the next backtick to form a backtick quoted string
266
- for toknum , tokval , start , _ , _ in token_generator :
267
- if tokval == "`" :
268
- try :
269
- yield tokenize_backtick_quoted_string (
270
- token_generator , source , string_start = start [1 ] + 1
271
- )
272
- except Exception as err :
273
- raise SyntaxError (f"Failed to parse backticks in '{ source } '." ) from err
274
- else :
275
- yield toknum , tokval
284
+ for toknum , tokval , _ , _ , _ in token_generator :
285
+ yield toknum , tokval
0 commit comments