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 by VaggelisD · Pull Request #21522 · python/mypy

[mypyc] Add librt.strings.isidentifier codepoint primitive #21522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
p-sawicki merged 4 commits into python:master from VaggelisD:librt-strings-isidentifier
May 26, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

[mypyc] Add librt.strings.isidentifier codepoint primitive #21522

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit Hold shift + click to select a range
283b2f8
[mypyc] Add `librt.strings.isidentifier` codepoint primitive
VaggelisD May 8, 2026
472aab0
Address review: abort on OOM, keep _extra_ops out of librt build
VaggelisD May 22, 2026
35aabc8
Route codepoint helpers through the capsule API
VaggelisD May 25, 2026
ec0b294
Inline codepoint helpers in librt_strings.h, drop capsule entries
VaggelisD May 26, 2026
File filter
Filter by extension


Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
[mypyc] Add librt.strings.isidentifier codepoint primitive
True if a codepoint can start a valid identifier (XID_Start, per
PEP 3131). The ASCII fast path covers `[A-Za-z_]` inline; non-ASCII
codepoints round-trip through PyUnicode_FromOrdinal +
PyUnicode_IsIdentifier so the answer matches str.isidentifier on a
1-character string.

The non-ASCII path is the first allocating helper in this series, so
its body lives out-of-line in codepoint_extra_ops.c (it would otherwise
be emitted as a separate copy in every translation unit that includes
the header). On OOM it swallows the exception via PyErr_Clear() and
returns False, which keeps the function ERR_NEVER. Documented at the
call site so callers don't get a surprising silent failure.

Stack: depends on the librt.strings.isspace primitive.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@VaggelisD
VaggelisD committed May 21, 2026
commit 283b2f8ffcf2e9c832b295f7a48255a054e555fb

There are no files selected for viewing

1 change: 1 addition & 0 deletions 1 mypy/typeshed/stubs/librt/librt/strings.pyi
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ def isspace(c: i32, /) -> bool: ...
def isdigit(c: i32, /) -> bool: ...
def isalnum(c: i32, /) -> bool: ...
def isalpha(c: i32, /) -> bool: ...
def isidentifier(c: i32, /) -> bool: ...
7 changes: 6 additions & 1 deletion 7 mypyc/build.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ class ModDesc(NamedTuple):

LIBRT_MODULES = [
ModDesc("librt.internal", ["internal/librt_internal.c"], [], ["internal"]),
ModDesc("librt.strings", ["strings/librt_strings.c"], [], ["strings"]),
ModDesc(
"librt.strings",
["strings/librt_strings.c", "codepoint_extra_ops.c"],
["codepoint_extra_ops.h"],
["strings"],
),
Comment thread Outdated

@p-sawicki p-sawicki May 21, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for not catching this earlier but it looks like the files are dependent on each other the wrong way. the _extra_ops files should be internal to mypyc and shouldn't be needed when building librt modules.

so instead of including codepoint_extra_ops in librt_strings we need it the other way around, with the common functionality in librt_strings. like it's done with the other _extra_ops files.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@VaggelisD VaggelisD May 22, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, that's on me first!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@p-sawicki p-sawicki May 22, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! i see there's still #include "codepoint_extra_ops.h" in librt_strings.c and it would be good to clean that up by moving the functions from there to librt_strings.h.

that would make the codepoint_extra_ops files empty so maybe they aren't actually needed and we could just compile the functions statically straight from librt_strings.h? unless you're planning on adding functions later that would be better placed outside of librt_strings.h.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@VaggelisD VaggelisD May 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was planning on using it to add functions like toupper(), tolower() etc so that they're separated from librt_strings, what do you think? Worth the complexity?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@p-sawicki p-sawicki May 25, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would there be separate implementations used by the python wrapper in librt_strings and when compiled statically with mypyc? in that case sure, i think it'd make sense to have them in codepoint_extra_ops.

but if they're more similar to the functions added so far where the python wrapper calls the same function that mypyc could generate in the C file then they would have to be in librt_strings. it would be cleaner to keep the separation between the librt modules and C files relevant only in mypyc generated code by not including them by the librt module files.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@VaggelisD VaggelisD May 25, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, they'd be the same function afaict. Do we worry about the librt capsule indirection overhead? I assume these codepoint primitives will participate in hot loops so preferably they'd be inlined instead of going through the librt fp array (?)

I see the overall point though, there's no mypyc callers so there's no real point in having these outside of librt_strings.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@p-sawicki p-sawicki May 26, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah there would be overhead if the functions are exposed through the C_API array. but i think we could avoid it by defining the C functions inline in librt_strings.h instead. that way when compiling with mypyc, the function would be part of the extension and called statically.

i think this would be better than exposing through C_API especially for the functions that are very simple and thus wouldn't bloat the extension object files. edit: and i think it would be fine to define all the functions you've added so far as inline.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@VaggelisD VaggelisD May 26, 2026

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the PR, hopefully with what you had in mind

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@p-sawicki p-sawicki May 26, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i think this will work well. thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ModDesc(
"librt.base64",
[
Expand Down
16 changes: 14 additions & 2 deletions 16 mypyc/lib-rt/codepoint_extra_ops.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@
// The classification helpers and the ASCII fast paths for case conversion
// stay inline in codepoint_extra_ops.h; this file holds the slow paths
// that round-trip through PyUnicode_FromOrdinal and CPython's Unicode
// machinery. Currently empty; populated as later commits add
// isidentifier, toupper, and tolower.
// machinery.

bool LibRTStrings_IsIdentifier_slow(int32_t c) {
PyObject *s = PyUnicode_FromOrdinal((int)c);
if (s == NULL) {
// OOM. Swallow and return false to keep the function ERR_NEVER;
// callers expect a defined answer, not a propagated exception.
PyErr_Clear();
return false;
}
Comment thread
VaggelisD marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

int r = PyUnicode_IsIdentifier(s);
Py_DECREF(s);
return r == 1;
}
19 changes: 19 additions & 0 deletions 19 mypyc/lib-rt/codepoint_extra_ops.h
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,23 @@ static inline bool LibRTStrings_IsAlpha(int32_t c) {
return c >= 0 && Py_UNICODE_ISALPHA((Py_UCS4)c);
}

// Slow path for non-ASCII isidentifier; defined out-of-line in
// codepoint_extra_ops.c because it allocates and calls into CPython.
bool LibRTStrings_IsIdentifier_slow(int32_t c);

// True if c could start a valid identifier (matches XID_Start
// semantics, which is what str.isidentifier reports for a 1-character
// string). The ASCII fast path covers `[A-Za-z_]` inline; non-ASCII
// delegates to PyUnicode_IsIdentifier for correct PEP 3131 handling.
// Returns false on OOM in the slow path (the function stays ERR_NEVER).
static inline bool LibRTStrings_IsIdentifier(int32_t c) {
if (c < 0) return false;
if (c < 128) {
return (c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')
|| c == '_';
}
return LibRTStrings_IsIdentifier_slow(c);
}

#endif // MYPYC_CODEPOINT_EXTRA_OPS_H
1 change: 1 addition & 0 deletions 1 mypyc/lib-rt/setup.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def run(self) -> None:
"librt.strings",
[
"strings/librt_strings.c",
"codepoint_extra_ops.c",
"init.c",
"int_ops.c",
"exc_ops.c",
Expand Down
4 changes: 4 additions & 0 deletions 4 mypyc/lib-rt/strings/librt_strings.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ DEFINE_CP_BOOL_WRAPPER(isspace, LibRTStrings_IsSpace)
DEFINE_CP_BOOL_WRAPPER(isdigit, LibRTStrings_IsDigit)
DEFINE_CP_BOOL_WRAPPER(isalnum, LibRTStrings_IsAlnum)
DEFINE_CP_BOOL_WRAPPER(isalpha, LibRTStrings_IsAlpha)
DEFINE_CP_BOOL_WRAPPER(isidentifier, LibRTStrings_IsIdentifier)

static PyMethodDef librt_strings_module_methods[] = {
{"write_i16_le", (PyCFunction) write_i16_le, METH_FASTCALL,
Expand Down Expand Up @@ -1268,6 +1269,9 @@ static PyMethodDef librt_strings_module_methods[] = {
{"isalpha", cp_isalpha, METH_O,
PyDoc_STR("Test whether a codepoint (i32) is a Unicode letter.")
},
{"isidentifier", cp_isidentifier, METH_O,
PyDoc_STR("Test whether a codepoint (i32) is a valid identifier start (XID_Start).")
},
{NULL, NULL, 0, NULL}
};

Expand Down
12 changes: 12 additions & 0 deletions 12 mypyc/primitives/librt_strings_ops.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -431,3 +431,15 @@
error_kind=ERR_NEVER,
dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS],
)

# isidentifier checks XID_Start semantics for a single codepoint, matching
# str.isidentifier() on a 1-character string. The non-ASCII path allocates
# but swallows OOM (returning False), keeping the function ERR_NEVER.
function_op(
name="librt.strings.isidentifier",
arg_types=[int32_rprimitive],
return_type=bool_rprimitive,
c_function_name="LibRTStrings_IsIdentifier",
error_kind=ERR_NEVER,
dependencies=[LIBRT_STRINGS, CODEPOINT_EXTRA_OPS],
)
14 changes: 14 additions & 0 deletions 14 mypyc/test-data/irbuild-librt-strings.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,17 @@ def is_a(c):
L0:
r0 = LibRTStrings_IsAlpha(c)
return r0

[case testLibrtStringsIsIdentifierIR]
from librt.strings import isidentifier
from mypy_extensions import i32

def is_id(c: i32) -> bool:
return isidentifier(c)
[out]
def is_id(c):
c :: i32
r0 :: bool
L0:
r0 = LibRTStrings_IsIdentifier(c)
return r0
5 changes: 4 additions & 1 deletion 5 mypyc/test-data/run-librt-strings.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ def test_new_without_init_is_usable() -> None:
[case testLibrtStringsCodepointClassifiers_librt]
from typing import Any
from mypy_extensions import i32
from librt.strings import isspace, isdigit, isalnum, isalpha
from librt.strings import isspace, isdigit, isalnum, isalpha, isidentifier

from testutil import assertRaises

Expand All @@ -1455,6 +1455,7 @@ def test_codepoint_classifiers() -> None:
assert not isdigit(bad)
assert not isalnum(bad)
assert not isalpha(bad)
assert not isidentifier(bad)
# Verify each codepoint primitive agrees with the matching str method
# across all Unicode codepoints, including the ord(chr(i)) round-trip.
# Any forces generic dispatch on the str side.
Expand All @@ -1466,6 +1467,7 @@ def test_codepoint_classifiers() -> None:
assert isdigit(o) == isdigit(i) == a.isdigit()
assert isalnum(o) == isalnum(i) == a.isalnum()
assert isalpha(o) == isalpha(i) == a.isalpha()
assert isidentifier(o) == isidentifier(i) == a.isidentifier()


def test_codepoint_classifiers_via_any() -> None:
Expand All @@ -1476,6 +1478,7 @@ def test_codepoint_classifiers_via_any() -> None:
(isdigit, "5", "a"),
(isalnum, "A", " "),
(isalpha, "A", " "),
(isidentifier, "A", "0"),
):
f: Any = fn
assert f(ord(true_input)) is True
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
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.