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 <[email protected]>
Signed-off-by: Ilya Maximets <[email protected]>
---
 python/ovs/vlog.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/python/ovs/vlog.py b/python/ovs/vlog.py
index 5b478fc54..30a88891e 100644
--- a/python/ovs/vlog.py
+++ b/python/ovs/vlog.py
@@ -139,7 +139,7 @@ class Vlog(object):
             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(r'(%(0?[1-9]?[dD])(\{(.*)\})?)')
-- 
2.17.1

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to