From 428353c46d9318658ebfb821fe0f1ec52ed5d839 Mon Sep 17 00:00:00 2001 From: Tom Tang Date: Thu, 22 Aug 2024 00:20:38 +0000 Subject: [PATCH] fix: Python 3.13 added the `PyLong_AsNativeBytes` API, but changed the function signature of the `_PyLong_AsByteArray` API See https://github.com/python/cpython/issues/111140 and https://github.com/capi-workgroup/decisions/issues/31 --- src/IntType.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/IntType.cc b/src/IntType.cc index c7a5cae5..30e974bf 100644 --- a/src/IntType.cc +++ b/src/IntType.cc @@ -139,7 +139,11 @@ JS::BigInt *IntType::toJsBigInt(JSContext *cx, PyObject *pyObject) { // Convert to bytes of 8-bit "digits" in **big-endian** order size_t byteCount = (size_t)JS_DIGIT_BYTE * jsDigitCount; uint8_t *bytes = (uint8_t *)PyMem_Malloc(byteCount); + #if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13 + _PyLong_AsByteArray((PyLongObject *)pyObject, bytes, byteCount, /*is_little_endian*/ false, false, false); + #else _PyLong_AsByteArray((PyLongObject *)pyObject, bytes, byteCount, /*is_little_endian*/ false, false); + #endif // Convert pm.bigint to JS::BigInt through hex strings (no public API to convert directly through bytes) // TODO (Tom Tang): We could manually allocate the memory, https://hg.mozilla.org/releases/mozilla-esr102/file/tip/js/src/vm/BigIntType.cpp#l162, but still no public API