Skip to content

Commit 472aab0

Browse files
committed
Address review: abort on OOM, keep _extra_ops out of librt build
- codepoint_extra_ops.h: include CPy.h and move the isidentifier slow path inline into LibRTStrings_IsIdentifier. Aborts via CPyError_OutOfMemory on allocation failure (the helper is ERR_NEVER, so returning a silently-wrong bool under memory pressure was the wrong contract). Matches the pattern in the sibling _extra_ops.h files (all bodies static inline, CPy.h included for runtime helpers). - codepoint_extra_ops.c: reduce to a single-line shim that #includes the header. Exists only so SourceDep("codepoint_extra_ops.c") pulls the header into user mypyc extensions in include_runtime_files mode. - build.py / lib-rt/setup.py: drop codepoint_extra_ops.c from the librt.strings module sources. The _extra_ops.c files are mypyc-internal (linked into user extensions via SourceDep in mypyc/ir/deps.py); the librt.strings Python module shouldn't depend on them, matching how bytes_extra_ops, str_extra_ops, etc. are organized. librt.strings now picks up LibRTStrings_IsIdentifier via #include of the header.
1 parent 283b2f8 commit 472aab0

4 files changed

Lines changed: 16 additions & 32 deletions

File tree

mypyc/build.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ class ModDesc(NamedTuple):
5454

5555
LIBRT_MODULES = [
5656
ModDesc("librt.internal", ["internal/librt_internal.c"], [], ["internal"]),
57-
ModDesc(
58-
"librt.strings",
59-
["strings/librt_strings.c", "codepoint_extra_ops.c"],
60-
["codepoint_extra_ops.h"],
61-
["strings"],
62-
),
57+
ModDesc("librt.strings", ["strings/librt_strings.c"], [], ["strings"]),
6358
ModDesc(
6459
"librt.base64",
6560
[

mypyc/lib-rt/codepoint_extra_ops.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1+
// All codepoint helper bodies live in codepoint_extra_ops.h as static
2+
// inline. This translation unit exists so the header is pulled into
3+
// mypyc-compiled extensions via SourceDep("codepoint_extra_ops.c") in
4+
// mypyc/ir/deps.py (which, in include_runtime_files mode, emits
5+
// `#include <codepoint_extra_ops.c>` into the generated __native.c).
16
#include "codepoint_extra_ops.h"
2-
3-
// Out-of-line bodies for codepoint helpers that are too large to inline.
4-
// The classification helpers and the ASCII fast paths for case conversion
5-
// stay inline in codepoint_extra_ops.h; this file holds the slow paths
6-
// that round-trip through PyUnicode_FromOrdinal and CPython's Unicode
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-
}

mypyc/lib-rt/codepoint_extra_ops.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <Python.h>
55
#include <stdbool.h>
66
#include <stdint.h>
7+
#include "CPy.h"
78

89
// Codepoint helpers for librt.strings.
910
// Inputs are signed int32_t for compatibility with mypyc's i32 type.
@@ -25,23 +26,26 @@ static inline bool LibRTStrings_IsAlpha(int32_t c) {
2526
return c >= 0 && Py_UNICODE_ISALPHA((Py_UCS4)c);
2627
}
2728

28-
// Slow path for non-ASCII isidentifier; defined out-of-line in
29-
// codepoint_extra_ops.c because it allocates and calls into CPython.
30-
bool LibRTStrings_IsIdentifier_slow(int32_t c);
31-
3229
// True if c could start a valid identifier (matches XID_Start
3330
// semantics, which is what str.isidentifier reports for a 1-character
3431
// string). The ASCII fast path covers `[A-Za-z_]` inline; non-ASCII
3532
// delegates to PyUnicode_IsIdentifier for correct PEP 3131 handling.
36-
// Returns false on OOM in the slow path (the function stays ERR_NEVER).
33+
// Aborts via CPyError_OutOfMemory on allocation failure, so this helper
34+
// stays ERR_NEVER.
3735
static inline bool LibRTStrings_IsIdentifier(int32_t c) {
3836
if (c < 0) return false;
3937
if (c < 128) {
4038
return (c >= 'a' && c <= 'z')
4139
|| (c >= 'A' && c <= 'Z')
4240
|| c == '_';
4341
}
44-
return LibRTStrings_IsIdentifier_slow(c);
42+
PyObject *s = PyUnicode_FromOrdinal((int)c);
43+
if (s == NULL) {
44+
CPyError_OutOfMemory();
45+
}
46+
int r = PyUnicode_IsIdentifier(s);
47+
Py_DECREF(s);
48+
return r == 1;
4549
}
4650

4751
#endif // MYPYC_CODEPOINT_EXTRA_OPS_H

mypyc/lib-rt/setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ def run(self) -> None:
103103
"librt.strings",
104104
[
105105
"strings/librt_strings.c",
106-
"codepoint_extra_ops.c",
107106
"init.c",
108107
"int_ops.c",
109108
"exc_ops.c",

0 commit comments

Comments
 (0)