[Python-checkins] cpython (3.6): Issue #27030: Unknown escapes in re.sub() replacement template are allowed

serhiy.storchaka python-checkins at python.org
Tue Dec 6 12:25:44 EST 2016


https://hg.python.org/cpython/rev/1b162d6e3d01
changeset: 105482:1b162d6e3d01
branch: 3.6
parent: 105479:a4d4dd411431
user: Serhiy Storchaka <storchaka at gmail.com>
date: Tue Dec 06 19:15:29 2016 +0200
summary:
 Issue #27030: Unknown escapes in re.sub() replacement template are allowed
again. But they still are deprecated and will be disabled in 3.7.
files:
 Doc/library/re.rst | 7 ++++++-
 Doc/whatsnew/3.6.rst | 5 +++--
 Lib/sre_parse.py | 4 +++-
 Lib/test/test_re.py | 2 +-
 Misc/NEWS | 3 +++
 5 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/Doc/library/re.rst b/Doc/library/re.rst
--- a/Doc/library/re.rst
+++ b/Doc/library/re.rst
@@ -758,7 +758,12 @@
 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)
diff --git a/Doc/whatsnew/3.6.rst b/Doc/whatsnew/3.6.rst
--- a/Doc/whatsnew/3.6.rst
+++ b/Doc/whatsnew/3.6.rst
@@ -2021,8 +2021,9 @@
 ------------------------
 
 * 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
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py
--- a/Lib/sre_parse.py
+++ b/Lib/sre_parse.py
@@ -947,7 +947,9 @@
 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)
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -126,7 +126,7 @@
 (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')
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,9 @@
 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.
 
-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /