From f10d82957ccac5e987d9c1af713721ced22f6b13 Mon Sep 17 00:00:00 2001 From: maurycy <5383+maurycy@users.noreply.github.com> Date: Mon, 12 Oct 2026 21:15:48 +0200 Subject: [PATCH 1/4] up --- Modules/_sre/sre.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_sre/sre.c b/Modules/_sre/sre.c index 47c2c4abc3ef24..da816a25643074 100644 --- a/Modules/_sre/sre.c +++ b/Modules/_sre/sre.c @@ -170,7 +170,8 @@ static unsigned int sre_upper_locale(unsigned int ch) #define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE(ch) #define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK(ch) #define SRE_UNI_IS_ALNUM(ch) Py_UNICODE_ISALNUM(ch) -#define SRE_UNI_IS_WORD(ch) (SRE_UNI_IS_ALNUM(ch) || (ch) == '_') +#define SRE_UNI_IS_WORD(ch)\ + ((ch) < 128 ? SRE_IS_WORD(ch) : (SRE_UNI_IS_ALNUM(ch) || (ch) == '_')) #define SRE_UNI_IS_ALPHA(ch) Py_UNICODE_ISALPHA(ch) #define SRE_UNI_IS_LOWER(ch) Py_UNICODE_ISLOWER(ch) #define SRE_UNI_IS_UPPER(ch) Py_UNICODE_ISUPPER(ch) From d250b2070787c7a388ef0dcf422d3ccdfc7d2c23 Mon Sep 17 00:00:00 2001 From: maurycy <5383+maurycy@users.noreply.github.com> Date: Mon, 12 Oct 2026 21:22:25 +0200 Subject: [PATCH 2/4] news --- .../Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst diff --git a/Misc/NEWS.d/next/Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst b/Misc/NEWS.d/next/Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst new file mode 100644 index 00000000000000..7c2a8fc5f2191f --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst @@ -0,0 +1,3 @@ +Speed up ``\w``, ``\W``, ``\b`` and ``\B`` on ASCII characters in :mod:`re` +patterns by checking the ASCII table before the Unicode database. Patch by +Maurycy Pawłowski-Wieroński. From 6c5f44f8a16a657d3c82d4beb2eee0795a81ad77 Mon Sep 17 00:00:00 2001 From: maurycy <5383+maurycy@users.noreply.github.com> Date: Tue, 13 Oct 2026 09:53:50 +0200 Subject: [PATCH 3/4] https://github.com/python/cpython/issues/159251#issuecomment-6140434277 --- Include/cpython/unicodeobject.h | 7 +++---- Objects/unicodectype.c | 9 +++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Include/cpython/unicodeobject.h b/Include/cpython/unicodeobject.h index 54defdb2061aa9..7339a553736cf2 100644 --- a/Include/cpython/unicodeobject.h +++ b/Include/cpython/unicodeobject.h @@ -777,11 +777,10 @@ static inline int Py_UNICODE_ISSPACE(Py_UCS4 ch) { #define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch) +PyAPI_FUNC(int) _PyUnicode_IsAlnum(Py_UCS4 ch); + static inline int Py_UNICODE_ISALNUM(Py_UCS4 ch) { - return (Py_UNICODE_ISALPHA(ch) - || Py_UNICODE_ISDECIMAL(ch) - || Py_UNICODE_ISDIGIT(ch) - || Py_UNICODE_ISNUMERIC(ch)); + return _PyUnicode_IsAlnum(ch); } diff --git a/Objects/unicodectype.c b/Objects/unicodectype.c index fdd380190ac1ec..0a0f369272885c 100644 --- a/Objects/unicodectype.c +++ b/Objects/unicodectype.c @@ -279,6 +279,15 @@ int _PyUnicode_IsCaseIgnorable(Py_UCS4 ch) /* Returns 1 for Unicode characters having the category 'Ll', 'Lu', 'Lt', 'Lo' or 'Lm', 0 otherwise. */ +int +_PyUnicode_IsAlnum(Py_UCS4 ch) +{ + const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); + + return (ctype->flags + & (ALPHA_MASK | DECIMAL_MASK | DIGIT_MASK | NUMERIC_MASK)) != 0; +} + int _PyUnicode_IsAlpha(Py_UCS4 ch) { const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); From 651fc9de5f0ab34e23ecef02528bf90f4c58c8af Mon Sep 17 00:00:00 2001 From: maurycy <5383+maurycy@users.noreply.github.com> Date: Tue, 13 Oct 2026 10:31:37 +0200 Subject: [PATCH 4/4] up news --- .../Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst b/Misc/NEWS.d/next/Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst index 7c2a8fc5f2191f..634959cc940fab 100644 --- a/Misc/NEWS.d/next/Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst +++ b/Misc/NEWS.d/next/Library/2026-10-12-21-22-20.gh-issue-159251.8S5Lp_.rst @@ -1,3 +1,3 @@ -Speed up ``\w``, ``\W``, ``\b`` and ``\B`` on ASCII characters in :mod:`re` -patterns by checking the ASCII table before the Unicode database. Patch by -Maurycy Pawłowski-Wieroński. +Speed up :meth:`str.isalnum` and Unicode word matching in :mod:`re` (``\w``, +``\W``, ``\b`` and ``\B``) by reducing Unicode lookups and adding an ASCII fast +path. Patch by Maurycy Pawłowski-Wieroński.