#1106682 Warning: invalid escape sequence in re patterns

#1106682#5
Date:
2025-05-27 18:44:31 UTC
From:
To:
Dear Maintainer,

Running anything that imports whois gives six lines of warnings:

/usr/lib/python3/dist-packages/whois/_3_adjust.py:77: SyntaxWarning: invalid escape sequence '\+'
  s = re.sub('(\+[0-9]{2}):([0-9]{2})', '\\1\\2', s)
/usr/lib/python3/dist-packages/whois/_3_adjust.py:78: SyntaxWarning: invalid escape sequence '\ '
  s = re.sub('(\ #.*)', '', s)
/usr/lib/python3/dist-packages/whois/_3_adjust.py:92: SyntaxWarning: invalid escape sequence '\+'
  tmp = re.findall('\+([0-9]{2})00', s)

This is because since Python 3.12, Python complains about strings that have things like backslashes in them that aren't string escapes.

The fix is very easy, just put r in front of each string, e.g.
  s = re.sub(r'(\+[0-9]{2}):([0-9]{2})', '\\1\\2', s)

Here's a patch that does that for the three lines that are printing warnings:

========= Begin patch
--- /usr/lib/python3/dist-packages/whois/_3_adjust.py	2019-05-13 16:08:01.000000000 -0600
+++ /tmp/_3_adjust.py	2025-05-27 12:30:38.067301104 -0600
@@ -74,8 +74,8 @@
         return

     s = s.replace('(jst)', '(+0900)')
-    s = re.sub('(\+[0-9]{2}):([0-9]{2})', '\\1\\2', s)
-    s = re.sub('(\ #.*)', '', s)
+    s = re.sub(r'(\+[0-9]{2}):([0-9]{2})', '\\1\\2', s)
+    s = re.sub(r'(\ #.*)', '', s)

     if PYTHON_VERSION < 3: return str_to_date_py2(s)

@@ -89,7 +89,7 @@


 def str_to_date_py2(s):
-    tmp = re.findall('\+([0-9]{2})00', s)
+    tmp = re.findall(r'\+([0-9]{2})00', s)
     if tmp:
         tz = int(tmp[0])
     else:
========= End patch