Skip to content

fix mark expression scanner: search for backslash in string value, not entire input#14475

Merged
bluetech merged 3 commits into
pytest-dev:mainfrom
EternalRights:fix-expression-backslash-check
May 13, 2026
Merged

fix mark expression scanner: search for backslash in string value, not entire input#14475
bluetech merged 3 commits into
pytest-dev:mainfrom
EternalRights:fix-expression-backslash-check

Conversation

@EternalRights
Copy link
Copy Markdown
Contributor

The backslash check in the string literal lexer was searching the entire input expression (input.find("\")) instead of only the current string value (value.find("\")). This caused false rejections when an identifier containing a backslash appeared in the same expression as a string literal.

For example, pytest -k 'test\nfoo\n and mark(x="y")' would fail with "escaping not supported" even though the backslash is in the identifier, not the string.

The fix:

  • Changed input.find("\") to value.find("\") so we only check for backslashes inside the string literal
  • Adjusted the column offset from backslash_pos + 1 to pos + backslash_pos + 1 so error positions point to the right place

Added a regression test for the case where backslashes in identifiers coexist with string literals in the same expression.

Closes #14474

…t entire input

The backslash check in the string literal lexer was searching the entire
input expression instead of only the current string value. This caused
false rejections when an identifier containing a backslash appeared in the
same expression as a string literal.

Changed input.find("\\") to value.find("\\") and adjusted the column
offset from backslash_pos + 1 to pos + backslash_pos + 1 so the error
points to the correct position when a backslash is inside a string.

Closes pytest-dev#14474
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label May 13, 2026
The test_backslash_in_identifier_with_string_literal test was failing
because the matcher function only recognized '\nfoo\n' as a valid
identifier. When the expression contained 'mark(x="y")', the matcher
returned False for 'mark', causing the entire 'and' expression to
evaluate to False instead of True.
Copy link
Copy Markdown
Member

@bluetech bluetech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look good, thanks!

Comment thread changelog/14474.bugfix.rst Outdated
@bluetech bluetech enabled auto-merge (squash) May 13, 2026 15:30
@bluetech bluetech added the backport 9.0.x apply to PRs at any point; backports the changes to the 9.0.x branch label May 13, 2026
@bluetech bluetech merged commit 984cabf into pytest-dev:main May 13, 2026
33 checks passed
@patchback
Copy link
Copy Markdown

patchback Bot commented May 13, 2026

Backport to 9.0.x: 💚 backport PR created

✅ Backport PR branch: patchback/backports/9.0.x/984cabfaccf8aa69fe49097ed3d07bd60d05f240/pr-14475

Backported as #14478

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 9.0.x apply to PRs at any point; backports the changes to the 9.0.x branch bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mark expression scanner: backslash check in string literal searches entire input instead of the string value

2 participants