Skip to content

Commit 88bf26f

Browse files
author
jhylton
committed
Add tests for __nonzero__() problems.
git-svn-id: http://svn.python.org/projects/python/trunk@33275 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 6819fa8 commit 88bf26f

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

Lib/test/test_bool.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,27 @@ def test_picklevalues(self):
320320
self.assertEqual(cPickle.dumps(True, True), "I01\n.")
321321
self.assertEqual(cPickle.dumps(False, True), "I00\n.")
322322

323+
def test_convert_to_bool(self):
324+
# Verify that TypeError occurs when bad things are returned
325+
# from __nonzero__(). This isn't really a bool test, but
326+
# it's related.
327+
check = lambda o: self.assertRaises(TypeError, bool, o)
328+
class Foo(object):
329+
def __nonzero__(self):
330+
return self
331+
check(Foo())
332+
333+
class Bar(object):
334+
def __nonzero__(self):
335+
return "Yes"
336+
check(Bar())
337+
338+
class Baz(int):
339+
def __nonzero__(self):
340+
return self
341+
check(Baz())
342+
343+
323344
def test_main():
324345
test_support.run_unittest(BoolTest)
325346

0 commit comments

Comments
 (0)