From 3f1c98ae98cd0c075fabbec24194da4bb0e30b5f Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Sat, 24 Jun 2023 14:37:59 +0100 Subject: [PATCH] [mypyc] Fix i16 on Python 3.12 Two recent mypyc PRs didn't work together (#15470 and #15464). Fix them. --- mypyc/lib-rt/int_ops.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mypyc/lib-rt/int_ops.c b/mypyc/lib-rt/int_ops.c index 2a118d99b5eb..528401692a3a 100644 --- a/mypyc/lib-rt/int_ops.c +++ b/mypyc/lib-rt/int_ops.c @@ -654,6 +654,18 @@ void CPyInt32_Overflow() { int16_t CPyLong_AsInt16(PyObject *o) { if (likely(PyLong_Check(o))) { + #if CPY_3_12_FEATURES + PyLongObject *lobj = (PyLongObject *)o; + size_t tag = CPY_LONG_TAG(lobj); + if (likely(tag == (1 << CPY_NON_SIZE_BITS))) { + // Fast path + digit x = CPY_LONG_DIGIT(lobj, 0); + if (x < 0x8000) + return x; + } else if (likely(tag == CPY_SIGN_ZERO)) { + return 0; + } + #else PyLongObject *lobj = (PyLongObject *)o; Py_ssize_t size = lobj->ob_base.ob_size; if (likely(size == 1)) { @@ -664,6 +676,7 @@ int16_t CPyLong_AsInt16(PyObject *o) { } else if (likely(size == 0)) { return 0; } + #endif } // Slow path int overflow;