Skip to content

Commit eb68bce

Browse files
committed
Use the large PyLong conversion functions in Py3.13.
See python/cpython#111140
1 parent 884ce74 commit eb68bce

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

Cython/Utility/TypeConversion.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,20 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
692692
}
693693
}
694694
{
695-
int one = 1; int little = (int)*(unsigned char *)&one;
696695
unsigned char *bytes = (unsigned char *)&value;
697-
#if !CYTHON_COMPILING_IN_LIMITED_API && PY_VERSION_HEX < 0x030d0000
696+
#if !CYTHON_COMPILING_IN_LIMITED_API && PY_VERSION_HEX >= 0x030d00A4
697+
if (is_unsigned) {
698+
return PyLong_FromUnsignedNativeBytes(bytes, sizeof(value), -1);
699+
} else {
700+
return PyLong_FromNativeBytes(bytes, sizeof(value), -1);
701+
}
702+
#elif !CYTHON_COMPILING_IN_LIMITED_API && PY_VERSION_HEX < 0x030d0000
703+
int one = 1; int little = (int)*(unsigned char *)&one;
698704
return _PyLong_FromByteArray(bytes, sizeof({{TYPE}}),
699705
little, !is_unsigned);
700706
#else
701707
// call int.from_bytes()
708+
int one = 1; int little = (int)*(unsigned char *)&one;
702709
PyObject *from_bytes, *result = NULL, *kwds = NULL;
703710
PyObject *py_bytes = NULL, *order_str = NULL;
704711
from_bytes = PyObject_GetAttrString((PyObject*)&PyLong_Type, "from_bytes");
@@ -1008,7 +1015,16 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
10081015
PyObject *v = __Pyx_PyNumber_IntOrLong(x);
10091016
if (likely(v)) {
10101017
int ret = -1;
1011-
#if PY_VERSION_HEX < 0x030d0000 && !(CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_LIMITED_API) || defined(_PyLong_AsByteArray)
1018+
#if PY_VERSION_HEX >= 0x030d00A4 && !CYTHON_COMPILING_IN_LIMITED_API
1019+
Py_ssize_t bytes_copied = PyLong_AsNativeBytes(v, &val, sizeof(val), -1);
1020+
if (unlikely(bytes_copied == -1)) {
1021+
// failed
1022+
} else if (unlikely(bytes_copied > sizeof(val))) {
1023+
goto raise_overflow;
1024+
} else {
1025+
ret = 0;
1026+
}
1027+
#elif PY_VERSION_HEX < 0x030d0000 && !(CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_LIMITED_API) || defined(_PyLong_AsByteArray)
10121028
int one = 1; int is_little = (int)*(unsigned char *)&one;
10131029
unsigned char *bytes = (unsigned char *)&val;
10141030
ret = _PyLong_AsByteArray((PyLongObject *)v,

0 commit comments

Comments
 (0)