mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
#2650: re.escape() no longer escapes the "_".
This commit is contained in:
parent
344d26c7a0
commit
88fdeb45ef
4 changed files with 15 additions and 8 deletions
|
@ -689,9 +689,12 @@ form.
|
||||||
|
|
||||||
.. function:: escape(string)
|
.. function:: escape(string)
|
||||||
|
|
||||||
Return *string* with all non-alphanumerics backslashed; this is useful if you
|
Escape all the characters in pattern except ASCII letters, numbers and ``'_'``.
|
||||||
want to match an arbitrary literal string that may have regular expression
|
This is useful if you want to match an arbitrary literal string that may
|
||||||
metacharacters in it.
|
have regular expression metacharacters in it.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.3
|
||||||
|
The ``'_'`` character is no longer escaped.
|
||||||
|
|
||||||
|
|
||||||
.. function:: purge()
|
.. function:: purge()
|
||||||
|
|
|
@ -215,12 +215,14 @@ def template(pattern, flags=0):
|
||||||
return _compile(pattern, flags|T)
|
return _compile(pattern, flags|T)
|
||||||
|
|
||||||
_alphanum_str = frozenset(
|
_alphanum_str = frozenset(
|
||||||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
|
"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
|
||||||
_alphanum_bytes = frozenset(
|
_alphanum_bytes = frozenset(
|
||||||
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
|
b"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
|
||||||
|
|
||||||
def escape(pattern):
|
def escape(pattern):
|
||||||
"Escape all non-alphanumeric characters in pattern."
|
"""
|
||||||
|
Escape all the characters in pattern except ASCII letters, numbers and '_'.
|
||||||
|
"""
|
||||||
if isinstance(pattern, str):
|
if isinstance(pattern, str):
|
||||||
alphanum = _alphanum_str
|
alphanum = _alphanum_str
|
||||||
s = list(pattern)
|
s = list(pattern)
|
||||||
|
|
|
@ -428,7 +428,7 @@ class ReTests(unittest.TestCase):
|
||||||
self.assertEqual(m.span(), span)
|
self.assertEqual(m.span(), span)
|
||||||
|
|
||||||
def test_re_escape(self):
|
def test_re_escape(self):
|
||||||
alnum_chars = string.ascii_letters + string.digits
|
alnum_chars = string.ascii_letters + string.digits + '_'
|
||||||
p = ''.join(chr(i) for i in range(256))
|
p = ''.join(chr(i) for i in range(256))
|
||||||
for c in p:
|
for c in p:
|
||||||
if c in alnum_chars:
|
if c in alnum_chars:
|
||||||
|
@ -441,7 +441,7 @@ class ReTests(unittest.TestCase):
|
||||||
self.assertMatch(re.escape(p), p)
|
self.assertMatch(re.escape(p), p)
|
||||||
|
|
||||||
def test_re_escape_byte(self):
|
def test_re_escape_byte(self):
|
||||||
alnum_chars = (string.ascii_letters + string.digits).encode('ascii')
|
alnum_chars = (string.ascii_letters + string.digits + '_').encode('ascii')
|
||||||
p = bytes(range(256))
|
p = bytes(range(256))
|
||||||
for i in p:
|
for i in p:
|
||||||
b = bytes([i])
|
b = bytes([i])
|
||||||
|
|
|
@ -98,6 +98,8 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #2650: re.escape() no longer escapes the '_'.
|
||||||
|
|
||||||
- Issue #11757: select.select() now raises ValueError when a negative timeout
|
- Issue #11757: select.select() now raises ValueError when a negative timeout
|
||||||
is passed (previously, a select.error with EINVAL would be raised). Patch
|
is passed (previously, a select.error with EINVAL would be raised). Patch
|
||||||
by Charles-François Natali.
|
by Charles-François Natali.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue