From: Jeremy Hylton Date: Fri, 27 Jun 2003 17:40:16 +0000 (+0000) Subject: Add tests for __nonzero__() problems. X-Git-Tag: v2.3c1~309 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7ff55e6bc548495ea8045194325c0226bf725359;p=python Add tests for __nonzero__() problems. --- diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py index dd18d845f5..f2a4322695 100644 --- a/Lib/test/test_bool.py +++ b/Lib/test/test_bool.py @@ -320,6 +320,27 @@ class BoolTest(unittest.TestCase): self.assertEqual(cPickle.dumps(True, True), "I01\n.") self.assertEqual(cPickle.dumps(False, True), "I00\n.") + def test_convert_to_bool(self): + # Verify that TypeError occurs when bad things are returned + # from __nonzero__(). This isn't really a bool test, but + # it's related. + check = lambda o: self.assertRaises(TypeError, bool, o) + class Foo(object): + def __nonzero__(self): + return self + check(Foo()) + + class Bar(object): + def __nonzero__(self): + return "Yes" + check(Bar()) + + class Baz(int): + def __nonzero__(self): + return self + check(Baz()) + + def test_main(): test_support.run_unittest(BoolTest)