Skip to content

Commit 37f35db

Browse files
authored
[mypyc] Enable assertions in tests and fix failures (#20553)
For some reason assertions are disabled when running mypyc tests. Explicitly undefining `NDEBUG` when compiling enables them again so I have added compiler options to undefine it when requested optimization level is 0. Found two issues after enabling them: - When interning strings there is an assertion that makes calling the function multiple times crash. It might be called multiple times when multiple modules are initialized within one compilation unit so I have added an early return when the strings are already interned. - `Py_SIZE` has an [assertion](https://github.com/python/cpython/blob/2750f3c9b5c5e3787ebf4637125c9d746ad4694b/Include/object.h#L231) to disallow calling it with `PyLong` arguments starting from python 3.12. Compiled code would call it when converting `PyLong` to `int64`. Instead we can use `PyUnstable_Long_CompactValue`.
1 parent 4912d77 commit 37f35db

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

mypyc/build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,10 +680,13 @@ def mypycify(
680680
cflags.append("-DMYPYC_LOG_TRACE")
681681
if experimental_features:
682682
cflags.append("-DMYPYC_EXPERIMENTAL")
683+
if opt_level == "0":
684+
cflags.append("-UNDEBUG")
683685
elif compiler.compiler_type == "msvc":
684686
# msvc doesn't have levels, '/O2' is full and '/Od' is disable
685687
if opt_level == "0":
686688
opt_level = "d"
689+
cflags.append("/UNDEBUG")
687690
elif opt_level in ("1", "2", "3"):
688691
opt_level = "2"
689692
if debug_level == "0":

mypyc/lib-rt/CPy.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,19 @@ static inline bool CPyTagged_IsLe(CPyTagged left, CPyTagged right) {
331331
static inline int64_t CPyLong_AsInt64(PyObject *o) {
332332
if (likely(PyLong_Check(o))) {
333333
PyLongObject *lobj = (PyLongObject *)o;
334+
#if CPY_3_12_FEATURES
335+
if (likely(PyUnstable_Long_IsCompact(lobj))) {
336+
return PyUnstable_Long_CompactValue(lobj);
337+
}
338+
#else
334339
Py_ssize_t size = Py_SIZE(lobj);
335340
if (likely(size == 1)) {
336341
// Fast path
337342
return CPY_LONG_DIGIT(lobj, 0);
338343
} else if (likely(size == 0)) {
339344
return 0;
340345
}
346+
#endif
341347
}
342348
// Slow path
343349
return CPyLong_AsInt64_(o);

mypyc/lib-rt/static_data.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ mypyc_interned_str_struct mypyc_interned_str;
1616

1717
int
1818
intern_strings(void) {
19+
if (mypyc_interned_str.values != NULL) {
20+
// Already interned.
21+
return 0;
22+
}
1923
INTERN_STRING(__init_subclass__, "__init_subclass__");
2024
INTERN_STRING(__mro_entries__, "__mro_entries__");
2125
INTERN_STRING(__name__, "__name__");

0 commit comments

Comments
 (0)