GeezerVison is showing github.com — [mypyc] Add `librt.strings.isidentifier` codepoint primitive (#21522) · python/mypy@965dd31
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 |
|---|
@@ -47,3 +47,4 @@ def isspace(c: i32, /) -> bool: ... | 47 | 47 | def isdigit(c: i32, /) -> bool: ... | 48 | 48 | def isalnum(c: i32, /) -> bool: ... | 49 | 49 | def isalpha(c: i32, /) -> bool: ... | | 50 | +def isidentifier(c: i32, /) -> bool: ... |
Collapse file Copy file name to clipboardExpand all lines: mypyc/ir/deps.py -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -116,5 +116,4 @@ def get_header(self) -> str: | 116 | 116 | STRING_WRITER_EXTRA_OPS: Final = SourceDep("stringwriter_extra_ops.c") | 117 | 117 | BYTEARRAY_EXTRA_OPS: Final = SourceDep("bytearray_extra_ops.c") | 118 | 118 | STR_EXTRA_OPS: Final = SourceDep("str_extra_ops.c") | 119 | | -CODEPOINT_EXTRA_OPS: Final = SourceDep("codepoint_extra_ops.c") | 120 | 119 | VECS_EXTRA_OPS: Final = SourceDep("vecs_extra_ops.c") |
Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/codepoint_extra_ops.c -8Lines changed: 0 additions & 8 deletions Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/codepoint_extra_ops.h -28Lines changed: 0 additions & 28 deletions Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/strings/librt_strings.c +9-10Lines changed: 9 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | #include <Python.h> | 5 | 5 | #include <stdint.h> | 6 | 6 | #include "CPy.h" | 7 | | -#include "codepoint_extra_ops.h" | 8 | 7 | #include "librt_strings.h" | 9 | 8 |
| 10 | 9 | #define CPY_BOOL_ERROR 2 | @@ -1154,15 +1153,11 @@ read_f64_be(PyObject *module, PyObject *const *args, size_t nargs) { | 1154 | 1153 | return PyFloat_FromDouble(CPyBytes_ReadF64BEUnsafe(data + index)); | 1155 | 1154 | } | 1156 | 1155 |
| 1157 | | -// Codepoint classification helpers exposed to interpreted callers. | 1158 | | -// The C-side names are prefixed `cp_` to avoid colliding with libc's | 1159 | | -// <ctype.h> isspace / isdigit / etc. Compiled callers go through the | 1160 | | -// LibRTStrings_* static inlines in codepoint_extra_ops.h instead. | 1161 | | -// | 1162 | | -// All wrappers parse a single int argument as i32 (codepoint) and | 1163 | | -// dispatch to the corresponding LibRTStrings_* function. The parse | 1164 | | -// step accepts any int but rejects values outside the i32 range with | 1165 | | -// OverflowError, matching the input domain of the compiled fast path. | | 1156 | +// Python-level wrappers (`cp_*`) for interpreted callers. The C-side names | | 1157 | +// are prefixed `cp_` to avoid colliding with libc's <ctype.h> isspace etc. | | 1158 | +// The LibRTStrings_Is* helpers themselves are static inline in librt_strings.h | | 1159 | +// so they compile directly into mypyc-emitted code with no capsule | | 1160 | +// indirection. | 1166 | 1161 |
| 1167 | 1162 | // Parse a Python int as i32 codepoint. Returns 0 on success and writes | 1168 | 1163 | // the value to *out; returns -1 on error with a Python exception set. | @@ -1194,6 +1189,7 @@ DEFINE_CP_BOOL_WRAPPER(isspace, LibRTStrings_IsSpace) | 1194 | 1189 | DEFINE_CP_BOOL_WRAPPER(isdigit, LibRTStrings_IsDigit) | 1195 | 1190 | DEFINE_CP_BOOL_WRAPPER(isalnum, LibRTStrings_IsAlnum) | 1196 | 1191 | DEFINE_CP_BOOL_WRAPPER(isalpha, LibRTStrings_IsAlpha) | | 1192 | +DEFINE_CP_BOOL_WRAPPER(isidentifier, LibRTStrings_IsIdentifier) | 1197 | 1193 |
| 1198 | 1194 | static PyMethodDef librt_strings_module_methods[] = { | 1199 | 1195 | {"write_i16_le", (PyCFunction) write_i16_le, METH_FASTCALL, | @@ -1268,6 +1264,9 @@ static PyMethodDef librt_strings_module_methods[] = { | 1268 | 1264 | {"isalpha", cp_isalpha, METH_O, | 1269 | 1265 | PyDoc_STR("Test whether a codepoint (i32) is a Unicode letter.") | 1270 | 1266 | }, | | 1267 | + {"isidentifier", cp_isidentifier, METH_O, | | 1268 | + PyDoc_STR("Test whether a codepoint (i32) is a valid identifier start (XID_Start).") | | 1269 | + }, | 1271 | 1270 | {NULL, NULL, 0, NULL} | 1272 | 1271 | }; | 1273 | 1272 |
| |
Collapse file Copy file name to clipboardExpand all lines: mypyc/lib-rt/strings/librt_strings.h +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | #include <Python.h> | 5 | 5 | #include <stdbool.h> | | 6 | +#include <stdint.h> | | 7 | +#include "CPy.h" | 6 | 8 | #include "librt_strings_common.h" | 7 | 9 |
| 8 | 10 | // ABI version -- only an exact match is compatible. This will only be changed in | @@ -28,4 +30,47 @@ typedef struct { | 28 | 30 | char data[WRITER_EMBEDDED_BUF_LEN]; // Default buffer | 29 | 31 | } StringWriterObject; | 30 | 32 |
| | 33 | +// Codepoint classification helpers. Inputs are signed i32 for compatibility | | 34 | +// with mypyc's int32_rprimitive; negative values are non-codepoints and | | 35 | +// return false. Defined `static inline` so they compile statically into | | 36 | +// both the librt.strings module and any mypyc-compiled extension that | | 37 | +// includes this header, avoiding the capsule indirection that would dwarf | | 38 | +// the work of a single Py_UNICODE_IS* macro call. | | 39 | + | | 40 | +static inline bool LibRTStrings_IsSpace(int32_t c) { | | 41 | + return c >= 0 && Py_UNICODE_ISSPACE((Py_UCS4)c); | | 42 | +} | | 43 | + | | 44 | +static inline bool LibRTStrings_IsDigit(int32_t c) { | | 45 | + return c >= 0 && Py_UNICODE_ISDIGIT((Py_UCS4)c); | | 46 | +} | | 47 | + | | 48 | +static inline bool LibRTStrings_IsAlnum(int32_t c) { | | 49 | + return c >= 0 && Py_UNICODE_ISALNUM((Py_UCS4)c); | | 50 | +} | | 51 | + | | 52 | +static inline bool LibRTStrings_IsAlpha(int32_t c) { | | 53 | + return c >= 0 && Py_UNICODE_ISALPHA((Py_UCS4)c); | | 54 | +} | | 55 | + | | 56 | +// True if c could start a valid identifier (XID_Start, per PEP 3131). | | 57 | +// ASCII fast path covers `[A-Za-z_]`; non-ASCII delegates to CPython's | | 58 | +// PyUnicode_IsIdentifier on a 1-character string. Aborts via | | 59 | +// CPyError_OutOfMemory on allocation failure to keep this ERR_NEVER. | | 60 | +static inline bool LibRTStrings_IsIdentifier(int32_t c) { | | 61 | + if (c < 0) return false; | | 62 | + if (c < 128) { | | 63 | + return (c >= 'a' && c <= 'z') | | 64 | + || (c >= 'A' && c <= 'Z') | | 65 | + || c == '_'; | | 66 | + } | | 67 | + PyObject *s = PyUnicode_FromOrdinal((int)c); | | 68 | + if (s == NULL) { | | 69 | + CPyError_OutOfMemory(); | | 70 | + } | | 71 | + int r = PyUnicode_IsIdentifier(s); | | 72 | + Py_DECREF(s); | | 73 | + return r == 1; | | 74 | +} | | 75 | + | 31 | 76 | #endif // LIBRT_STRINGS_H |
Collapse file Copy file name to clipboardExpand all lines: mypyc/primitives/librt_strings_ops.py +17-10Lines changed: 17 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -from mypyc.ir.deps import ( | 2 | | - BYTES_WRITER_EXTRA_OPS, | 3 | | - CODEPOINT_EXTRA_OPS, | 4 | | - LIBRT_STRINGS, | 5 | | - STRING_WRITER_EXTRA_OPS, | 6 | | -) | | 1 | +from mypyc.ir.deps import BYTES_WRITER_EXTRA_OPS, LIBRT_STRINGS, STRING_WRITER_EXTRA_OPS | 7 | 2 | from mypyc.ir.ops import ERR_MAGIC, ERR_MAGIC_OVERLAPPING, ERR_NEVER | 8 | 3 | from mypyc.ir.rtypes import ( | 9 | 4 | bool_rprimitive, | | 402 | 397 | return_type=bool_rprimitive, | 403 | 398 | c_function_name="LibRTStrings_IsSpace", | 404 | 399 | error_kind=ERR_NEVER, | 405 | | - dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS], | | 400 | + dependencies=[LIBRT_STRINGS], | 406 | 401 | ) | 407 | 402 |
| 408 | 403 | function_op( | | 411 | 406 | return_type=bool_rprimitive, | 412 | 407 | c_function_name="LibRTStrings_IsDigit", | 413 | 408 | error_kind=ERR_NEVER, | 414 | | - dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS], | | 409 | + dependencies=[LIBRT_STRINGS], | 415 | 410 | ) | 416 | 411 |
| 417 | 412 | function_op( | | 420 | 415 | return_type=bool_rprimitive, | 421 | 416 | c_function_name="LibRTStrings_IsAlnum", | 422 | 417 | error_kind=ERR_NEVER, | 423 | | - dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS], | | 418 | + dependencies=[LIBRT_STRINGS], | 424 | 419 | ) | 425 | 420 |
| 426 | 421 | function_op( | | 429 | 424 | return_type=bool_rprimitive, | 430 | 425 | c_function_name="LibRTStrings_IsAlpha", | 431 | 426 | error_kind=ERR_NEVER, | 432 | | - dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS], | | 427 | + dependencies=[LIBRT_STRINGS], | | 428 | +) | | 429 | + | | 430 | +# isidentifier checks XID_Start semantics for a single codepoint, matching | | 431 | +# str.isidentifier() on a 1-character string. The non-ASCII path allocates | | 432 | +# and aborts via CPyError_OutOfMemory on failure, so this stays ERR_NEVER. | | 433 | +function_op( | | 434 | + name="librt.strings.isidentifier", | | 435 | + arg_types=[int32_rprimitive], | | 436 | + return_type=bool_rprimitive, | | 437 | + c_function_name="LibRTStrings_IsIdentifier", | | 438 | + error_kind=ERR_NEVER, | | 439 | + dependencies=[LIBRT_STRINGS], | 433 | 440 | ) |
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 |
|---|
@@ -387,3 +387,17 @@ def is_a(c): | 387 | 387 | L0: | 388 | 388 | r0 = LibRTStrings_IsAlpha(c) | 389 | 389 | return r0 | | 390 | + | | 391 | +[case testLibrtStringsIsIdentifierIR] | | 392 | +from librt.strings import isidentifier | | 393 | +from mypy_extensions import i32 | | 394 | + | | 395 | +def is_id(c: i32) -> bool: | | 396 | + return isidentifier(c) | | 397 | +[out] | | 398 | +def is_id(c): | | 399 | + c :: i32 | | 400 | + r0 :: bool | | 401 | +L0: | | 402 | + r0 = LibRTStrings_IsIdentifier(c) | | 403 | + 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, isalpha | | 1446 | +from librt.strings import isspace, isdigit, isalnum, isalpha, isidentifier | 1447 | 1447 |
| 1448 | 1448 | from testutil import assertRaises | 1449 | 1449 |
| @@ -1455,6 +1455,7 @@ def test_codepoint_classifiers() -> None: | 1455 | 1455 | assert not isdigit(bad) | 1456 | 1456 | assert not isalnum(bad) | 1457 | 1457 | assert not isalpha(bad) | | 1458 | + assert not isidentifier(bad) | 1458 | 1459 | # Verify each codepoint primitive agrees with the matching str method | 1459 | 1460 | # across all Unicode codepoints, including the ord(chr(i)) round-trip. | 1460 | 1461 | # Any forces generic dispatch on the str side. | @@ -1466,6 +1467,7 @@ def test_codepoint_classifiers() -> None: | 1466 | 1467 | assert isdigit(o) == isdigit(i) == a.isdigit() | 1467 | 1468 | assert isalnum(o) == isalnum(i) == a.isalnum() | 1468 | 1469 | assert isalpha(o) == isalpha(i) == a.isalpha() | | 1470 | + assert isidentifier(o) == isidentifier(i) == a.isidentifier() | 1469 | 1471 |
| 1470 | 1472 |
| 1471 | 1473 | def test_codepoint_classifiers_via_any() -> None: | @@ -1476,6 +1478,7 @@ def test_codepoint_classifiers_via_any() -> None: | 1476 | 1478 | (isdigit, "5", "a"), | 1477 | 1479 | (isalnum, "A", " "), | 1478 | 1480 | (isalpha, "A", " "), | | 1481 | + (isidentifier, "A", "0"), | 1479 | 1482 | ): | 1480 | 1483 | f: Any = fn | 1481 | 1484 | assert f(ord(true_input)) is True | |
|