Skip to content
Prev Previous commit
Next Next commit
Use Py_ASNATIVEBYTES_ALLOW_INDEX
  • Loading branch information
vstinner committed Aug 27, 2024
commit 830d250818ea4a7cc2d337d75b46891c7a484af6
8 changes: 4 additions & 4 deletions Lib/test/test_capi/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,26 +659,26 @@ def test_long_asint32(self):
# Test PyLong_AsInt32() and PyLong_FromInt32()
to_int32 = _testlimitedcapi.pylong_asint32
from _testcapi import INT32_MIN, INT32_MAX
self.check_long_asint(to_int32, INT32_MIN, INT32_MAX, use_index=False)
self.check_long_asint(to_int32, INT32_MIN, INT32_MAX)

def test_long_asint64(self):
# Test PyLong_AsInt64() and PyLong_FromInt64()
as_int64 = _testlimitedcapi.pylong_asint64
from _testcapi import INT64_MIN, INT64_MAX
self.check_long_asint(as_int64, INT64_MIN, INT64_MAX, use_index=False)
self.check_long_asint(as_int64, INT64_MIN, INT64_MAX)

def test_long_asuint32(self):
# Test PyLong_AsUInt32() and PyLong_FromUInt32()
as_uint32 = _testlimitedcapi.pylong_asuint32
from _testcapi import UINT32_MAX
self.check_long_asunsignedint(as_uint32, UINT32_MAX,
self.check_long_asunsignedint(as_uint32, UINT32_MAX, use_index=True,
negative_value_error=True)

def test_long_asuint64(self):
# Test PyLong_AsUInt64() and PyLong_FromUInt64()
as_uint64 = _testlimitedcapi.pylong_asuint64
from _testcapi import UINT64_MAX
self.check_long_asunsignedint(as_uint64, UINT64_MAX,
self.check_long_asunsignedint(as_uint64, UINT64_MAX, use_index=True,
negative_value_error=True)

if __name__ == "__main__":
Expand Down
6 changes: 4 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -6743,7 +6743,8 @@ PyObject* PyLong_FromUInt64(uint64_t value)

#define LONG_TO_INT(obj, value, type_name) \
do { \
int flags = Py_ASNATIVEBYTES_NATIVE_ENDIAN; \
int flags = (Py_ASNATIVEBYTES_NATIVE_ENDIAN \
| Py_ASNATIVEBYTES_ALLOW_INDEX); \
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, value, sizeof(*value), flags); \
if (bytes < 0) { \
return -1; \
Expand All @@ -6770,7 +6771,8 @@ int PyLong_AsInt64(PyObject *obj, int64_t *value)
do { \
int flags = (Py_ASNATIVEBYTES_NATIVE_ENDIAN \
| Py_ASNATIVEBYTES_UNSIGNED_BUFFER \
| Py_ASNATIVEBYTES_REJECT_NEGATIVE); \
| Py_ASNATIVEBYTES_REJECT_NEGATIVE \
| Py_ASNATIVEBYTES_ALLOW_INDEX); \
Py_ssize_t bytes = PyLong_AsNativeBytes(obj, value, sizeof(*value), flags); \
Comment thread
vstinner marked this conversation as resolved.
if (bytes < 0) { \
return -1; \
Expand Down