diff --git a/Lib/test/test_re.py b/Lib/test/test_re.py index 41ee35958b77314..d0736aae1256eba 100644 --- a/Lib/test/test_re.py +++ b/Lib/test/test_re.py @@ -902,6 +902,25 @@ def test_named_unicode_escapes(self): self.checkPatternError(br'\N{LESS-THAN SIGN}', r'bad escape \N', 0) self.checkPatternError(br'[\N{LESS-THAN SIGN}]', r'bad escape \N', 1) + def test_property_escapes_in_ignorecase_sets(self): + cases = ( + (r'[\p{Lu}x]', 'A', True), + (r'[\p{Lu}x]', 'a', False), + (r'[\p{Lu}x]', 'X', True), + (r'[\p{Lu}x]', 'x', True), + (r'[^\p{Lu}x]', 'A', False), + (r'[^\p{Lu}x]', 'a', True), + (r'[\P{Lu}a-c]', 'Z', False), + (r'[\P{Lu}a-c]', 'B', True), + (r'[\p{Lowercase}x]', 'A', False), + (r'[\p{Lt}x]', '\u01c5', True), + ) + for flags in (re.IGNORECASE, re.IGNORECASE | re.ASCII): + for pattern, text, matches in cases: + with self.subTest(pattern=pattern, text=text, flags=flags): + self.assertEqual(bool(re.fullmatch(pattern, text, flags)), + matches) + def test_property_escapes(self): import unicodedata # Properties that reuse the engine categories behave exactly like diff --git a/Misc/NEWS.d/next/Library/2026-11-19-09-56-47.gh-issue-161814.XDB2-G.rst b/Misc/NEWS.d/next/Library/2026-11-19-09-56-47.gh-issue-161814.XDB2-G.rst new file mode 100644 index 000000000000000..a1bcab5c575cea2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-09-56-47.gh-issue-161814.XDB2-G.rst @@ -0,0 +1,2 @@ +Fix Unicode property matching in :mod:`re` case-insensitive character sets +that also contain literals or ranges. diff --git a/Modules/_sre/sre_lib.h b/Modules/_sre/sre_lib.h index fbdbaefb399193d..01668c2bd724d9f 100644 --- a/Modules/_sre/sre_lib.h +++ b/Modules/_sre/sre_lib.h @@ -89,7 +89,7 @@ SRE(at)(SRE_STATE* state, const SRE_CHAR* ptr, SRE_CODE at) } LOCAL(int) -SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch) +SRE(charset_match)(const SRE_CODE* set, SRE_CODE ch, SRE_CODE category_ch) { /* check if character is a member of the given set */ @@ -109,8 +109,8 @@ SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch) break; case SRE_OP_CATEGORY: - /* */ - if (sre_category(set[0], (int) ch)) + /* , matched against the original character */ + if (sre_category(set[0], (int) category_ch)) return ok; set++; break; @@ -175,6 +175,12 @@ SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch) } } +LOCAL(int) +SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch) +{ + return SRE(charset_match)(set, ch, ch); +} + /* Like SRE(charset), but matches both locale cases of ch against every set member. Testing the whole set once per case would complement it before 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) TARGET(SRE_OP_IN_IGNORE): TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr)); if (ptr >= end - || !SRE(charset)(state, pattern+1, - (SRE_CODE)sre_lower_ascii(*ptr))) + || !SRE(charset_match)(pattern + 1, + sre_lower_ascii(*ptr), *ptr)) RETURN_FAILURE; pattern += pattern[0]; ptr++; @@ -888,8 +894,8 @@ SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) TARGET(SRE_OP_IN_UNI_IGNORE): TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr)); if (ptr >= end - || !SRE(charset)(state, pattern+1, - (SRE_CODE)sre_lower_unicode(*ptr))) + || !SRE(charset_match)(pattern + 1, + sre_lower_unicode(*ptr), *ptr)) RETURN_FAILURE; pattern += pattern[0]; ptr++;