GeezerVison is showing github.com — [mypyc] Add `librt.strings.isidentifier` codepoint primitive · python/mypy@add1d44
File treeExpand file treeCollapse file tree Open diff view settings mypy/typeshed/stubs/librt/librt
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: mypy/typeshed/stubs/librt/librt/strings.pyi +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,3 +46,4 @@ def read_f64_be(b: bytes, index: i64, /) -> float: ... | 46 | 46 | def isspace(c: i32, /) -> bool: ... | 47 | 47 | def isdigit(c: i32, /) -> bool: ... | 48 | 48 | def isalnum(c: i32, /) -> bool: ... | | 49 | +def isidentifier(c: i32, /) -> bool: ... |
Collapse file Copy file name to clipboardExpand all lines: mypyc/build.py +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,7 +54,12 @@ class ModDesc(NamedTuple): | 54 | 54 |
| 55 | 55 | LIBRT_MODULES = [ | 56 | 56 | ModDesc("librt.internal", ["internal/librt_internal.c"], [], ["internal"]), | 57 | | - ModDesc("librt.strings", ["strings/librt_strings.c"], [], ["strings"]), | | 57 | + ModDesc( | | 58 | + "librt.strings", | | 59 | + ["strings/librt_strings.c", "codepoint_extra_ops.c"], | | 60 | + ["codepoint_extra_ops.h"], | | 61 | + ["strings"], | | 62 | + ), | 58 | 63 | ModDesc( | 59 | 64 | "librt.base64", | 60 | 65 | [ | |
Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/codepoint_extra_ops.c +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | // The classification helpers and the ASCII fast paths for case conversion | 5 | 5 | // stay inline in codepoint_extra_ops.h; this file holds the slow paths | 6 | 6 | // that round-trip through PyUnicode_FromOrdinal and CPython's Unicode | 7 | | -// machinery. Currently empty; populated as later commits add | 8 | | -// isidentifier, toupper, and tolower. | | 7 | +// machinery. | | 8 | + | | 9 | +bool LibRTStrings_IsIdentifier_slow(int32_t c) { | | 10 | + PyObject *s = PyUnicode_FromOrdinal((int)c); | | 11 | + if (s == NULL) { | | 12 | + // OOM. Swallow and return false to keep the function ERR_NEVER; | | 13 | + // callers expect a defined answer, not a propagated exception. | | 14 | + PyErr_Clear(); | | 15 | + return false; | | 16 | + } | | 17 | + int r = PyUnicode_IsIdentifier(s); | | 18 | + Py_DECREF(s); | | 19 | + return r == 1; | | 20 | +} |
Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/codepoint_extra_ops.h +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,4 +21,23 @@ static inline bool LibRTStrings_IsAlnum(int32_t c) { | 21 | 21 | return c >= 0 && Py_UNICODE_ISALNUM((Py_UCS4)c); | 22 | 22 | } | 23 | 23 |
| | 24 | +// Slow path for non-ASCII isidentifier; defined out-of-line in | | 25 | +// codepoint_extra_ops.c because it allocates and calls into CPython. | | 26 | +bool LibRTStrings_IsIdentifier_slow(int32_t c); | | 27 | + | | 28 | +// True if c could start a valid identifier (matches XID_Start | | 29 | +// semantics, which is what str.isidentifier reports for a 1-character | | 30 | +// string). The ASCII fast path covers `[A-Za-z_]` inline; non-ASCII | | 31 | +// delegates to PyUnicode_IsIdentifier for correct PEP 3131 handling. | | 32 | +// Returns false on OOM in the slow path (the function stays ERR_NEVER). | | 33 | +static inline bool LibRTStrings_IsIdentifier(int32_t c) { | | 34 | + if (c < 0) return false; | | 35 | + if (c < 128) { | | 36 | + return (c >= 'a' && c <= 'z') | | 37 | + || (c >= 'A' && c <= 'Z') | | 38 | + || c == '_'; | | 39 | + } | | 40 | + return LibRTStrings_IsIdentifier_slow(c); | | 41 | +} | | 42 | + | 24 | 43 | #endif // MYPYC_CODEPOINT_EXTRA_OPS_H |
Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/setup.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -103,6 +103,7 @@ def run(self) -> None: | 103 | 103 | "librt.strings", | 104 | 104 | [ | 105 | 105 | "strings/librt_strings.c", | | 106 | + "codepoint_extra_ops.c", | 106 | 107 | "init.c", | 107 | 108 | "int_ops.c", | 108 | 109 | "exc_ops.c", | |
Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/strings/librt_strings.c +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1193,6 +1193,7 @@ cp_parse_i32(PyObject *arg, int32_t *out) { | 1193 | 1193 | DEFINE_CP_BOOL_WRAPPER(isspace, LibRTStrings_IsSpace) | 1194 | 1194 | DEFINE_CP_BOOL_WRAPPER(isdigit, LibRTStrings_IsDigit) | 1195 | 1195 | DEFINE_CP_BOOL_WRAPPER(isalnum, LibRTStrings_IsAlnum) | | 1196 | +DEFINE_CP_BOOL_WRAPPER(isidentifier, LibRTStrings_IsIdentifier) | 1196 | 1197 |
| 1197 | 1198 | static PyMethodDef librt_strings_module_methods[] = { | 1198 | 1199 | {"write_i16_le", (PyCFunction) write_i16_le, METH_FASTCALL, | @@ -1264,6 +1265,9 @@ static PyMethodDef librt_strings_module_methods[] = { | 1264 | 1265 | {"isalnum", cp_isalnum, METH_O, | 1265 | 1266 | PyDoc_STR("Test whether a codepoint (i32) is alphanumeric.") | 1266 | 1267 | }, | | 1268 | + {"isidentifier", cp_isidentifier, METH_O, | | 1269 | + PyDoc_STR("Test whether a codepoint (i32) is a valid identifier start (XID_Start).") | | 1270 | + }, | 1267 | 1271 | {NULL, NULL, 0, NULL} | 1268 | 1272 | }; | 1269 | 1273 |
| |
Collapse file Copy file name to clipboardExpand all lines: mypyc/primitives/librt_strings_ops.py +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 422 | 422 | error_kind=ERR_NEVER, | 423 | 423 | dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS], | 424 | 424 | ) | | 425 | +# isidentifier checks XID_Start semantics for a single codepoint, matching | | 426 | +# str.isidentifier() on a 1-character string. The non-ASCII path allocates | | 427 | +# but swallows OOM (returning False), keeping the function ERR_NEVER. | | 428 | +function_op( | | 429 | + name="librt.strings.isidentifier", | | 430 | + arg_types=[int32_rprimitive], | | 431 | + return_type=bool_rprimitive, | | 432 | + c_function_name="LibRTStrings_IsIdentifier", | | 433 | + error_kind=ERR_NEVER, | | 434 | + dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS], | | 435 | +) |
Collapse file Copy file name to clipboardExpand all lines: mypyc/test-data/irbuild-librt-strings.test +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -373,3 +373,17 @@ def is_an(c): | 373 | 373 | L0: | 374 | 374 | r0 = LibRTStrings_IsAlnum(c) | 375 | 375 | return r0 | | 376 | + | | 377 | +[case testLibrtStringsIsIdentifierIR] | | 378 | +from librt.strings import isidentifier | | 379 | +from mypy_extensions import i32 | | 380 | + | | 381 | +def is_id(c: i32) -> bool: | | 382 | + return isidentifier(c) | | 383 | +[out] | | 384 | +def is_id(c): | | 385 | + c :: i32 | | 386 | + r0 :: bool | | 387 | +L0: | | 388 | + r0 = LibRTStrings_IsIdentifier(c) | | 389 | + return r0 |
Collapse file Copy file name to clipboardExpand all lines: mypyc/test-data/run-librt-strings.test +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1443,7 +1443,7 @@ def test_new_without_init_is_usable() -> None: | 1443 | 1443 | [case testLibrtStringsCodepointClassifiers_librt] | 1444 | 1444 | from typing import Any | 1445 | 1445 | from mypy_extensions import i32 | 1446 | | -from librt.strings import isspace, isdigit, isalnum | | 1446 | +from librt.strings import isspace, isdigit, isalnum, isidentifier | 1447 | 1447 |
| 1448 | 1448 | from testutil import assertRaises | 1449 | 1449 |
| @@ -1454,6 +1454,7 @@ def test_codepoint_classifiers() -> None: | 1454 | 1454 | assert not isspace(bad) | 1455 | 1455 | assert not isdigit(bad) | 1456 | 1456 | assert not isalnum(bad) | | 1457 | + assert not isidentifier(bad) | 1457 | 1458 | # Verify each codepoint primitive agrees with the matching str method | 1458 | 1459 | # across all Unicode codepoints, including the ord(chr(i)) round-trip. | 1459 | 1460 | # Any forces generic dispatch on the str side. | @@ -1464,6 +1465,7 @@ def test_codepoint_classifiers() -> None: | 1464 | 1465 | assert isspace(o) == isspace(i) == a.isspace() | 1465 | 1466 | assert isdigit(o) == isdigit(i) == a.isdigit() | 1466 | 1467 | assert isalnum(o) == isalnum(i) == a.isalnum() | | 1468 | + assert isidentifier(o) == isidentifier(i) == a.isidentifier() | 1467 | 1469 |
| 1468 | 1470 |
| 1469 | 1471 | def test_codepoint_classifiers_via_any() -> None: | @@ -1473,6 +1475,7 @@ def test_codepoint_classifiers_via_any() -> None: | 1473 | 1475 | (isspace, " ", "a"), | 1474 | 1476 | (isdigit, "5", "a"), | 1475 | 1477 | (isalnum, "A", " "), | | 1478 | + (isidentifier, "A", "0"), | 1476 | 1479 | ): | 1477 | 1480 | f: Any = fn | 1478 | 1481 | assert f(ord(true_input)) is True | |
|