[libvirt] [PATCH 04/10] python: mark regex strings with 'r' prefix

Daniel P. Berrangé berrange at redhat.com
Wed Oct 9 12:58:38 UTC 2019


When writing regexes special regex matches like "\d" can get
misinterpreted as normal string escape sequences:

docs/apibuild.py:1359:51: W605 invalid escape sequence '\d'
                        value = value + re.sub("^(\d+)U$", "\\1", token[1])
                                                  ^
docs/apibuild.py:2134:31: W605 invalid escape sequence '\('
                m = re.match("\(?1<<(\d+)\)?", info[0])
                              ^
docs/apibuild.py:2134:38: W605 invalid escape sequence '\d'
                m = re.match("\(?1<<(\d+)\)?", info[0])
                                     ^
docs/apibuild.py:2134:42: W605 invalid escape sequence '\)'
                m = re.match("\(?1<<(\d+)\)?", info[0])
                                         ^

To avoid this probem all regexes should use the r"...." syntax for their
strings, which disables normal string escape sequences.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 build-aux/syntax-check.mk | 2 +-
 docs/apibuild.py          | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk
index 97255908b8..acd3f96c6d 100644
--- a/build-aux/syntax-check.mk
+++ b/build-aux/syntax-check.mk
@@ -884,7 +884,7 @@ FLAKE8_WHITESPACE = E211,E221,E222,E225,E226,E231,E261
 FLAKE8_BLANK_LINES = E301,E302,E303,E305
 FLAKE8_LINE_LENGTH = E501
 FLAKE8_STATEMENTS = E722,E741
-FLAKE8_WARNINGS = W504,W605
+FLAKE8_WARNINGS = W504
 
 FLAKE8_IGNORE = $(FLAKE8_INDENTATION),$\
 		$(FLAKE8_WHITESPACE),$\
diff --git a/docs/apibuild.py b/docs/apibuild.py
index dc52caf76d..18dc3604d0 100755
--- a/docs/apibuild.py
+++ b/docs/apibuild.py
@@ -665,7 +665,7 @@ class CParser:
         for line in lines:
             line = line.lstrip().lstrip('*').lstrip()
 
-            m = re.match('([_.a-zA-Z0-9]+):(.*)', line)
+            m = re.match(r'([_.a-zA-Z0-9]+):(.*)', line)
             if m:
                 item = m.group(1)
                 line = m.group(2).lstrip()
@@ -1333,7 +1333,7 @@ class CParser:
                     while token[0] != "sep" or (token[1] != ',' and
                           token[1] != '}'):
                         # We might be dealing with '1U << 12' here
-                        value = value + re.sub("^(\d+)U$", "\\1", token[1])
+                        value = value + re.sub(r"^(\d+)U$", "\\1", token[1])
                         token = self.token()
                 else:
                     try:
@@ -2108,7 +2108,7 @@ class docBuilder:
                 if valhex != "":
                     output.write(" value_hex='%s'" % (valhex))
 
-                m = re.match("\(?1<<(\d+)\)?", info[0])
+                m = re.match(r"\(?1<<(\d+)\)?", info[0])
                 if m:
                     output.write(" value_bitshift='%s'" % (m.group(1)))
 
-- 
2.21.0




More information about the libvir-list mailing list