Skip to content

Commit 9c18d78

Browse files
[3.10] gh-90568: Fix exception type for \N with a named sequence in RE (GH-91665) (GH-91830)
re.error is now raised instead of TypeError. (cherry picked from commit 6ccfa31)
1 parent 9f8b9a3 commit 9c18d78

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

Lib/sre_parse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def _class_escape(source, escape):
330330
charname = source.getuntil('}', 'character name')
331331
try:
332332
c = ord(unicodedata.lookup(charname))
333-
except KeyError:
333+
except (KeyError, TypeError):
334334
raise source.error("undefined character name %r" % charname,
335335
len(charname) + len(r'\N{}'))
336336
return LITERAL, c
@@ -390,7 +390,7 @@ def _escape(source, escape, state):
390390
charname = source.getuntil('}', 'character name')
391391
try:
392392
c = ord(unicodedata.lookup(charname))
393-
except KeyError:
393+
except (KeyError, TypeError):
394394
raise source.error("undefined character name %r" % charname,
395395
len(charname) + len(r'\N{}'))
396396
return LITERAL, c

Lib/test/test_re.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,10 @@ def test_named_unicode_escapes(self):
754754
"undefined character name 'SPAM'", 0)
755755
self.checkPatternError(r'[\N{SPAM}]',
756756
"undefined character name 'SPAM'", 1)
757+
self.checkPatternError(r'\N{KEYCAP NUMBER SIGN}',
758+
"undefined character name 'KEYCAP NUMBER SIGN'", 0)
759+
self.checkPatternError(r'[\N{KEYCAP NUMBER SIGN}]',
760+
"undefined character name 'KEYCAP NUMBER SIGN'", 1)
757761
self.checkPatternError(br'\N{LESS-THAN SIGN}', r'bad escape \N', 0)
758762
self.checkPatternError(br'[\N{LESS-THAN SIGN}]', r'bad escape \N', 1)
759763

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Parsing ``\N`` escapes of Unicode Named Character Sequences in a
2+
:mod:`regular expression <re>` raises now :exc:`re.error` instead of
3+
``TypeError``.

0 commit comments

Comments
 (0)