Skip to content

Commit 53c53ea

Browse files
Issue python#27030: Unknown escapes in re.sub() replacement template are allowed
again. But they still are deprecated and will be disabled in 3.7.
1 parent b0f75c5 commit 53c53ea

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

Doc/library/re.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,12 @@ form.
758758
Unmatched groups are replaced with an empty string.
759759

760760
.. versionchanged:: 3.6
761-
Unknown escapes consisting of ``'\'`` and an ASCII letter now are errors.
761+
Unknown escapes in *pattern* consisting of ``'\'`` and an ASCII letter
762+
now are errors.
763+
764+
.. deprecated-removed:: 3.5 3.7
765+
Unknown escapes in *repl* consist of ``'\'`` and ASCII letter now raise
766+
a deprecation warning and will be forbidden in Python 3.7.
762767

763768

764769
.. function:: subn(pattern, repl, string, count=0, flags=0)

Doc/whatsnew/3.6.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,8 +2021,9 @@ API and Feature Removals
20212021
------------------------
20222022

20232023
* Unknown escapes consisting of ``'\'`` and an ASCII letter in
2024-
regular expressions will now cause an error. The :const:`re.LOCALE`
2025-
flag can now only be used with binary patterns.
2024+
regular expressions will now cause an error. In replacement templates for
2025+
:func:`re.sub` they are still allowed, but deprecated.
2026+
The :const:`re.LOCALE` flag can now only be used with binary patterns.
20262027

20272028
* ``inspect.getmoduleinfo()`` was removed (was deprecated since CPython 3.3).
20282029
:func:`inspect.getmodulename` should be used for obtaining the module

Lib/sre_parse.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,9 @@ def addgroup(index, pos):
947947
this = chr(ESCAPES[this][1])
948948
except KeyError:
949949
if c in ASCIILETTERS:
950-
raise s.error('bad escape %s' % this, len(this))
950+
import warnings
951+
warnings.warn('bad escape %s' % this,
952+
DeprecationWarning, stacklevel=4)
951953
lappend(this)
952954
else:
953955
lappend(this)

Lib/test/test_re.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def test_basic_re_sub(self):
126126
(chr(9)+chr(10)+chr(11)+chr(13)+chr(12)+chr(7)+chr(8)))
127127
for c in 'cdehijklmopqsuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
128128
with self.subTest(c):
129-
with self.assertRaises(re.error):
129+
with self.assertWarns(DeprecationWarning):
130130
self.assertEqual(re.sub('a', '\\' + c, 'a'), '\\' + c)
131131

132132
self.assertEqual(re.sub(r'^\s*', 'X', 'test'), 'Xtest')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Core and Builtins
2626
Library
2727
-------
2828

29+
- Issue #27030: Unknown escapes in re.sub() replacement template are allowed
30+
again. But they still are deprecated and will be disabled in 3.7.
31+
2932
- Issue #28835: Fix a regression introduced in warnings.catch_warnings():
3033
call warnings.showwarning() if it was overriden inside the context manager.
3134

0 commit comments

Comments
 (0)