]> granicus.if.org Git - python/commitdiff
#2650: re.escape() no longer escapes the "_".
authorEzio Melotti <ezio.melotti@gmail.com>
Sun, 10 Apr 2011 09:59:16 +0000 (12:59 +0300)
committerEzio Melotti <ezio.melotti@gmail.com>
Sun, 10 Apr 2011 09:59:16 +0000 (12:59 +0300)
Doc/library/re.rst
Lib/re.py
Lib/test/test_re.py
Misc/NEWS

index 3e9cf026f07df167f3782fab53c9b48803247809..b1c3804b67ba3f6bbe9b528c26dd20a8c2e96185 100644 (file)
@@ -689,9 +689,12 @@ form.
 
 .. function:: escape(string)
 
-   Return *string* with all non-alphanumerics backslashed; this is useful if you
-   want to match an arbitrary literal string that may have regular expression
-   metacharacters in it.
+   Escape all the characters in pattern except ASCII letters, numbers and ``'_'``.
+   This is useful if you want to match an arbitrary literal string that may
+   have regular expression metacharacters in it.
+
+   .. versionchanged:: 3.3
+      The ``'_'`` character is no longer escaped.
 
 
 .. function:: purge()
index abd7ea27b32bca30c88dbe29007fccd77560e023..cdf597627f095e7ce36922e1880fb8013e830b30 100644 (file)
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -215,12 +215,14 @@ def template(pattern, flags=0):
     return _compile(pattern, flags|T)
 
 _alphanum_str = frozenset(
-    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
+    "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
 _alphanum_bytes = frozenset(
-    b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
+    b"_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890")
 
 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):
         alphanum = _alphanum_str
         s = list(pattern)
index fe8bc3403947981ac9618fba2f28b2b979b2ab42..e3d10f735249e5476218503d9e4f685d1a7d380f 100644 (file)
@@ -428,7 +428,7 @@ class ReTests(unittest.TestCase):
         self.assertEqual(m.span(), span)
 
     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))
         for c in p:
             if c in alnum_chars:
@@ -441,7 +441,7 @@ class ReTests(unittest.TestCase):
         self.assertMatch(re.escape(p), p)
 
     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))
         for i in p:
             b = bytes([i])
index fdd410b77d5e289900cf174fe32c879923606897..9d6307469c6b43c6500ba018734f396618ec9434 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #2650: re.escape() no longer escapes the '_'.
+
 - Issue #11757: select.select() now raises ValueError when a negative timeout
   is passed (previously, a select.error with EINVAL would be raised).  Patch
   by Charles-François Natali.