Skip to content

Commit 2bb7078

Browse files
authored
[mypyc] Fix i16 on Python 3.12 (#15510)
Two recent mypyc PRs didn't work together (#15470 and #15464). Fix them.
1 parent 9bd8563 commit 2bb7078

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

mypyc/lib-rt/int_ops.c

+13
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,18 @@ void CPyInt32_Overflow() {
654654

655655
int16_t CPyLong_AsInt16(PyObject *o) {
656656
if (likely(PyLong_Check(o))) {
657+
#if CPY_3_12_FEATURES
658+
PyLongObject *lobj = (PyLongObject *)o;
659+
size_t tag = CPY_LONG_TAG(lobj);
660+
if (likely(tag == (1 << CPY_NON_SIZE_BITS))) {
661+
// Fast path
662+
digit x = CPY_LONG_DIGIT(lobj, 0);
663+
if (x < 0x8000)
664+
return x;
665+
} else if (likely(tag == CPY_SIGN_ZERO)) {
666+
return 0;
667+
}
668+
#else
657669
PyLongObject *lobj = (PyLongObject *)o;
658670
Py_ssize_t size = lobj->ob_base.ob_size;
659671
if (likely(size == 1)) {
@@ -664,6 +676,7 @@ int16_t CPyLong_AsInt16(PyObject *o) {
664676
} else if (likely(size == 0)) {
665677
return 0;
666678
}
679+
#endif
667680
}
668681
// Slow path
669682
int overflow;

0 commit comments

Comments
 (0)