summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlya Maximets <i.maximets@samsung.com>2019-01-15 17:03:00 +0300
committerBen Pfaff <blp@ovn.org>2019-01-15 09:12:59 -0800
commit79b7778006cb26f972339f0ccd34ddd0ca1edff8 (patch)
tree8c8837cd6dadba6cb5e3c0577ba80a7c59df46a0
parenta2d1a08c8d2efae938683bcbf6b66fba19ace103 (diff)
downloadopenvswitch-79b7778006cb26f972339f0ccd34ddd0ca1edff8.tar.gz
python: Escape backslashes while formatting logs.
Since python version 3.7 (and some 3.6+ versions) regexp engine changed to treat the wrong escape sequences as errors. Previously, if the replace string had something like '\u0000', '\u' was qualified as a bad escape sequence and treated just as a sequence of characters '\' and 'u'. But know this triggers an error: Traceback (most recent call last): File "/usr/lib/python3.7/sre_parse.py", line 1021, in parse_template this = chr(ESCAPES[this][1]) KeyError: '\\u' From the documentation [1]: Unknown escapes consisting of '\' and an ASCII letter in replacement templates for re.sub() were deprecated in Python 3.5, and will now cause an error. [1] https://docs.python.org/3/whatsnew/3.7.html#api-and-feature-removals We need to escape the backslash by another one to keep regexp engine from errors. In case of '\\u000', '\\' is a valid escape sequence and the 'u' is a simple character. To be 100% safe we need to use 're.escape(replace)', but it escapes too many characters making the logs hard to read. This change fixes Python 3 tests on systems with python 3.7. Should be backward compatible. Reported-by: Ben Pfaff <blp@ovn.org> Signed-off-by: Ilya Maximets <i.maximets@samsung.com> Signed-off-by: Ben Pfaff <blp@ovn.org>
-rw-r--r--python/ovs/vlog.py2
1 files changed, 1 insertions, 1 deletions
diff --git a/python/ovs/vlog.py b/python/ovs/vlog.py
index d5741a65f..60cece29e 100644
--- a/python/ovs/vlog.py
+++ b/python/ovs/vlog.py
@@ -136,7 +136,7 @@ class Vlog:
min_width = int(matches.group(2))
if len(replace) < min_width:
replace = replace.center(min_width)
- return re.sub(match, replace, tmp)
+ return re.sub(match, replace.replace('\\', r'\\'), tmp)
def _format_time(self, tmp):
date_regex = re.compile('(%(0?[1-9]?[dD])(\{(.*)\})?)')