Skip to content

Commit c4d9405

Browse files
committed
refactor: add shim for PyLong_AsByteArray to work with different function signatures
1 parent 6bf3d70 commit c4d9405

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

include/pyshim.hh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,18 @@ inline PyObject *PyObject_CallOneArg(PyObject *func, PyObject *arg) {
128128
}
129129
#endif
130130

131+
/**
132+
* @brief Shim for `_PyLong_AsByteArray`.
133+
* Python 3.13.0a4 added a new public API `PyLong_AsNativeBytes()` to replace the private `_PyLong_AsByteArray()`.
134+
* But this change also modified the function signature of `_PyLong_AsByteArray()`.
135+
* @see https://github.com/python/cpython/issues/111140
136+
*/
137+
inline int PyLong_AsByteArray(PyLongObject *v, unsigned char *bytes, size_t n, bool little_endian, bool is_signed) {
138+
#if PY_VERSION_HEX >= 0x030d0000 // Python version is 3.13 or higher
139+
return _PyLong_AsByteArray(v, bytes, n, little_endian, is_signed, /*with_exceptions*/ false);
140+
#else
141+
return _PyLong_AsByteArray(v, bytes, n, little_endian, is_signed);
142+
#endif
143+
}
144+
131145
#endif // #ifndef PythonMonkey_py_version_shim_

src/IntType.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,7 @@ JS::BigInt *IntType::toJsBigInt(JSContext *cx, PyObject *pyObject) {
134134
// Convert to bytes of 8-bit "digits" in **big-endian** order
135135
size_t byteCount = (size_t)JS_DIGIT_BYTE * jsDigitCount;
136136
uint8_t *bytes = (uint8_t *)PyMem_Malloc(byteCount);
137-
#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13
138-
_PyLong_AsByteArray((PyLongObject *)pyObject, bytes, byteCount, /*is_little_endian*/ false, false, false);
139-
#else
140-
_PyLong_AsByteArray((PyLongObject *)pyObject, bytes, byteCount, /*is_little_endian*/ false, false);
141-
#endif
137+
PyLong_AsByteArray((PyLongObject *)pyObject, bytes, byteCount, /*is_little_endian*/ false, false);
142138

143139
// Convert pm.bigint to JS::BigInt through hex strings (no public API to convert directly through bytes)
144140
// 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

0 commit comments

Comments
 (0)