@@ -42,6 +42,12 @@ def test_float(self):
4242 self .assertEqual (float (True ), 1.0 )
4343 self .assertIsNot (float (True ), True )
4444
45+ def test_complex (self ):
46+ self .assertEqual (complex (False ), 0j )
47+ self .assertEqual (complex (False ), False )
48+ self .assertEqual (complex (True ), 1 + 0j )
49+ self .assertEqual (complex (True ), True )
50+
4551 def test_math (self ):
4652 self .assertEqual (+ False , 0 )
4753 self .assertIsNot (+ False , False )
@@ -54,8 +60,22 @@ def test_math(self):
5460 self .assertEqual (- True , - 1 )
5561 self .assertEqual (abs (True ), 1 )
5662 self .assertIsNot (abs (True ), True )
57- self .assertEqual (~ False , - 1 )
58- self .assertEqual (~ True , - 2 )
63+ with self .assertWarns (DeprecationWarning ):
64+ # We need to put the bool in a variable, because the constant
65+ # ~False is evaluated at compile time due to constant folding;
66+ # consequently the DeprecationWarning would be issued during
67+ # module loading and not during test execution.
68+ false = False
69+ self .assertEqual (~ false , - 1 )
70+ with self .assertWarns (DeprecationWarning ):
71+ # also check that the warning is issued in case of constant
72+ # folding at compile time
73+ self .assertEqual (eval ("~False" ), - 1 )
74+ with self .assertWarns (DeprecationWarning ):
75+ true = True
76+ self .assertEqual (~ true , - 2 )
77+ with self .assertWarns (DeprecationWarning ):
78+ self .assertEqual (eval ("~True" ), - 2 )
5979
6080 self .assertEqual (False + 2 , 2 )
6181 self .assertEqual (True + 2 , 3 )
@@ -315,6 +335,26 @@ def __len__(self):
315335 return - 1
316336 self .assertRaises (ValueError , bool , Eggs ())
317337
338+ def test_interpreter_convert_to_bool_raises (self ):
339+ class SymbolicBool :
340+ def __bool__ (self ):
341+ raise TypeError
342+
343+ class Symbol :
344+ def __gt__ (self , other ):
345+ return SymbolicBool ()
346+
347+ x = Symbol ()
348+
349+ with self .assertRaises (TypeError ):
350+ if x > 0 :
351+ msg = "x > 0 was true"
352+ else :
353+ msg = "x > 0 was false"
354+
355+ # This used to create negative refcounts, see gh-102250
356+ del x
357+
318358 def test_from_bytes (self ):
319359 self .assertIs (bool .from_bytes (b'\x00 ' * 8 , 'big' ), False )
320360 self .assertIs (bool .from_bytes (b'abcd' , 'little' ), True )
0 commit comments