Skip to content

Commit

Permalink
Use the large PyLong conversion functions in Py3.13.
Browse files Browse the repository at this point in the history
  • Loading branch information
scoder committed May 3, 2024
1 parent 884ce74 commit eb68bce
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Cython/Utility/TypeConversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,13 +692,20 @@ static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
}
}
{
int one = 1; int little = (int)*(unsigned char *)&one;
unsigned char *bytes = (unsigned char *)&value;
#if !CYTHON_COMPILING_IN_LIMITED_API && PY_VERSION_HEX < 0x030d0000
#if !CYTHON_COMPILING_IN_LIMITED_API && PY_VERSION_HEX >= 0x030d00A4
if (is_unsigned) {
return PyLong_FromUnsignedNativeBytes(bytes, sizeof(value), -1);
} else {
return PyLong_FromNativeBytes(bytes, sizeof(value), -1);
}
#elif !CYTHON_COMPILING_IN_LIMITED_API && PY_VERSION_HEX < 0x030d0000
int one = 1; int little = (int)*(unsigned char *)&one;
return _PyLong_FromByteArray(bytes, sizeof({{TYPE}}),
little, !is_unsigned);
#else
// call int.from_bytes()
int one = 1; int little = (int)*(unsigned char *)&one;
PyObject *from_bytes, *result = NULL, *kwds = NULL;
PyObject *py_bytes = NULL, *order_str = NULL;
from_bytes = PyObject_GetAttrString((PyObject*)&PyLong_Type, "from_bytes");
Expand Down Expand Up @@ -1008,7 +1015,16 @@ static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *x) {
PyObject *v = __Pyx_PyNumber_IntOrLong(x);
if (likely(v)) {
int ret = -1;
#if PY_VERSION_HEX < 0x030d0000 && !(CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_LIMITED_API) || defined(_PyLong_AsByteArray)
#if PY_VERSION_HEX >= 0x030d00A4 && !CYTHON_COMPILING_IN_LIMITED_API
Py_ssize_t bytes_copied = PyLong_AsNativeBytes(v, &val, sizeof(val), -1);
if (unlikely(bytes_copied == -1)) {
// failed
} else if (unlikely(bytes_copied > sizeof(val))) {
goto raise_overflow;
} else {
ret = 0;
}
#elif PY_VERSION_HEX < 0x030d0000 && !(CYTHON_COMPILING_IN_PYPY || CYTHON_COMPILING_IN_LIMITED_API) || defined(_PyLong_AsByteArray)
int one = 1; int is_little = (int)*(unsigned char *)&one;
unsigned char *bytes = (unsigned char *)&val;
ret = _PyLong_AsByteArray((PyLongObject *)v,
Expand Down

0 comments on commit eb68bce

Please sign in to comment.