Skip to content
Prev Previous commit
Next Next commit
Remove check_long_asunsignedint() in tests
Reuse check_long_asint().
  • Loading branch information
vstinner committed Aug 28, 2024
commit 71748a7cdf29cf693a8d2f09041bc2d8921c7988
32 changes: 4 additions & 28 deletions Lib/test/test_capi/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,30 +232,6 @@ def test_long_aslongandoverflow(self):
from _testcapi import LONG_MIN, LONG_MAX
self.check_long_asintandoverflow(aslongandoverflow, LONG_MIN, LONG_MAX)

def check_long_asunsignedint(self, long_asuint, max_val,
use_index=False, negative_value_error=False):
# round trip (object -> unsigned long -> object)
for value in (0, 1, 1234, max_val):
with self.subTest(value=value):
self.assertEqual(long_asuint(value), value)
if use_index:
self.assertEqual(long_asuint(Index(value)), value)

self.assertEqual(long_asuint(IntSubclass(42)), 42)
if not use_index:
self.assertRaises(TypeError, long_asuint, Index(42))
self.assertRaises(TypeError, long_asuint, MyIndexAndInt())

if negative_value_error:
self.assertRaises(ValueError, long_asuint, -1)
else:
self.assertRaises(OverflowError, long_asuint, -1)
self.assertRaises(OverflowError, long_asuint, max_val + 1)
self.assertRaises(TypeError, long_asuint, 1.0)
self.assertRaises(TypeError, long_asuint, b'2')
self.assertRaises(TypeError, long_asuint, '3')
self.assertRaises(SystemError, long_asuint, NULL)

def test_long_asunsignedlong(self):
# Test PyLong_AsUnsignedLong() and PyLong_FromUnsignedLong()
asunsignedlong = _testlimitedcapi.pylong_asunsignedlong
Expand Down Expand Up @@ -671,15 +647,15 @@ 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, use_index=True,
negative_value_error=True)
self.check_long_asint(as_uint32, 0, UINT32_MAX,
negative_value_error=ValueError)

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, use_index=True,
negative_value_error=True)
self.check_long_asint(as_uint64, 0, UINT64_MAX,
negative_value_error=ValueError)

if __name__ == "__main__":
unittest.main()