Skip to content

Commit 97d14e1

Browse files
[3.9] gh-90568: Fix exception type for \N with a named sequence in RE (GH-91665) (GH-91830) (GH-91834)
re.error is now raised instead of TypeError. (cherry picked from commit 6ccfa31) (cherry picked from commit 9c18d78) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 2f75d43 commit 97d14e1

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
@@ -753,6 +753,10 @@ def test_named_unicode_escapes(self):
753753
"undefined character name 'SPAM'", 0)
754754
self.checkPatternError(r'[\N{SPAM}]',
755755
"undefined character name 'SPAM'", 1)
756+
self.checkPatternError(r'\N{KEYCAP NUMBER SIGN}',
757+
"undefined character name 'KEYCAP NUMBER SIGN'", 0)
758+
self.checkPatternError(r'[\N{KEYCAP NUMBER SIGN}]',
759+
"undefined character name 'KEYCAP NUMBER SIGN'", 1)
756760
self.checkPatternError(br'\N{LESS-THAN SIGN}', r'bad escape \N', 0)
757761
self.checkPatternError(br'[\N{LESS-THAN SIGN}]', r'bad escape \N', 1)
758762

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)