Skip to content

Commit a449c98

Browse files
ulfalizergalak
authored andcommitted
scripts: Fix risky uses of non-raw regex strings in Python scripts
Fixes pylint warnings like this one: doc/conf.py:325:0: W1401: Anomalous backslash in string: '\s'. String constant might be missing an r prefix. (anomalous-backslash-in-string) The reason for this warning is that backslash escapes are interpreted in non-raw (non-r-prefixed) strings. For example, '\a' and r'\a' are not the same string (first one has a single ASCII bell character, second one has two characters). It just happens that there's no \s (or \., or \/) escape for example, and '\s' turns into two characters (as needed for a regex). It's risky to rely on stuff like that regexes though. Best to make them raw strings unless they're super trivial. Also note that '\s' and '\\s' turn into the same string. Another tip: A literal ' can be put into a string with "blah'blah" instead of 'blah\'blah'. Signed-off-by: Ulf Magnusson <[email protected]>
1 parent 3c45eb4 commit a449c98

File tree

10 files changed

+17
-17
lines changed

10 files changed

+17
-17
lines changed

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@
322322
#'pointsize': '10pt',
323323

324324
# Additional stuff for the LaTeX preamble.
325-
'preamble': '\setcounter{tocdepth}{2}',
325+
'preamble': r'\setcounter{tocdepth}{2}',
326326

327327
# Latex figure (float) alignment
328328
#'figure_align': 'htbp',

doc/extensions/zephyr/application.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# This could be as simple as generating a couple of sets of instructions, one
1515
# for Unix environments, and another for Windows.
1616
class ZephyrAppCommandsDirective(Directive):
17-
'''
17+
r'''
1818
This is a Zephyr directive for generating consistent documentation
1919
of the shell commands needed to manage (build, flash, etc.) an application.
2020

doc/extensions/zephyr/link-roles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def setup(app):
3030

3131
def autolink(pattern):
3232
def role(name, rawtext, text, lineno, inliner, options={}, content=[]):
33-
m = re.search('(.*)\s*<(.*)>', text) # noqa: W605 - regular expression
33+
m = re.search(r'(.*)\s*<(.*)>', text)
3434
if m:
3535
link_text = m.group(1)
3636
link = m.group(2)

doc/scripts/extract_content.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def src_deps(zephyr_base, src_file, dest):
8181
# argument, which is a (relative) path to the additional
8282
# dependency file.
8383
directives = "|".join(DIRECTIVES)
84-
pattern = re.compile("\.\.\s+(?P<directive>%s)::\s+(?P<dep_rel>.*)" %
84+
pattern = re.compile(r"\.\.\s+(?P<directive>%s)::\s+(?P<dep_rel>.*)" %
8585
directives)
8686
deps = []
8787
for l in content:

scripts/check_link_map.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
# to write a valid linker script that will fail this script, but we
2323
# don't have such a use case and one isn't forseen.
2424

25-
section_re = re.compile('(?x)' # (allow whitespace)
26-
'^([a-zA-Z0-9_\.]+) \s+' # name
27-
' (0x[0-9a-f]+) \s+' # addr
28-
' (0x[0-9a-f]+)\s*') # size
25+
section_re = re.compile(r'(?x)' # (allow whitespace)
26+
r'^([a-zA-Z0-9_\.]+) \s+' # name
27+
r' (0x[0-9a-f]+) \s+' # addr
28+
r' (0x[0-9a-f]+)\s*') # size
2929

3030
load_addr_re = re.compile('load address (0x[0-9a-f]+)')
3131

scripts/ci/get_modified_boards.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def main():
6060
for f in files:
6161
if f.endswith(".rst") or f.endswith(".png") or f.endswith(".jpg"):
6262
continue
63-
p = re.match("^boards\/[^/]+\/([^/]+)\/", f)
63+
p = re.match(r"^boards\/[^/]+\/([^/]+)\/", f)
6464
if p and p.groups():
6565
boards.add(p.group(1))
6666

scripts/filter-known-issues.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
# first is a list of one or more comment lines
4444
# followed by a list of non-comments which describe a multiline regex
4545
config_regex = \
46-
b"(?P<comment>(^\s*#.*\n)+)" \
46+
b"(?P<comment>(^\\s*#.*\n)+)" \
4747
b"(?P<regex>(^[^#].*\n)+)"
4848

4949

@@ -87,7 +87,7 @@ def config_import_path(path):
8787
"""
8888
Imports regular expresions from any file *.conf in the given path
8989
"""
90-
file_regex = re.compile(".*\.conf$")
90+
file_regex = re.compile(r".*\.conf$")
9191
try:
9292
for dirpath, _, filenames in os.walk(path):
9393
for _filename in sorted(filenames):

scripts/gen_app_partitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def parse_obj_files(partitions):
126126
# Iterate over all object files to find partitions
127127
for dirpath, _, files in os.walk(args.directory):
128128
for filename in files:
129-
if re.match(".*\.obj$",filename):
129+
if re.match(r".*\.obj$",filename):
130130
fullname = os.path.join(dirpath, filename)
131131
find_obj_file_partitions(fullname, partitions)
132132

scripts/gitlint/zephyr_commit_rules.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def validate(self, commit):
6666
flags |= re.IGNORECASE
6767
for line in commit.message.body:
6868
if line.lower().startswith("signed-off-by"):
69-
if not re.search('(^)Signed-off-by: ([-\'\w.]+) ([-\'\w.]+) (.*)', line, flags=flags):
69+
if not re.search(r"(^)Signed-off-by: ([-'\w.]+) ([-'\w.]+) (.*)", line, flags=flags):
7070
return [RuleViolation(self.id, "Signed-off-by: must have a full name", line_nr=1)]
7171
else:
7272
return
@@ -106,7 +106,7 @@ class MaxLineLengthExceptions(LineRule):
106106

107107
def validate(self, line, _commit):
108108
max_length = self.options['line-length'].value
109-
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line)
109+
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', line)
110110
if line.startswith('Signed-off-by'):
111111
return
112112

scripts/kconfig/checkconfig.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ def search_config_in_file(tree, items, completelog, exclude):
105105
with open(os.path.join(dirName, fname), "r", encoding="utf-8", errors="ignore") as f:
106106
searchConf = f.readlines()
107107
for line in searchConf:
108-
if re.search('(^|[\s|(])CONFIG_([a-zA-Z0-9_]+)', line) :
109-
configName = re.search('(^|[\s|(])'
110-
+'CONFIG_([a-zA-Z0-9_]+)', line)
108+
if re.search(r'(^|[\s|(])CONFIG_([a-zA-Z0-9_]+)', line) :
109+
configName = re.search(r'(^|[\s|(])'
110+
+ r'CONFIG_([a-zA-Z0-9_]+)', line)
111111
configs = configs + 1
112112
if completelog:
113113
print('\n' + configName.group(2) + ' at '

0 commit comments

Comments
 (0)