Skip to content

Commit 6030c85

Browse files
committed
bindings/blst.swg: make it work with Python 3.13.
1 parent 246f752 commit 6030c85

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

bindings/blst.swg

+21-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@
2020

2121
#if defined(SWIGPYTHON)
2222

23+
%header %{
24+
#if PY_VERSION_HEX<0x030d0000
25+
/* Tailored polyfill, for example no need to handle |n_bytes| == 0 here */
26+
Py_ssize_t PyLong_AsNativeBytes(PyObject* v, void* buffer, Py_ssize_t n_bytes,
27+
int flags)
28+
{
29+
return _PyLong_AsByteArray((PyLongObject*)v,
30+
(unsigned char*)buffer, n_bytes,
31+
flags&1, (flags&4) == 0) < 0 ? -1 : n_bytes;
32+
}
33+
# define My_PYLONG_FLAGS (1 | 4 | 8)
34+
#else
35+
# define My_PYLONG_FLAGS (Py_ASNATIVEBYTES_LITTLE_ENDIAN | \
36+
Py_ASNATIVEBYTES_UNSIGNED_BUFFER | \
37+
Py_ASNATIVEBYTES_REJECT_NEGATIVE)
38+
#endif
39+
%}
40+
2341
// some sorcery to allow assignments as output, e.g.
2442
// hash = blst.encode_to_g1(b"foo")
2543
%typemap(in, numinputs=0) OBJECT *OUTPUT($1_basetype temp) %{ $1 = &temp; %}
@@ -139,7 +157,7 @@
139157
$2 = _PyLong_NumBits($input);
140158
$1 = ($1_ltype)alloca(nbytes = ($2 + 7)/8);
141159

142-
if (_PyLong_AsByteArray((PyLongObject*)$input, $1, nbytes, 1, 0) < 0)
160+
if (PyLong_AsNativeBytes($input, $1, nbytes, My_PYLONG_FLAGS) < 0)
143161
SWIG_exception_fail(SWIG_OverflowError, "in method '$symname'");
144162
} else {
145163
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', "
@@ -265,8 +283,8 @@
265283
bytes = std::unique_ptr<byte[]>(new byte[_global_npoints*nbytes]);
266284
byte* scalar = bytes.get();
267285
for (size_t i = 0; i < _global_npoints; i++, scalar += nbytes)
268-
_PyLong_AsByteArray((PyLongObject*)PyList_GET_ITEM($input, i),
269-
scalar, nbytes, 1, 0);
286+
PyLong_AsNativeBytes(PyList_GET_ITEM($input, i),
287+
scalar, nbytes, My_PYLONG_FLAGS);
270288

271289
scalars[0] = bytes.get();
272290
scalars[1] = nullptr;

0 commit comments

Comments
 (0)