Skip to content

Commit 8ccf6ca

Browse files
clean up
1 parent 5875421 commit 8ccf6ca

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

pandas/core/computation/parsing.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,24 @@ def tokenize_backtick_quoted_string(
171171
return BACKTICK_QUOTED_STRING, source[string_start:string_end]
172172

173173

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+
"""
175192
substrings = []
176193
substring = ""
177194
i = 0
@@ -249,27 +266,20 @@ def tokenize_string(source: str) -> Iterator[tuple[int, str]]:
249266
An iterator yielding all tokens with only toknum and tokval (Tuple[ing, str]).
250267
"""
251268
# GH 59285
269+
# Escape characters, including backticks
252270
source = "".join(
253271
(
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
256274
else substring
257275
)
258-
for is_backticked, substring in split_by_backtick(source)
276+
for is_backtick_quoted, substring in _split_by_backtick(source)
259277
)
260278

261279
line_reader = StringIO(source).readline
262280
token_generator = tokenize.generate_tokens(line_reader)
263281

264282
# Loop over all tokens till a backtick (`) is found.
265283
# 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

Comments
 (0)