mirror of
https://github.com/python/cpython
synced 2026-09-28 10:08:18 +03:00
- 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:
parent
dfc9d4f7aa
commit
4dafcc4ece
31 changed files with 118 additions and 82 deletions
|
|
@ -633,14 +633,14 @@ class Decimal(object):
|
|||
return other
|
||||
return 0
|
||||
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
"""Is the number non-zero?
|
||||
|
||||
0 if self == 0
|
||||
1 if self != 0
|
||||
"""
|
||||
if self._is_special:
|
||||
return 1
|
||||
return True
|
||||
return sum(self._int) != 0
|
||||
|
||||
def __cmp__(self, other, context=None):
|
||||
|
|
@ -759,7 +759,7 @@ class Decimal(object):
|
|||
i = int(self)
|
||||
if self == Decimal(i):
|
||||
return hash(i)
|
||||
assert self.__nonzero__() # '-0' handled by integer case
|
||||
assert self.__bool__() # '-0' handled by integer case
|
||||
return hash(str(self.normalize()))
|
||||
|
||||
def as_tuple(self):
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ if have_unicode:
|
|||
]
|
||||
|
||||
class TestFailingBool:
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
raise RuntimeError
|
||||
|
||||
class TestFailingIter:
|
||||
|
|
|
|||
|
|
@ -990,7 +990,7 @@ class DecimalUsabilityTest(unittest.TestCase):
|
|||
checkSameDec("__mod__", True)
|
||||
checkSameDec("__mul__", True)
|
||||
checkSameDec("__neg__")
|
||||
checkSameDec("__nonzero__")
|
||||
checkSameDec("__bool__")
|
||||
checkSameDec("__pos__")
|
||||
checkSameDec("__pow__", True)
|
||||
checkSameDec("__radd__", True)
|
||||
|
|
|
|||
|
|
@ -417,8 +417,8 @@ def ints():
|
|||
if verbose: print "Testing int operations..."
|
||||
numops(100, 3)
|
||||
# The following crashes in Python 2.2
|
||||
vereq((1).__nonzero__(), 1)
|
||||
vereq((0).__nonzero__(), 0)
|
||||
vereq((1).__bool__(), True)
|
||||
vereq((0).__bool__(), False)
|
||||
# This returns 'NotImplemented' in Python 2.2
|
||||
class C(int):
|
||||
def __add__(self, other):
|
||||
|
|
@ -1682,7 +1682,7 @@ def specials():
|
|||
class Proxy(object):
|
||||
def __init__(self, x):
|
||||
self.x = x
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
return not not self.x
|
||||
def __hash__(self):
|
||||
return hash(self.x)
|
||||
|
|
@ -1722,7 +1722,7 @@ def specials():
|
|||
class DProxy(object):
|
||||
def __init__(self, x):
|
||||
self.x = x
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
return not not self.x
|
||||
def __hash__(self):
|
||||
return hash(self.x)
|
||||
|
|
|
|||
|
|
@ -327,10 +327,10 @@ class TestCase(unittest.TestCase):
|
|||
class Boolean:
|
||||
def __init__(self, truth):
|
||||
self.truth = truth
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
return self.truth
|
||||
bTrue = Boolean(1)
|
||||
bFalse = Boolean(0)
|
||||
bTrue = Boolean(True)
|
||||
bFalse = Boolean(False)
|
||||
|
||||
class Seq:
|
||||
def __init__(self, *args):
|
||||
|
|
|
|||
|
|
@ -319,7 +319,7 @@ class OperatorTestCase(unittest.TestCase):
|
|||
|
||||
def test_truth(self):
|
||||
class C(object):
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
raise SyntaxError
|
||||
self.failUnlessRaises(TypeError, operator.truth)
|
||||
self.failUnlessRaises(SyntaxError, operator.truth, C())
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ class Vector:
|
|||
def __hash__(self):
|
||||
raise TypeError, "Vectors cannot be hashed"
|
||||
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
raise TypeError, "Vectors cannot be used in Boolean contexts"
|
||||
|
||||
def __cmp__(self, other):
|
||||
|
|
@ -133,7 +133,7 @@ class VectorTest(unittest.TestCase):
|
|||
|
||||
for ops in opmap.itervalues():
|
||||
for op in ops:
|
||||
# calls __nonzero__, which should fail
|
||||
# calls __bool__, which should fail
|
||||
self.assertRaises(TypeError, bool, op(a, b))
|
||||
|
||||
class NumberTest(unittest.TestCase):
|
||||
|
|
@ -208,13 +208,13 @@ class MiscTest(unittest.TestCase):
|
|||
self.assertRaises(RuntimeError, cmp, a, b)
|
||||
|
||||
def test_not(self):
|
||||
# Check that exceptions in __nonzero__ are properly
|
||||
# Check that exceptions in __bool__ are properly
|
||||
# propagated by the not operator
|
||||
import operator
|
||||
class Exc(Exception):
|
||||
pass
|
||||
class Bad:
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
raise Exc
|
||||
|
||||
def do(bad):
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class Node(xml.dom.Node):
|
|||
|
||||
prefix = EMPTY_PREFIX # non-null only for NS elements and attributes
|
||||
|
||||
def __nonzero__(self):
|
||||
def __bool__(self):
|
||||
return True
|
||||
|
||||
def toxml(self, encoding = None):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue