From 5f72f7a79f79b3c0caf5b88ab9ed7f2aa2ebd225 Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Sat, 17 Jun 2017 03:28:17 -0400 Subject: [PATCH 1/3] added \N{name} escapes to re patterns --- Doc/library/re.rst | 5 ++++- Lib/sre_parse.py | 28 ++++++++++++++++++++++++++++ Lib/test/test_re.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 0b9d9755f30ec6..6a0bbd328541d4 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -443,7 +443,7 @@ character ``'$'``. Most of the standard escapes supported by Python string literals are also accepted by the regular expression parser:: - \a \b \f \n + \a \b \f \n \N{name} \r \t \u \U \v \x \\ @@ -464,6 +464,9 @@ three digits in length. .. versionchanged:: 3.6 Unknown escapes consisting of ``'\'`` and an ASCII letter now are errors. +.. versionchanged:: 3.7 + The ``'\N{name}'`` escape sequence has been added. As in string literals, + it expands to the named Unicode character (e.g. ``'\N{EM DASH}'``). .. seealso:: diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 545252074f63d1..a6f726aef9b28d 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -13,6 +13,7 @@ # XXX: show string offset and offending character for all errors from sre_constants import * +from ast import literal_eval SPECIAL_CHARS = ".\\[{()*+?^$|" REPEAT_CHARS = "*+?{" @@ -25,6 +26,11 @@ WHITESPACE = frozenset(" \t\n\r\v\f") +UNICODE_NAME = ASCIILETTERS | DIGITS | frozenset(' -') +CLOSING_BRACE = frozenset("}") +OPENING_BRACE = frozenset("{") + + _REPEATCODES = frozenset({MIN_REPEAT, MAX_REPEAT}) _UNITCODES = frozenset({ANY, RANGE, IN, LITERAL, NOT_LITERAL, CATEGORY}) @@ -322,6 +328,17 @@ def _class_escape(source, escape): c = int(escape[2:], 16) chr(c) # raise ValueError for invalid code return LITERAL, c + elif c == "N" and source.istext: + # named unicode escape e.g. \N{EM DASH} + escape += source.getwhile(1, OPENING_BRACE) + escape += source.getwhile(100, UNICODE_NAME) + escape += source.getwhile(1, CLOSING_BRACE) + try: + c = ord(literal_eval('"%s"' % escape)) + except SyntaxError: + charname = escape[2:].strip('{}') + raise source.error("unknown Unicode character name %s" % charname, len(escape)) + return LITERAL, c elif c in OCTDIGITS: # octal escape (up to three digits) escape += source.getwhile(2, OCTDIGITS) @@ -370,6 +387,17 @@ def _escape(source, escape, state): c = int(escape[2:], 16) chr(c) # raise ValueError for invalid code return LITERAL, c + elif c == "N" and source.istext: + # named unicode escape e.g. \N{EM DASH} + escape += source.getwhile(1, OPENING_BRACE) + escape += source.getwhile(100, UNICODE_NAME) + escape += source.getwhile(1, CLOSING_BRACE) + try: + c = ord(literal_eval('"%s"' % escape)) + except SyntaxError: + charname = escape[2:].strip('{}') + raise source.error("unknown Unicode character name %s" % charname, len(escape)) + return LITERAL, c elif c == "0": # octal escape escape += source.getwhile(2, OCTDIGITS) diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 0ea5a20469646b..a05c35a2a4740e 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -700,6 +700,39 @@ def test_other_escapes(self): with self.subTest(c): self.assertRaises(re.error, re.compile, '[\\%c]' % c) + def test_named_unicode_escapes(self): + # test individual Unicode named escapes + suites = [ + [ # basic matches + ['\u2014', r'\u2014', '\N{EM DASH}', + r'\N{EM DASH}'], # pattern + ['\u2014', '\N{EM DASH}', '—', '—and more'], # matches + ['\u2015', '\N{EN DASH}'] # no match + ], + [ # character set matches + ['[\u2014-\u2020]', r'[\u2014-\u2020]', + '[\N{EM DASH}-\N{DAGGER}]', r'[\N{EM DASH}-\N{DAGGER}]', + '[\u2014-\N{DAGGER}]', '[\N{EM DASH}-\u2020]',], # pattern + ['\u2014', '\N{EM DASH}', '—', '—and more', '\u2020', + '\N{DAGGER}', '†', '\u2017', '\N{DOUBLE LOW LINE}'], + ['\u2011', '\N{EN DASH}', '\u2013', 'xyz', '\u2021'] + ], + ] + + for patterns, match_yes, match_no in suites: + for pat in patterns: + for target in match_yes: + self.assertTrue(re.match(pat, target)) + for target in match_no: + self.assertIsNone(re.match(pat, target)) + + # test errors in \N{name} handling - only valid names should pass + badly_formed = [r'\N{BUBBA DASH}', r'\N{EM DASH', + r'\NEM DASH}', r'\NOGGIN'] + for bad in badly_formed: + with self.assertRaises(re.error): + re.compile(bad) + def test_string_boundaries(self): # See http://bugs.python.org/issue10713 self.assertEqual(re.search(r"\b(abc)\b", "abc").group(1), From 7fb2983532cd951bc3ecd041ad806b4c1c10d1aa Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Sat, 17 Jun 2017 12:08:29 -0400 Subject: [PATCH 2/3] mention limitation of \N sequences to Unicode patterns --- Doc/library/re.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 6a0bbd328541d4..94a6acffc92d73 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -450,7 +450,7 @@ accepted by the regular expression parser:: (Note that ``\b`` is used to represent word boundaries, and means "backspace" only inside character classes.) -``'\u'`` and ``'\U'`` escape sequences are only recognized in Unicode +``'\u'``, ``'\U'``, and ``'\N{name}'`` escape sequences are only recognized in Unicode patterns. In bytes patterns they are not treated specially. Octal escapes are included in a limited form. If the first digit is a 0, or if From 4db797b1cfe06c265e56e40d63501bc76e616c0f Mon Sep 17 00:00:00 2001 From: Jonathan Eunice Date: Sat, 17 Jun 2017 12:10:29 -0400 Subject: [PATCH 3/3] mention \N escapes more tersely --- Doc/library/re.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 94a6acffc92d73..9fafc0f462d599 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -443,14 +443,14 @@ character ``'$'``. Most of the standard escapes supported by Python string literals are also accepted by the regular expression parser:: - \a \b \f \n \N{name} + \a \b \f \n \N \r \t \u \U \v \x \\ (Note that ``\b`` is used to represent word boundaries, and means "backspace" only inside character classes.) -``'\u'``, ``'\U'``, and ``'\N{name}'`` escape sequences are only recognized in Unicode +``'\u'``, ``'\U'``, and ``'\N'`` escape sequences are only recognized in Unicode patterns. In bytes patterns they are not treated specially. Octal escapes are included in a limited form. If the first digit is a 0, or if