]> granicus.if.org Git - python/commitdiff
Issue #27030: Unknown escapes in re.sub() replacement template are allowed
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 6 Dec 2016 17:15:29 +0000 (19:15 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 6 Dec 2016 17:15:29 +0000 (19:15 +0200)
again.  But they still are deprecated and will be disabled in 3.7.

Doc/library/re.rst
Doc/whatsnew/3.6.rst
Lib/sre_parse.py
Lib/test/test_re.py
Misc/NEWS

index 218bbf88cf56a437b86a0cd253bd80e60fb48f68..a298d97c9c391a2a717e0c9e4d9685822fedb4ca 100644 (file)
@@ -758,7 +758,12 @@ form.
       Unmatched groups are replaced with an empty string.
 
    .. versionchanged:: 3.6
-      Unknown escapes consisting of ``'\'`` and an ASCII letter now are errors.
+      Unknown escapes in *pattern* consisting of ``'\'`` and an ASCII letter
+      now are errors.
+
+   .. deprecated-removed:: 3.5 3.7
+      Unknown escapes in *repl* consist of ``'\'`` and ASCII letter now raise
+      a deprecation warning and will be forbidden in Python 3.7.
 
 
 .. function:: subn(pattern, repl, string, count=0, flags=0)
index 040a533da7ef4945b237dc043ce5cd7cfc6978ba..3c2ab129762ea46cb77d92819678663e285748fe 100644 (file)
@@ -2021,8 +2021,9 @@ API and Feature Removals
 ------------------------
 
 * Unknown escapes consisting of ``'\'`` and an ASCII letter in
-  regular expressions will now cause an error.  The :const:`re.LOCALE`
-  flag can now only be used with binary patterns.
+  regular expressions will now cause an error.  In replacement templates for
+  :func:`re.sub` they are still allowed, but deprecated.
+  The :const:`re.LOCALE` flag can now only be used with binary patterns.
 
 * ``inspect.getmoduleinfo()`` was removed (was deprecated since CPython 3.3).
   :func:`inspect.getmodulename` should be used for obtaining the module
index ab37fd3fe2f46f2257e80085914c2178f6bcab9b..6aa49c3bf6f8ed10ae27b05c8efa963d9b54552f 100644 (file)
@@ -947,7 +947,9 @@ def parse_template(source, pattern):
                     this = chr(ESCAPES[this][1])
                 except KeyError:
                     if c in ASCIILETTERS:
-                        raise s.error('bad escape %s' % this, len(this))
+                        import warnings
+                        warnings.warn('bad escape %s' % this,
+                                      DeprecationWarning, stacklevel=4)
                 lappend(this)
         else:
             lappend(this)
index 84131d2b9260dfb829c5adf3c99be28af2aae368..4bdaa4b6c627097ec07e56f7acb5157c5c57a7e0 100644 (file)
@@ -126,7 +126,7 @@ class ReTests(unittest.TestCase):
                          (chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)+chr(8)))
         for c in 'cdehijklmopqsuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
             with self.subTest(c):
-                with self.assertRaises(re.error):
+                with self.assertWarns(DeprecationWarning):
                     self.assertEqual(re.sub('a', '\\' + c, 'a'), '\\' + c)
 
         self.assertEqual(re.sub(r'^\s*', 'X', 'test'), 'Xtest')
index fed13a3d797ce3ec70f94e07d1e358d4487332ab..1f47248822791d2d75e44e28ec7100503633713f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #27030: Unknown escapes in re.sub() replacement template are allowed
+  again.  But they still are deprecated and will be disabled in 3.7.
+
 - Issue #28835: Fix a regression introduced in warnings.catch_warnings():
   call warnings.showwarning() if it was overriden inside the context manager.