@@ -235,6 +235,10 @@ def __init__(self, value):
235235 def __index__ (self ):
236236 return self .value
237237
238+ class BadDescr :
239+ def __get__ (self , obj , objtype = None ):
240+ raise ValueError
241+
238242class MathTests (unittest .TestCase ):
239243
240244 def ftest (self , name , got , expected , ulp_tol = 5 , abs_tol = 0.0 ):
@@ -324,6 +328,7 @@ def testAtan2(self):
324328 self .ftest ('atan2(0, 1)' , math .atan2 (0 , 1 ), 0 )
325329 self .ftest ('atan2(1, 1)' , math .atan2 (1 , 1 ), math .pi / 4 )
326330 self .ftest ('atan2(1, 0)' , math .atan2 (1 , 0 ), math .pi / 2 )
331+ self .ftest ('atan2(1, -1)' , math .atan2 (1 , - 1 ), 3 * math .pi / 4 )
327332
328333 # math.atan2(0, x)
329334 self .ftest ('atan2(0., -inf)' , math .atan2 (0. , NINF ), math .pi )
@@ -417,16 +422,22 @@ def __ceil__(self):
417422 return 42
418423 class TestNoCeil :
419424 pass
425+ class TestBadCeil :
426+ __ceil__ = BadDescr ()
420427 self .assertEqual (math .ceil (TestCeil ()), 42 )
421428 self .assertEqual (math .ceil (FloatCeil ()), 42 )
422429 self .assertEqual (math .ceil (FloatLike (42.5 )), 43 )
423430 self .assertRaises (TypeError , math .ceil , TestNoCeil ())
431+ self .assertRaises (ValueError , math .ceil , TestBadCeil ())
424432
425433 t = TestNoCeil ()
426434 t .__ceil__ = lambda * args : args
427435 self .assertRaises (TypeError , math .ceil , t )
428436 self .assertRaises (TypeError , math .ceil , t , 0 )
429437
438+ self .assertEqual (math .ceil (FloatLike (+ 1.0 )), + 1.0 )
439+ self .assertEqual (math .ceil (FloatLike (- 1.0 )), - 1.0 )
440+
430441 @requires_IEEE_754
431442 def testCopysign (self ):
432443 self .assertEqual (math .copysign (1 , 42 ), 1.0 )
@@ -567,16 +578,22 @@ def __floor__(self):
567578 return 42
568579 class TestNoFloor :
569580 pass
581+ class TestBadFloor :
582+ __floor__ = BadDescr ()
570583 self .assertEqual (math .floor (TestFloor ()), 42 )
571584 self .assertEqual (math .floor (FloatFloor ()), 42 )
572585 self .assertEqual (math .floor (FloatLike (41.9 )), 41 )
573586 self .assertRaises (TypeError , math .floor , TestNoFloor ())
587+ self .assertRaises (ValueError , math .floor , TestBadFloor ())
574588
575589 t = TestNoFloor ()
576590 t .__floor__ = lambda * args : args
577591 self .assertRaises (TypeError , math .floor , t )
578592 self .assertRaises (TypeError , math .floor , t , 0 )
579593
594+ self .assertEqual (math .floor (FloatLike (+ 1.0 )), + 1.0 )
595+ self .assertEqual (math .floor (FloatLike (- 1.0 )), - 1.0 )
596+
580597 def testFmod (self ):
581598 self .assertRaises (TypeError , math .fmod )
582599 self .ftest ('fmod(10, 1)' , math .fmod (10 , 1 ), 0.0 )
@@ -598,6 +615,7 @@ def testFmod(self):
598615 self .assertEqual (math .fmod (- 3.0 , NINF ), - 3.0 )
599616 self .assertEqual (math .fmod (0.0 , 3.0 ), 0.0 )
600617 self .assertEqual (math .fmod (0.0 , NINF ), 0.0 )
618+ self .assertRaises (ValueError , math .fmod , INF , INF )
601619
602620 def testFrexp (self ):
603621 self .assertRaises (TypeError , math .frexp )
@@ -714,6 +732,11 @@ def msum(iterable):
714732 s = msum (vals )
715733 self .assertEqual (msum (vals ), math .fsum (vals ))
716734
735+ self .assertEqual (math .fsum ([1.0 , math .inf ]), math .inf )
736+ self .assertRaises (OverflowError , math .fsum , [1e+308 , 1e+308 ])
737+ self .assertRaises (ValueError , math .fsum , [math .inf , - math .inf ])
738+ self .assertRaises (TypeError , math .fsum , ['spam' ])
739+
717740 def testGcd (self ):
718741 gcd = math .gcd
719742 self .assertEqual (gcd (0 , 0 ), 0 )
@@ -831,6 +854,8 @@ def testHypot(self):
831854 scale = FLOAT_MIN / 2.0 ** exp
832855 self .assertEqual (math .hypot (4 * scale , 3 * scale ), 5 * scale )
833856
857+ self .assertRaises (TypeError , math .hypot , * ([1.0 ]* 18 ), 'spam' )
858+
834859 @requires_IEEE_754
835860 @unittest .skipIf (HAVE_DOUBLE_ROUNDING ,
836861 "hypot() loses accuracy on machines with double rounding" )
@@ -966,13 +991,19 @@ class T(tuple):
966991 dist ((1 , 2 , 3 , 4 ), (5 , 6 , 7 ))
967992 with self .assertRaises (ValueError ): # Check dimension agree
968993 dist ((1 , 2 , 3 ), (4 , 5 , 6 , 7 ))
994+ with self .assertRaises (TypeError ):
995+ dist ((1 ,)* 17 + ("spam" ,), (1 ,)* 18 )
969996 with self .assertRaises (TypeError ): # Rejects invalid types
970997 dist ("abc" , "xyz" )
971998 int_too_big_for_float = 10 ** (sys .float_info .max_10_exp + 5 )
972999 with self .assertRaises ((ValueError , OverflowError )):
9731000 dist ((1 , int_too_big_for_float ), (2 , 3 ))
9741001 with self .assertRaises ((ValueError , OverflowError )):
9751002 dist ((2 , 3 ), (1 , int_too_big_for_float ))
1003+ with self .assertRaises (TypeError ):
1004+ dist ((1 ,), 2 )
1005+ with self .assertRaises (TypeError ):
1006+ dist ([1 ], 2 )
9761007
9771008 # Verify that the one dimensional case is equivalent to abs()
9781009 for i in range (20 ):
@@ -1111,6 +1142,7 @@ def test_lcm(self):
11111142
11121143 def testLdexp (self ):
11131144 self .assertRaises (TypeError , math .ldexp )
1145+ self .assertRaises (TypeError , math .ldexp , 2.0 , 1.1 )
11141146 self .ftest ('ldexp(0,1)' , math .ldexp (0 ,1 ), 0 )
11151147 self .ftest ('ldexp(1,1)' , math .ldexp (1 ,1 ), 2 )
11161148 self .ftest ('ldexp(1,-1)' , math .ldexp (1 ,- 1 ), 0.5 )
@@ -1153,6 +1185,7 @@ def testLog(self):
11531185 2302.5850929940457 )
11541186 self .assertRaises (ValueError , math .log , - 1.5 )
11551187 self .assertRaises (ValueError , math .log , - 10 ** 1000 )
1188+ self .assertRaises (ValueError , math .log , 10 , - 10 )
11561189 self .assertRaises (ValueError , math .log , NINF )
11571190 self .assertEqual (math .log (INF ), INF )
11581191 self .assertTrue (math .isnan (math .log (NAN )))
@@ -2378,6 +2411,14 @@ def __float__(self):
23782411 # argument to a float.
23792412 self .assertFalse (getattr (y , "converted" , False ))
23802413
2414+ def test_input_exceptions (self ):
2415+ self .assertRaises (TypeError , math .exp , "spam" )
2416+ self .assertRaises (TypeError , math .erf , "spam" )
2417+ self .assertRaises (TypeError , math .atan2 , "spam" , 1.0 )
2418+ self .assertRaises (TypeError , math .atan2 , 1.0 , "spam" )
2419+ self .assertRaises (TypeError , math .atan2 , 1.0 )
2420+ self .assertRaises (TypeError , math .atan2 , 1.0 , 2.0 , 3.0 )
2421+
23812422 # Custom assertions.
23822423
23832424 def assertIsNaN (self , value ):
0 commit comments