Skip to content

Commit

Permalink
fix: update _PyLong_AsByteArray call for Python 3.13 API changes (#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
vowstar authored Oct 10, 2024
1 parent 71ba68b commit ffba2de
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions bindings/python/NumericBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,17 @@ static SVInt SVIntFromPyInt(const py::int_& value) {
size_t numBytes = ((bits - 1) / 32 + 1) * 4;
std::vector<byte> mem(numBytes);

int r = _PyLong_AsByteArray(reinterpret_cast<PyLongObject*>(value.ptr()),
reinterpret_cast<unsigned char*>(mem.data()), numBytes, 1, 1);
int r = -1;
#if PY_VERSION_HEX < 0x030D0000
r = _PyLong_AsByteArray(reinterpret_cast<PyLongObject*>(value.ptr()),
reinterpret_cast<unsigned char*>(mem.data()), numBytes, 1, 1);
#else
// fix build error with python 3.13
r = _PyLong_AsByteArray(reinterpret_cast<PyLongObject*>(value.ptr()),
reinterpret_cast<unsigned char*>(mem.data()), numBytes, 1, 1, 0);
// No exception is thrown here because it will be done later.
#endif

if (r == -1)
throw py::error_already_set();

Expand Down

0 comments on commit ffba2de

Please sign in to comment.