From 7725befe33813070e355aebbde4a6c23c1685b9d Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Mon, 9 Nov 2026 11:42:15 +0500 Subject: [PATCH 1/2] [mypyc] Add primitive for str.isidentifier ASCII strings are checked using a fast path. Other strings use PyUnicode_IsIdentifier. Part of mypyc/mypyc#644. --- mypyc/doc/str_operations.rst | 1 + mypyc/lib-rt/CPy.h | 1 + mypyc/lib-rt/str_ops.c | 18 +++++++++++ mypyc/primitives/str_ops.py | 9 ++++++ mypyc/test-data/fixtures/ir.py | 1 + mypyc/test-data/irbuild-str.test | 11 +++++++ mypyc/test-data/run-strings.test | 54 ++++++++++++++++++++++++++++++++ 7 files changed, 95 insertions(+) diff --git a/mypyc/doc/str_operations.rst b/mypyc/doc/str_operations.rst index 8f2daba02b72d..8efeaef87b349 100644 --- a/mypyc/doc/str_operations.rst +++ b/mypyc/doc/str_operations.rst @@ -46,6 +46,7 @@ Methods * ``s1.find(s2: str, start: int, end: int)`` * ``s.isalnum()`` * ``s.isdigit()`` +* ``s.isidentifier()`` * ``s.isspace()`` * ``s.lower()`` * ``s.join(x: Iterable)`` diff --git a/mypyc/lib-rt/CPy.h b/mypyc/lib-rt/CPy.h index 65cc8f038e508..ec71520a38ce1 100644 --- a/mypyc/lib-rt/CPy.h +++ b/mypyc/lib-rt/CPy.h @@ -1043,6 +1043,7 @@ PyObject *CPyStr_Upper(PyObject *str); bool CPyStr_IsSpace(PyObject *str); bool CPyStr_IsAlnum(PyObject *str); bool CPyStr_IsDigit(PyObject *str); +bool CPyStr_IsIdentifier(PyObject *str); // Bytes operations diff --git a/mypyc/lib-rt/str_ops.c b/mypyc/lib-rt/str_ops.c index 9e3d494868017..c0b248ae9ada2 100644 --- a/mypyc/lib-rt/str_ops.c +++ b/mypyc/lib-rt/str_ops.c @@ -806,3 +806,21 @@ bool CPyStr_IsDigit(PyObject *str) { #undef CHECK_ISDIGIT } + +bool CPyStr_IsIdentifier(PyObject *str) { + if (PyUnicode_IS_ASCII(str)) { + // ASCII fast path: [A-Za-z_][A-Za-z0-9_]* + Py_ssize_t len = PyUnicode_GET_LENGTH(str); + if (len == 0) return false; + const Py_UCS1 *data = PyUnicode_1BYTE_DATA(str); + if (!Py_ISALPHA(data[0]) && data[0] != '_') + return false; + for (Py_ssize_t i = 1; i < len; i++) { + if (!Py_ISALNUM(data[i]) && data[i] != '_') + return false; + } + return true; + } + // Non-ASCII identifiers need XID_Start/XID_Continue checks (PEP 3131) + return PyUnicode_IsIdentifier(str) == 1; +} diff --git a/mypyc/primitives/str_ops.py b/mypyc/primitives/str_ops.py index 27cf79e62352d..fa715b4807d99 100644 --- a/mypyc/primitives/str_ops.py +++ b/mypyc/primitives/str_ops.py @@ -439,6 +439,15 @@ error_kind=ERR_NEVER, ) +# str.isidentifier() +method_op( + name="isidentifier", + arg_types=[str_rprimitive], + return_type=bool_rprimitive, + c_function_name="CPyStr_IsIdentifier", + error_kind=ERR_NEVER, +) + # obj.decode() method_op( diff --git a/mypyc/test-data/fixtures/ir.py b/mypyc/test-data/fixtures/ir.py index 8fed798bbc695..d477309751263 100644 --- a/mypyc/test-data/fixtures/ir.py +++ b/mypyc/test-data/fixtures/ir.py @@ -140,6 +140,7 @@ def count(self, substr: str, start: Optional[int] = None, end: Optional[int] = N def isspace(self) -> bool: ... def isalnum(self) -> bool: ... def isdigit(self) -> bool: ... + def isidentifier(self) -> bool: ... class float: def __init__(self, x: object) -> None: pass diff --git a/mypyc/test-data/irbuild-str.test b/mypyc/test-data/irbuild-str.test index bf2773196cdec..d6e57e7725694 100644 --- a/mypyc/test-data/irbuild-str.test +++ b/mypyc/test-data/irbuild-str.test @@ -1026,6 +1026,17 @@ L0: r0 = CPyStr_IsDigit(x) return r0 +[case testStrIsIdentifier] +def is_identifier(x: str) -> bool: + return x.isidentifier() +[out] +def is_identifier(x): + x :: str + r0 :: bool +L0: + r0 = CPyStr_IsIdentifier(x) + return r0 + [case testStrIndexEqLiteral_64bit] def is_comma(s: str, i: int) -> bool: return s[i] == "," diff --git a/mypyc/test-data/run-strings.test b/mypyc/test-data/run-strings.test index f26b160d816c1..b0404daee4d1a 100644 --- a/mypyc/test-data/run-strings.test +++ b/mypyc/test-data/run-strings.test @@ -1410,6 +1410,60 @@ def test_isdigit_strings() -> None: assert not "123\u00e9".isdigit() assert not "\U0001d7ce!".isdigit() +[case testStrIsIdentifier] +from typing import Any + +def check(s: str) -> None: + # Any forces generic dispatch, so we compare our primitive against CPython's + a: Any = s + assert s.isidentifier() == a.isidentifier(), f"isidentifier mismatch for {s!r}" + +def test_all_code_points() -> None: + # Covers the ASCII, UCS-1, UCS-2 and UCS-4 string kinds, and both the start + # and continuation positions + for i in range(0x110000): + c = chr(i) + check(c) + check("a" + c) + +def test_strings() -> None: + samples = [ + "", "_", "__", "a", "abc", "ABC", "_abc_123", "abc_", "class", "None", "1", "1abc", + "abc-def", "a b", "abc!", "abc\n", "\u00e9t\u00e9", "\u00e9", "\u4e2d\u6587", "x\u0660", + "\u0660x", "x\u00b2", "\u2167", "\u212e", "\U00010400abc", "abc\U00010400", + "\U0001f600", "abc\u2000", "abc\u00a0", + ] + for s in samples: + check(s) + check(s + s) + +def test_isidentifier() -> None: + assert "abc".isidentifier() + assert "_".isidentifier() + assert "_abc_123".isidentifier() + assert "ABC".isidentifier() + assert "class".isidentifier() # Keywords are identifiers + assert not "".isidentifier() + assert not "1abc".isidentifier() + assert not "abc-def".isidentifier() + assert not "a b".isidentifier() + assert "\u00e9t\u00e9".isidentifier() + assert "\u4e2d\u6587".isidentifier() + assert "x\u0660".isidentifier() + assert not "\u0660x".isidentifier() + assert "\U00010400abc".isidentifier() + assert not "\U0001f600".isidentifier() + assert not "abc\u2000".isidentifier() + +def test_str_subclass() -> None: + from strsubclass import S + assert S("abc").isidentifier() + assert not S("1abc").isidentifier() + +[file strsubclass.py] +class S(str): + pass + [case testStrIndexEqLiteralSpecialize] from testutil import assertRaises From 623a2cc58c065e3f9e23d50404ec269cba61a32a Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Thu, 12 Nov 2026 17:06:31 +0500 Subject: [PATCH 2/2] Only check a sample of non-BMP code points in tests --- mypyc/test-data/run-strings.test | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mypyc/test-data/run-strings.test b/mypyc/test-data/run-strings.test index d4ec52cca13ad..0dcb5c9880589 100644 --- a/mypyc/test-data/run-strings.test +++ b/mypyc/test-data/run-strings.test @@ -1577,8 +1577,9 @@ def check(s: str) -> None: def test_all_code_points() -> None: # Covers the ASCII, UCS-1, UCS-2 and UCS-4 string kinds, and both the start - # and continuation positions - for i in range(0x110000): + # and continuation positions. Check all BMP code points, and a sample of the + # others (checking all of them is slow). + for i in [*range(0x10000), *range(0x10000, 0x110000, 97), 0x10FFFF]: c = chr(i) check(c) check("a" + c)