GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [mypyc] Add `librt.strings.isidentifier` codepoint primitive · python/mypy@add1d44

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • mypyc
    • build.py
    • lib-rt
      • codepoint_extra_ops.c
      • codepoint_extra_ops.h
      • setup.py
      • strings
        • librt_strings.c
    • primitives
      • librt_strings_ops.py
    • test-data
      • irbuild-librt-strings.test
      • run-librt-strings.test
  • mypy/typeshed/stubs/librt/librt
    • strings.pyi
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎mypy/typeshed/stubs/librt/librt/strings.pyi‎

Copy file name to clipboardExpand all lines: mypy/typeshed/stubs/librt/librt/strings.pyi
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ def read_f64_be(b: bytes, index: i64, /) -> float: ...
4646
def isspace(c: i32, /) -> bool: ...
4747
def isdigit(c: i32, /) -> bool: ...
4848
def isalnum(c: i32, /) -> bool: ...
49+
def isidentifier(c: i32, /) -> bool: ...
Collapse file

‎mypyc/build.py‎

Copy file name to clipboardExpand all lines: mypyc/build.py
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ class ModDesc(NamedTuple):
5454

5555
LIBRT_MODULES = [
5656
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+
),
5863
ModDesc(
5964
"librt.base64",
6065
[
Collapse file

‎mypyc/lib-rt/codepoint_extra_ops.c‎

Copy file name to clipboardExpand all lines: mypyc/lib-rt/codepoint_extra_ops.c
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,17 @@
44
// The classification helpers and the ASCII fast paths for case conversion
55
// stay inline in codepoint_extra_ops.h; this file holds the slow paths
66
// 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

‎mypyc/lib-rt/codepoint_extra_ops.h‎

Copy file name to clipboardExpand all lines: mypyc/lib-rt/codepoint_extra_ops.h
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,23 @@ static inline bool LibRTStrings_IsAlnum(int32_t c) {
2121
return c >= 0 && Py_UNICODE_ISALNUM((Py_UCS4)c);
2222
}
2323

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+
2443
#endif // MYPYC_CODEPOINT_EXTRA_OPS_H
Collapse file

‎mypyc/lib-rt/setup.py‎

Copy file name to clipboardExpand all lines: mypyc/lib-rt/setup.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def run(self) -> None:
103103
"librt.strings",
104104
[
105105
"strings/librt_strings.c",
106+
"codepoint_extra_ops.c",
106107
"init.c",
107108
"int_ops.c",
108109
"exc_ops.c",
Collapse file

‎mypyc/lib-rt/strings/librt_strings.c‎

Copy file name to clipboardExpand all lines: mypyc/lib-rt/strings/librt_strings.c
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ cp_parse_i32(PyObject *arg, int32_t *out) {
11931193
DEFINE_CP_BOOL_WRAPPER(isspace, LibRTStrings_IsSpace)
11941194
DEFINE_CP_BOOL_WRAPPER(isdigit, LibRTStrings_IsDigit)
11951195
DEFINE_CP_BOOL_WRAPPER(isalnum, LibRTStrings_IsAlnum)
1196+
DEFINE_CP_BOOL_WRAPPER(isidentifier, LibRTStrings_IsIdentifier)
11961197

11971198
static PyMethodDef librt_strings_module_methods[] = {
11981199
{"write_i16_le", (PyCFunction) write_i16_le, METH_FASTCALL,
@@ -1264,6 +1265,9 @@ static PyMethodDef librt_strings_module_methods[] = {
12641265
{"isalnum", cp_isalnum, METH_O,
12651266
PyDoc_STR("Test whether a codepoint (i32) is alphanumeric.")
12661267
},
1268+
{"isidentifier", cp_isidentifier, METH_O,
1269+
PyDoc_STR("Test whether a codepoint (i32) is a valid identifier start (XID_Start).")
1270+
},
12671271
{NULL, NULL, 0, NULL}
12681272
};
12691273

Collapse file

‎mypyc/primitives/librt_strings_ops.py‎

Copy file name to clipboardExpand all lines: mypyc/primitives/librt_strings_ops.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,14 @@
422422
error_kind=ERR_NEVER,
423423
dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS],
424424
)
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

‎mypyc/test-data/irbuild-librt-strings.test‎

Copy file name to clipboardExpand all lines: mypyc/test-data/irbuild-librt-strings.test
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,17 @@ def is_an(c):
373373
L0:
374374
r0 = LibRTStrings_IsAlnum(c)
375375
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

‎mypyc/test-data/run-librt-strings.test‎

Copy file name to clipboardExpand all lines: mypyc/test-data/run-librt-strings.test
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,7 @@ def test_new_without_init_is_usable() -> None:
14431443
[case testLibrtStringsCodepointClassifiers_librt]
14441444
from typing import Any
14451445
from mypy_extensions import i32
1446-
from librt.strings import isspace, isdigit, isalnum
1446+
from librt.strings import isspace, isdigit, isalnum, isidentifier
14471447

14481448
from testutil import assertRaises
14491449

@@ -1454,6 +1454,7 @@ def test_codepoint_classifiers() -> None:
14541454
assert not isspace(bad)
14551455
assert not isdigit(bad)
14561456
assert not isalnum(bad)
1457+
assert not isidentifier(bad)
14571458
# Verify each codepoint primitive agrees with the matching str method
14581459
# across all Unicode codepoints, including the ord(chr(i)) round-trip.
14591460
# Any forces generic dispatch on the str side.
@@ -1464,6 +1465,7 @@ def test_codepoint_classifiers() -> None:
14641465
assert isspace(o) == isspace(i) == a.isspace()
14651466
assert isdigit(o) == isdigit(i) == a.isdigit()
14661467
assert isalnum(o) == isalnum(i) == a.isalnum()
1468+
assert isidentifier(o) == isidentifier(i) == a.isidentifier()
14671469

14681470

14691471
def test_codepoint_classifiers_via_any() -> None:
@@ -1473,6 +1475,7 @@ def test_codepoint_classifiers_via_any() -> None:
14731475
(isspace, " ", "a"),
14741476
(isdigit, "5", "a"),
14751477
(isalnum, "A", " "),
1478+
(isidentifier, "A", "0"),
14761479
):
14771480
f: Any = fn
14781481
assert f(ord(true_input)) is True

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.