- patch #1600346 submitted by Tomer Filiba

- Renamed nb_nonzero slots to nb_bool
- Renamed __nonzero__ methods to __bool__
- update core, lib, docs, and tests to match
This commit is contained in:
Jack Diederich 2006-11-28 19:15:13 +00:00
parent dfc9d4f7aa
commit 4dafcc4ece
31 changed files with 118 additions and 82 deletions

View file

@ -335,24 +335,51 @@ class BoolTest(unittest.TestCase):
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
# from __bool__(). 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):
def __bool__(self):
return self
check(Foo())
class Bar(object):
def __nonzero__(self):
def __bool__(self):
return "Yes"
check(Bar())
class Baz(int):
def __nonzero__(self):
def __bool__(self):
return self
check(Baz())
# __bool__() must return a bool not an int
class Spam(int):
def __bool__(self):
return 1
check(Spam())
class Eggs:
def __len__(self):
return -1
self.assertRaises(ValueError, bool, Eggs())
def test_sane_len(self):
# this test just tests our assumptions about __len__
# this will start failing if __len__ changes assertions
for badval in ['illegal', -1, 1 << 32]:
class A:
def __len__(self):
return badval
try:
bool(A())
except (Exception), e_bool:
pass
try:
len(A())
except (Exception), e_len:
pass
self.assertEqual(str(e_bool), str(e_len))
def test_main():
test_support.run_unittest(BoolTest)