Skip to contentSkip to conversation

patman: Resolve python string vs. regex escaping syntax

PATCH In review Brian Norris 2 participants 2 messages Jul 26, 2024 – Jul 28, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 patman: Resolve python string vs. regex escaping syntax

Python strings have their own notion of backslash-escaping, and that can conflict with the intentions for strings passed to the 're' module. In particular, I get warnings like this:

tools/patman/../patman/commit.py:9: SyntaxWarning: invalid escape sequence '\s'
re_subject_tag = re.compile('([^:\s]*):\s*(.*)')

We should use a raw string (r'...') so that all escaping is passed into the regex module, not interpreted within the string itself.

---

 tools/patman/commit.py      | 2 +-
 tools/patman/patchstream.py | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
tools/patman/commit.py +1 -1
index 684225c0e60d..ce37a3d95eb9 100644
@@ -6,7 +6,7 @@ import collections
 import re
 
 # Separates a tag: at the beginning of the subject from the rest of it
-re_subject_tag = re.compile('([^:\s]*):\s*(.*)')
+re_subject_tag = re.compile(r'([^:\s]*):\s*(.*)')
 
 class Commit:
     """Holds information about a single commit/patch in the series.
tools/patman/patchstream.py +1 -1
index a09ae9c73714..4955f6aaab99 100644
@@ -48,7 +48,7 @@ RE_TAG = re.compile('^(Tested-by|Acked-by|Reviewed-by|Patch-cc|Fixes): (.*)')
 RE_COMMIT = re.compile('^commit ([0-9a-f]*)$')
 
 # We detect these since checkpatch doesn't always do it
-RE_SPACE_BEFORE_TAB = re.compile('^[+].* \t')
+RE_SPACE_BEFORE_TAB = re.compile(r'^[+].* \t')
 
 # Match indented lines for changes
 RE_LEADING_WHITESPACE = re.compile(r'^\s')
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Brian Norris

Python strings have their own notion of backslash-escaping, and that can conflict with the intentions for strings passed to the 're' module. In particular, I get warnings like this:

tools/patman/../patman/commit.py:9: SyntaxWarning: invalid escape sequence '\s'
re_subject_tag = re.compile('([^:\s]*):\s*(.*)')

We should use a raw string (r'...') so that all escaping is passed into the regex module, not interpreted within the string itself.

Quoted 6 lines

---

 tools/patman/commit.py      | 2 +-
 tools/patman/patchstream.py | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)