{"record":{"id":"f389a931ee3d64df","repo":"pytest-dev/pytest","slug":"escaping-with-not-supported-in-marker-expressi","errorCode":null,"errorMessage":"escaping with \"\\\" not supported in marker expression","messagePattern":"escaping with \"\\\\\" not supported in marker expression","errorType":"exception","errorClass":"SyntaxError","httpStatus":null,"severity":"error","filePath":"src/_pytest/mark/expression.py","lineNumber":106,"sourceCode":"            elif input[pos] == \")\":\n                yield Token(TokenType.RPAREN, \")\", pos)\n                pos += 1\n            elif input[pos] == \"=\":\n                yield Token(TokenType.EQUAL, \"=\", pos)\n                pos += 1\n            elif input[pos] == \",\":\n                yield Token(TokenType.COMMA, \",\", pos)\n                pos += 1\n            elif (quote_char := input[pos]) in (\"'\", '\"'):\n                end_quote_pos = input.find(quote_char, pos + 1)\n                if end_quote_pos == -1:\n                    raise SyntaxError(\n                        f'closing quote \"{quote_char}\" is missing',\n                        (FILE_NAME, 1, pos + 1, input),\n                    )\n                value = input[pos : end_quote_pos + 1]\n                if (backslash_pos := value.find(\"\\\\\")) != -1:\n                    raise SyntaxError(\n                        r'escaping with \"\\\" not supported in marker expression',\n                        (FILE_NAME, 1, pos + backslash_pos + 1, input),\n                    )\n                yield Token(TokenType.STRING, value, pos)\n                pos += len(value)\n            else:\n                match = re.match(r\"(:?\\w|:|\\+|-|\\.|\\[|\\]|\\\\|/)+\", input[pos:])\n                if match:\n                    value = match.group(0)\n                    if value == \"or\":\n                        yield Token(TokenType.OR, value, pos)\n                    elif value == \"and\":\n                        yield Token(TokenType.AND, value, pos)\n                    elif value == \"not\":\n                        yield Token(TokenType.NOT, value, pos)\n                    else:\n                        yield Token(TokenType.IDENT, value, pos)\n                    pos += len(value)","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/mark/expression.py#L88-L124","documentation":"A SyntaxError raised by the marker-expression tokenizer when a quoted string value contains a backslash. pytest's expression grammar deliberately rejects escape sequences; the offending backslash position is reported via the SyntaxError offset tuple. It fires only after a successful close-quote match, while scanning the captured value.","triggerScenarios":"Using `-m \"my_mark(path='C:\\\\temp')\"`, `-m \"my_mark(regex='a\\\\b')\"`, or any marker kwarg value containing a backslash. Even a single backslash before the closing quote triggers it because value.find('\\\\') is non-negative.","commonSituations":"Windows file paths or regex patterns passed as marker kwarg values through -m; copying a Python string literal (with escapes) into a CLI marker expression.","solutions":["Remove all backslashes from the marker value, or replace them with forward slashes / character classes.","Pass complex data via a fixture or environment variable instead of the marker expression string.","Define the marker with the value in code (not via -m) so escaping is handled by Python, not the expression grammar."],"exampleFix":"# before\npytest -m \"my_mark(path='C:\\\\temp')\"\n# after\npytest -m \"my_mark(path='C:/temp')\"","handlingStrategy":"validation","validationCode":"def is_safe_marker_value(value: str) -> bool:\n    return '\\\\' not in value","typeGuard":null,"tryCatchPattern":"try:\n    Expression.compile(expr)\nexcept SyntaxError as e:\n    if 'escaping' in e.msg:\n        value = value.replace('\\\\', '/')\n        ...","preventionTips":["Never use backslashes in marker expression values.","Prefer forward slashes for paths.","Pass rich data via fixtures, not marker strings."],"tags":["pytest","marker-expression","tokenizer","syntax-error","escaping"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}