Skip to content

Commit 971af63

Browse files
gh-161814: Fix Unicode property matching in case-insensitive sets
1 parent aced381 commit 971af63

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

‎Lib/test/test_re.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,25 @@ def test_named_unicode_escapes(self):
902902
self.checkPatternError(br'\N{LESS-THAN SIGN}', r'bad escape \N', 0)
903903
self.checkPatternError(br'[\N{LESS-THAN SIGN}]', r'bad escape \N', 1)
904904

905+
def test_property_escapes_in_ignorecase_sets(self):
906+
cases = (
907+
(r'[\p{Lu}x]', 'A', True),
908+
(r'[\p{Lu}x]', 'a', False),
909+
(r'[\p{Lu}x]', 'X', True),
910+
(r'[\p{Lu}x]', 'x', True),
911+
(r'[^\p{Lu}x]', 'A', False),
912+
(r'[^\p{Lu}x]', 'a', True),
913+
(r'[\P{Lu}a-c]', 'Z', False),
914+
(r'[\P{Lu}a-c]', 'B', True),
915+
(r'[\p{Lowercase}x]', 'A', False),
916+
(r'[\p{Lt}x]', '\u01c5', True),
917+
)
918+
for flags in (re.IGNORECASE, re.IGNORECASE | re.ASCII):
919+
for pattern, text, matches in cases:
920+
with self.subTest(pattern=pattern, text=text, flags=flags):
921+
self.assertEqual(bool(re.fullmatch(pattern, text, flags)),
922+
matches)
923+
905924
def test_property_escapes(self):
906925
import unicodedata
907926
# Properties that reuse the engine categories behave exactly like
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix Unicode property matching in :mod:`re` case-insensitive character sets
2+
that also contain literals or ranges.

‎Modules/_sre/sre_lib.h‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ SRE(at)(SRE_STATE* state, const SRE_CHAR* ptr, SRE_CODE at)
8989
}
9090

9191
LOCAL(int)
92-
SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
92+
SRE(charset_match)(const SRE_CODE* set, SRE_CODE ch, SRE_CODE category_ch)
9393
{
9494
/* check if character is a member of the given set */
9595

@@ -109,8 +109,8 @@ SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
109109
break;
110110

111111
case SRE_OP_CATEGORY:
112-
/* <CATEGORY> <code> */
113-
if (sre_category(set[0], (int) ch))
112+
/* <CATEGORY> <code>, matched against the original character */
113+
if (sre_category(set[0], (int) category_ch))
114114
return ok;
115115
set++;
116116
break;
@@ -175,6 +175,12 @@ SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
175175
}
176176
}
177177

178+
LOCAL(int)
179+
SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch)
180+
{
181+
return SRE(charset_match)(set, ch, ch);
182+
}
183+
178184
/* Like SRE(charset), but matches both locale cases of ch against every set
179185
member. Testing the whole set once per case would complement it before
180186
closing it under case instead of after, so that [^bc] matched b'B'.
@@ -878,8 +884,8 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
878884
TARGET(SRE_OP_IN_IGNORE):
879885
TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr));
880886
if (ptr >= end
881-
|| !SRE(charset)(state, pattern+1,
882-
(SRE_CODE)sre_lower_ascii(*ptr)))
887+
|| !SRE(charset_match)(pattern + 1,
888+
sre_lower_ascii(*ptr), *ptr))
883889
RETURN_FAILURE;
884890
pattern += pattern[0];
885891
ptr++;
@@ -888,8 +894,8 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel)
888894
TARGET(SRE_OP_IN_UNI_IGNORE):
889895
TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr));
890896
if (ptr >= end
891-
|| !SRE(charset)(state, pattern+1,
892-
(SRE_CODE)sre_lower_unicode(*ptr)))
897+
|| !SRE(charset_match)(pattern + 1,
898+
sre_lower_unicode(*ptr), *ptr))
893899
RETURN_FAILURE;
894900
pattern += pattern[0];
895901
ptr++;

0 commit comments

Comments
 (0)