Update Lib/test/test_bool.py from CPython v3.12.0

This commit is contained in:
NakanoMiku committed 2023-11-23 15:51:02 +08:00
1 parent 5303b33c8b
commit 81208637f5
1 file changed
+42 -2
+42 -2
View File
@@ -42,6 +42,12 @@ class BoolTest(unittest.TestCase):
self.assertEqual(float(True), 1.0)
self.assertIsNot(float(True), True)
def test_complex(self):
self.assertEqual(complex(False), 0j)
self.assertEqual(complex(False), False)
self.assertEqual(complex(True), 1+0j)
self.assertEqual(complex(True), True)
def test_math(self):
self.assertEqual(+False, 0)
self.assertIsNot(+False, False)
@@ -54,8 +60,22 @@ class BoolTest(unittest.TestCase):
self.assertEqual(-True, -1)
self.assertEqual(abs(True), 1)
self.assertIsNot(abs(True), True)
self.assertEqual(~False, -1)
self.assertEqual(~True, -2)
with self.assertWarns(DeprecationWarning):
# We need to put the bool in a variable, because the constant
# ~False is evaluated at compile time due to constant folding;
# consequently the DeprecationWarning would be issued during
# module loading and not during test execution.
false = False
self.assertEqual(~false, -1)
with self.assertWarns(DeprecationWarning):
# also check that the warning is issued in case of constant
# folding at compile time
self.assertEqual(eval("~False"), -1)
with self.assertWarns(DeprecationWarning):
true = True
self.assertEqual(~true, -2)
with self.assertWarns(DeprecationWarning):
self.assertEqual(eval("~True"), -2)
self.assertEqual(False+2, 2)
self.assertEqual(True+2, 3)
@@ -315,6 +335,26 @@ class BoolTest(unittest.TestCase):
return -1
self.assertRaises(ValueError, bool, Eggs())
def test_interpreter_convert_to_bool_raises(self):
class SymbolicBool:
def __bool__(self):
raise TypeError
class Symbol:
def __gt__(self, other):
return SymbolicBool()
x = Symbol()
with self.assertRaises(TypeError):
if x > 0:
msg = "x > 0 was true"
else:
msg = "x > 0 was false"
# This used to create negative refcounts, see gh-102250
del x
def test_from_bytes(self):
self.assertIs(bool.from_bytes(b'\x00'*8, 'big'), False)
self.assertIs(bool.from_bytes(b'abcd', 'little'), True)