Skip to content

fix(util): use raw strings for regex patterns (W605 invalid escapes) - #102

Open
randomizedcoder wants to merge 1 commit into
PlatformLab:mainfrom
randomizedcoder:fix/python-regex-w605
Open

randomizedcoder wants to merge 1 commit into
PlatformLab:mainfrom
randomizedcoder:fix/python-regex-w605

Conversation

@randomizedcoder

Copy link
Copy Markdown

Bug: invalid escape sequences in regex string literals (W605)

Several regex patterns in the util/ trace-analysis scripts are written as ordinary (non-raw) string literals but contain regex escapes like \[, \], \(, \), \+:

match = re.match(' *([-0-9.]+) us .* \[C([0-9]+)\]', line)   # rpcid.py, smi.py, tput.py, tthoma.py

Python treats \[ etc. as invalid escape sequences. Today they happen to evaluate to the intended backslash-plus-character, but each one emits a DeprecationWarning/SyntaxWarning and is scheduled to become a hard error in a future Python. ruff reports every one as W605.

Fix

Convert the affected literals to raw strings:

-match = re.match(' *([-0-9.]+) us .* \[C([0-9]+)\]', line)
+match = re.match(r' *([-0-9.]+) us .* \[C([0-9]+)\]', line)

Files touched: rpcid.py, smi.py, tput.py, tthoma.py, ttmerge.py, ttsyslog.py.

Every conversion is behavior-preserving. The evaluated string value is byte-for-byte identical before and after — verified by tokenizing each file and comparing ast.literal_eval of every string literal (old tree vs new tree): all values identical. One line, tthoma.py:1841, also contained a genuine \\ (backslash) escape mixed in with the invalid ones, so it becomes r'...' with the doubled backslash reduced to a single one, which keeps the exact same string value.

Verification (linter gate + regression scaffold)

Before:  ruff check --select W605 util/   ->  32 findings
After:   ruff check --select W605 util/   ->  All checks passed!  (0)

A standalone, table-driven pytest scaffold is included as executable documentation (no new CI is wired):

$ python -W error::SyntaxWarning -m pytest util/tests/test_trace_line.py -q
11 passed

util/tests/test_trace_line.py pins the trace-line regex behavior (positive / boundary / negative / corner rows, each with a description and expected) and asserts every fixed source compiles with no SyntaxWarning under warnings-as-errors — the pytest form of the W605 gate.

Several regex string literals in the util/ trace-analysis scripts used
sequences like '\[', '\]', '\(', '\)' and '\+' inside ordinary (non-raw)
string literals. Python treats these as invalid escape sequences: they
currently evaluate to the intended backslash-plus-char by accident, but
emit a DeprecationWarning/SyntaxWarning and are slated to become errors
in a future Python. ruff flags every one as W605.

Convert the affected literals to raw strings (r'...'). Every conversion
is behavior-preserving: the evaluated string value is byte-for-byte
identical before and after (verified by tokenizing each file and
comparing the eval of every string literal). tthoma.py:1841 additionally
had a real '\\' (backslash) escape mixed in, so it becomes r'...' with
the doubled backslash reduced to one to keep the same value.

Files: rpcid.py, smi.py, tput.py, tthoma.py, ttmerge.py, ttsyslog.py.

Gate: `ruff check --select W605 util/` reports 32 findings before and 0
after. A standalone pytest scaffold (util/tests/test_trace_line.py,
table-driven) pins the trace-line regex behavior and asserts each fixed
file compiles with no SyntaxWarning under -W error; no new CI is wired.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant