@@ -185,25 +185,28 @@ def test_long_asint(self):
185185 self .assertRaises (TypeError , PyLong_AsInt , '3' )
186186 self .assertRaises (SystemError , PyLong_AsInt , NULL )
187187
188+ def check_long_asint (self , long_asint , min_val , max_val ):
189+ # round trip (object -> C integer -> object)
190+ for value in (min_val , max_val , - 1 , 0 , 1 , 1234 ):
191+ with self .subTest (value = value ):
192+ self .assertEqual (long_asint (value ), value )
193+
194+ self .assertEqual (long_asint (IntSubclass (42 )), 42 )
195+ self .assertEqual (long_asint (Index (42 )), 42 )
196+ self .assertEqual (long_asint (MyIndexAndInt ()), 10 )
197+
198+ self .assertRaises (OverflowError , long_asint , min_val - 1 )
199+ self .assertRaises (OverflowError , long_asint , max_val + 1 )
200+ self .assertRaises (TypeError , long_asint , 1.0 )
201+ self .assertRaises (TypeError , long_asint , b'2' )
202+ self .assertRaises (TypeError , long_asint , '3' )
203+ self .assertRaises (SystemError , long_asint , NULL )
204+
188205 def test_long_aslong (self ):
189206 # Test PyLong_AsLong() and PyLong_FromLong()
190207 aslong = _testlimitedcapi .pylong_aslong
191208 from _testcapi import LONG_MIN , LONG_MAX
192- # round trip (object -> long -> object)
193- for value in (LONG_MIN , LONG_MAX , - 1 , 0 , 1 , 1234 ):
194- with self .subTest (value = value ):
195- self .assertEqual (aslong (value ), value )
196-
197- self .assertEqual (aslong (IntSubclass (42 )), 42 )
198- self .assertEqual (aslong (Index (42 )), 42 )
199- self .assertEqual (aslong (MyIndexAndInt ()), 10 )
200-
201- self .assertRaises (OverflowError , aslong , LONG_MIN - 1 )
202- self .assertRaises (OverflowError , aslong , LONG_MAX + 1 )
203- self .assertRaises (TypeError , aslong , 1.0 )
204- self .assertRaises (TypeError , aslong , b'2' )
205- self .assertRaises (TypeError , aslong , '3' )
206- self .assertRaises (SystemError , aslong , NULL )
209+ self .check_long_asint (aslong , LONG_MIN , LONG_MAX )
207210
208211 def test_long_aslongandoverflow (self ):
209212 # Test PyLong_AsLongAndOverflow()
@@ -223,25 +226,28 @@ def test_long_aslongandoverflow(self):
223226 # CRASHES aslongandoverflow(1.0)
224227 # CRASHES aslongandoverflow(NULL)
225228
226- def test_long_asunsignedlong (self ):
227- # Test PyLong_AsUnsignedLong() and PyLong_FromUnsignedLong()
228- asunsignedlong = _testlimitedcapi .pylong_asunsignedlong
229- from _testcapi import ULONG_MAX
229+ def check_long_asunsignedint (self , long_asuint , max_val ):
230230 # round trip (object -> unsigned long -> object)
231- for value in (ULONG_MAX , 0 , 1 , 1234 ):
231+ for value in (0 , 1 , 1234 , max_val ):
232232 with self .subTest (value = value ):
233- self .assertEqual (asunsignedlong (value ), value )
233+ self .assertEqual (long_asuint (value ), value )
234+
235+ self .assertEqual (long_asuint (IntSubclass (42 )), 42 )
236+ self .assertRaises (TypeError , long_asuint , Index (42 ))
237+ self .assertRaises (TypeError , long_asuint , MyIndexAndInt ())
234238
235- self .assertEqual (asunsignedlong (IntSubclass (42 )), 42 )
236- self .assertRaises (TypeError , asunsignedlong , Index (42 ))
237- self .assertRaises (TypeError , asunsignedlong , MyIndexAndInt ())
239+ self .assertRaises (OverflowError , long_asuint , - 1 )
240+ self .assertRaises (OverflowError , long_asuint , max_val + 1 )
241+ self .assertRaises (TypeError , long_asuint , 1.0 )
242+ self .assertRaises (TypeError , long_asuint , b'2' )
243+ self .assertRaises (TypeError , long_asuint , '3' )
244+ self .assertRaises (SystemError , long_asuint , NULL )
238245
239- self .assertRaises (OverflowError , asunsignedlong , - 1 )
240- self .assertRaises (OverflowError , asunsignedlong , ULONG_MAX + 1 )
241- self .assertRaises (TypeError , asunsignedlong , 1.0 )
242- self .assertRaises (TypeError , asunsignedlong , b'2' )
243- self .assertRaises (TypeError , asunsignedlong , '3' )
244- self .assertRaises (SystemError , asunsignedlong , NULL )
246+ def test_long_asunsignedlong (self ):
247+ # Test PyLong_AsUnsignedLong() and PyLong_FromUnsignedLong()
248+ asunsignedlong = _testlimitedcapi .pylong_asunsignedlong
249+ from _testcapi import ULONG_MAX
250+ self .check_long_asunsignedint (asunsignedlong , ULONG_MAX )
245251
246252 def test_long_asunsignedlongmask (self ):
247253 # Test PyLong_AsUnsignedLongMask()
@@ -737,6 +743,29 @@ def test_long_getsign(self):
737743
738744 # CRASHES getsign(NULL)
739745
746+ def test_long_asint32 (self ):
747+ # Test PyLong_ToInt32() and PyLong_FromInt32()
748+ to_int32 = _testlimitedcapi .pylong_toint32
749+ from _testcapi import INT32_MIN , INT32_MAX
750+ self .check_long_asint (to_int32 , INT32_MIN , INT32_MAX )
751+
752+ def test_long_asuint32 (self ):
753+ # Test PyLong_ToUInt32() and PyLong_FromUInt32()
754+ to_uint32 = _testlimitedcapi .pylong_touint32
755+ from _testcapi import UINT32_MAX
756+ self .check_long_asunsignedint (to_uint32 , UINT32_MAX )
757+
758+ def test_long_asint64 (self ):
759+ # Test PyLong_ToInt64() and PyLong_FromInt64()
760+ to_int64 = _testlimitedcapi .pylong_toint64
761+ from _testcapi import INT64_MIN , INT64_MAX
762+ self .check_long_asint (to_int64 , INT64_MIN , INT64_MAX )
763+
764+ def test_long_asuint64 (self ):
765+ # Test PyLong_ToUInt64() and PyLong_FromUInt64()
766+ to_uint64 = _testlimitedcapi .pylong_touint64
767+ from _testcapi import UINT64_MAX
768+ self .check_long_asunsignedint (to_uint64 , UINT64_MAX )
740769
741770if __name__ == "__main__" :
742771 unittest .main ()
0 commit comments