diff -r 062533327ad2 Lib/re.py --- a/Lib/re.py Mon Aug 19 10:03:25 2013 +0300 +++ b/Lib/re.py Mon Aug 19 22:54:23 2013 +0800 @@ -199,10 +199,11 @@ return _compile(pattern, flags|T) _alphanum = frozenset( - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") + "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") def escape(pattern): - "Escape all non-alphanumeric characters in pattern." + """Escape all non-alphanumeric characters and non-underscore characters + in pattern.""" s = list(pattern) alphanum = _alphanum for i, c in enumerate(pattern): diff -r 062533327ad2 Lib/test/test_re.py --- a/Lib/test/test_re.py Mon Aug 19 10:03:25 2013 +0300 +++ b/Lib/test/test_re.py Mon Aug 19 22:54:23 2013 +0800 @@ -501,7 +501,7 @@ 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 = u''.join(unichr(i) for i in range(256)) for c in p: if c in alnum_chars: @@ -514,7 +514,8 @@ 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 = ''.join(chr(i) for i in range(256)) for b in p: if b in alnum_chars: