Fix invalid escape sequence warning

Python 3 interprets string literals as Unicode strings,
and therefore '\d' is treated as an escaped Unicode character.

If you’re not using a raw string to express the pattern,
remember that Python also uses the backslash as an escape sequence in
string literals; if the escape sequence isn’t recognized by Python’s parser,
the backslash and subsequent character are included in the resulting string.
However, if Python would recognize the resulting sequence, the backslash
should be repeated twice. This is complicated and hard to understand,
so it’s highly recommended that you use raw strings for all but the
simplest expressions. [1]

This patch use raw string for the regular expression.

[1] https://docs.python.org/3.6/library/re.html#regular-expression-syntax

Change-Id: Ibee059af3b24896749b66d87a6691fdf40a5767a
Closes-Bug: #1871621
This commit is contained in:
Dongcan Ye 2020-04-08 13:00:35 +00:00
parent 358d352027
commit be935a2256
2 changed files with 4 additions and 4 deletions

View File

@ -182,12 +182,12 @@ class BaseSwanProcess(object, metaclass=abc.ABCMeta):
'erouted': constants.ACTIVE,
'unrouted': constants.DOWN
}
STATUS_RE = '\d\d\d "([a-f0-9\-]+).* (unrouted|erouted);'
STATUS_RE = r'\d\d\d "([a-f0-9\-]+).* (unrouted|erouted);'
STATUS_NOT_RUNNING_RE = 'Command:.*ipsec.*status.*Exit code: [1|3]$'
STATUS_IPSEC_SA_ESTABLISHED_RE = (
'\d{3} #\d+: "([a-f0-9\-]+).*established.*newest IPSEC')
r'\d{3} #\d+: "([a-f0-9\-]+).*established.*newest IPSEC')
STATUS_IPSEC_SA_ESTABLISHED_RE2 = (
'\d{3} #\d+: "([a-f0-9\-\/x]+).*established.*newest IPSEC')
r'\d{3} #\d+: "([a-f0-9\-\/x]+).*established.*newest IPSEC')
def __init__(self, conf, process_id, vpnservice, namespace):
self.conf = conf

View File

@ -72,7 +72,7 @@ class StrongSwanProcess(ipsec.BaseSwanProcess):
'CONNECTING': constants.DOWN,
'INSTALLED': constants.ACTIVE
}
STATUS_RE = '([a-f0-9\-]+).* (ROUTED|CONNECTING|INSTALLED)'
STATUS_RE = r'([a-f0-9\-]+).* (ROUTED|CONNECTING|INSTALLED)'
STATUS_NOT_RUNNING_RE = 'Command:.*ipsec.*status.*Exit code: [1|3] '
def __init__(self, conf, process_id, vpnservice, namespace):