summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-04-22 15:35:28 (GMT)
committerGitHub <noreply@github.com>2022-04-22 15:35:28 (GMT)
commit6ccfa31421393910b52936e0447625db06f2a655 (patch)
tree825f9f3bcd6c14f5bab82ee08d55988f8748a168 /Lib
parent2f233fceae9a0c5e66e439bc0169b36547ba47c3 (diff)
downloadcpython-6ccfa31421393910b52936e0447625db06f2a655.zip
cpython-6ccfa31421393910b52936e0447625db06f2a655.tar.gz
cpython-6ccfa31421393910b52936e0447625db06f2a655.tar.bz2
gh-90568: Fix exception type for \N with a named sequence in RE (GH-91665)
re.error is now raised instead of TypeError.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/re/_parser.py4
-rw-r--r--Lib/test/test_re.py4
2 files changed, 6 insertions, 2 deletions
diff --git a/Lib/re/_parser.py b/Lib/re/_parser.py
index f191f80..6588862 100644
--- a/Lib/re/_parser.py
+++ b/Lib/re/_parser.py
@@ -333,7 +333,7 @@ def _class_escape(source, escape):
charname = source.getuntil('}', 'character name')
try:
c = ord(unicodedata.lookup(charname))
- except KeyError:
+ except (KeyError, TypeError):
raise source.error("undefined character name %r" % charname,
len(charname) + len(r'\N{}')) from None
return LITERAL, c
@@ -393,7 +393,7 @@ def _escape(source, escape, state):
charname = source.getuntil('}', 'character name')
try:
c = ord(unicodedata.lookup(charname))
- except KeyError:
+ except (KeyError, TypeError):
raise source.error("undefined character name %r" % charname,
len(charname) + len(r'\N{}')) from None
return LITERAL, c
diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py
index 781bfd6..2d3fef8 100644
--- a/Lib/test/test_re.py
+++ b/Lib/test/test_re.py
@@ -772,6 +772,10 @@ class ReTests(unittest.TestCase):
"undefined character name 'SPAM'", 0)
self.checkPatternError(r'[\N{SPAM}]',
"undefined character name 'SPAM'", 1)
+ self.checkPatternError(r'\N{KEYCAP NUMBER SIGN}',
+ "undefined character name 'KEYCAP NUMBER SIGN'", 0)
+ self.checkPatternError(r'[\N{KEYCAP NUMBER SIGN}]',
+ "undefined character name 'KEYCAP NUMBER SIGN'", 1)
self.checkPatternError(br'\N{LESS-THAN SIGN}', r'bad escape \N', 0)
self.checkPatternError(br'[\N{LESS-THAN SIGN}]', r'bad escape \N', 1)