mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[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 commit6ccfa31421) (cherry picked from commit9c18d783c3) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
2f75d43f1e
commit
97d14e1dfb
3 changed files with 9 additions and 2 deletions
|
|
@ -330,7 +330,7 @@ def _class_escape(source, escape):
|
||||||
charname = source.getuntil('}', 'character name')
|
charname = source.getuntil('}', 'character name')
|
||||||
try:
|
try:
|
||||||
c = ord(unicodedata.lookup(charname))
|
c = ord(unicodedata.lookup(charname))
|
||||||
except KeyError:
|
except (KeyError, TypeError):
|
||||||
raise source.error("undefined character name %r" % charname,
|
raise source.error("undefined character name %r" % charname,
|
||||||
len(charname) + len(r'\N{}'))
|
len(charname) + len(r'\N{}'))
|
||||||
return LITERAL, c
|
return LITERAL, c
|
||||||
|
|
@ -390,7 +390,7 @@ def _escape(source, escape, state):
|
||||||
charname = source.getuntil('}', 'character name')
|
charname = source.getuntil('}', 'character name')
|
||||||
try:
|
try:
|
||||||
c = ord(unicodedata.lookup(charname))
|
c = ord(unicodedata.lookup(charname))
|
||||||
except KeyError:
|
except (KeyError, TypeError):
|
||||||
raise source.error("undefined character name %r" % charname,
|
raise source.error("undefined character name %r" % charname,
|
||||||
len(charname) + len(r'\N{}'))
|
len(charname) + len(r'\N{}'))
|
||||||
return LITERAL, c
|
return LITERAL, c
|
||||||
|
|
|
||||||
|
|
@ -753,6 +753,10 @@ def test_named_unicode_escapes(self):
|
||||||
"undefined character name 'SPAM'", 0)
|
"undefined character name 'SPAM'", 0)
|
||||||
self.checkPatternError(r'[\N{SPAM}]',
|
self.checkPatternError(r'[\N{SPAM}]',
|
||||||
"undefined character name 'SPAM'", 1)
|
"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', 0)
|
||||||
self.checkPatternError(br'[\N{LESS-THAN SIGN}]', r'bad escape \N', 1)
|
self.checkPatternError(br'[\N{LESS-THAN SIGN}]', r'bad escape \N', 1)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
Parsing ``\N`` escapes of Unicode Named Character Sequences in a
|
||||||
|
:mod:`regular expression <re>` raises now :exc:`re.error` instead of
|
||||||
|
``TypeError``.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue