Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix Unicode property matching in :mod:`re` case-insensitive character sets
that also contain literals or ranges.
20 changes: 13 additions & 7 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

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

case SRE_OP_CATEGORY:
/* <CATEGORY> <code> */
if (sre_category(set[0], (int) ch))
/* <CATEGORY> <code>, matched against the original character */
if (sre_category(set[0], (int) category_ch))
return ok;
set++;
break;
Expand Down Expand Up @@ -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'.
Expand Down Expand Up @@ -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++;
Expand All @@ -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++;
Expand Down
Loading