Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa86e35

Browse files
committed
- bool() called without arguments now returns False rather than
raising an exception. This is consistent with calling the constructors for the other builtin types -- called without argument they all return the false value of that type. (SF patch #724135) Thanks to Alex Martelli.
1 parent a268540 commit aa86e35

4 files changed

Lines changed: 18 additions & 2 deletions

File tree

Lib/test/test_bool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class C(bool):
137137
veris(bool(0), False)
138138
veris(bool("hello"), True)
139139
veris(bool(""), False)
140+
veris(bool(), False)
140141

141142
veris(hasattr([], "append"), True)
142143
veris(hasattr([], "wobble"), False)

Lib/test/test_types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ class C: pass
8686
if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed, 'mixed comparisons'
8787
if -1 != -1L or -1 != -1.0 or -1L != -1.0:
8888
raise TestFailed, 'int/long/float value not equal'
89+
# calling built-in types without argument must return 0
90+
if int() != 0: raise TestFailed, 'int() does not return 0'
91+
if long() != 0L: raise TestFailed, 'long() does not return 0L'
92+
if float() != 0.0: raise TestFailed, 'float() does not return 0.0'
8993
if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
9094
else: raise TestFailed, 'int() does not round properly'
9195
if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass
@@ -214,6 +218,8 @@ class C: pass
214218

215219

216220
print '6.5.2 Tuples'
221+
# calling built-in types without argument must return empty
222+
if tuple() != (): raise TestFailed,'tuple() does not return ()'
217223
if len(()) != 0: raise TestFailed, 'len(())'
218224
if len((1,)) != 1: raise TestFailed, 'len((1,))'
219225
if len((1,2,3,4,5,6)) != 6: raise TestFailed, 'len((1,2,3,4,5,6))'
@@ -251,6 +257,8 @@ def f():
251257
vereq(list(tuple(f())), range(1000))
252258

253259
print '6.5.3 Lists'
260+
# calling built-in types without argument must return empty
261+
if list() != []: raise TestFailed,'list() does not return []'
254262
if len([]) != 0: raise TestFailed, 'len([])'
255263
if len([1,]) != 1: raise TestFailed, 'len([1,])'
256264
if len([1,2,3,4,5,6]) != 6: raise TestFailed, 'len([1,2,3,4,5,6])'
@@ -441,6 +449,8 @@ def selfmodifyingComparison(x,y):
441449

442450

443451
print '6.6 Mappings == Dictionaries'
452+
# calling built-in types without argument must return empty
453+
if dict() != {}: raise TestFailed,'dict() does not return {}'
444454
d = {}
445455
if d.keys() != []: raise TestFailed, '{}.keys()'
446456
if d.values() != []: raise TestFailed, '{}.values()'

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ What's New in Python 2.3 beta 1?
1212
Core and builtins
1313
-----------------
1414

15+
- bool() called without arguments now returns False rather than
16+
raising an exception. This is consistent with calling the
17+
constructors for the other builtin types -- called without argument
18+
they all return the false value of that type. (SF patch #724135)
19+
1520
- In support of PEP 269 (making the pgen parser generator accessible
1621
from Python), some changes to the pgen code structure were made; a
1722
few files that used to be linked only with pgen are now linked with

Objects/boolobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ static PyObject *
5151
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5252
{
5353
static char *kwlist[] = {"x", 0};
54-
PyObject *x;
54+
PyObject *x = Py_False;
5555
long ok;
5656

57-
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:bool", kwlist, &x))
57+
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
5858
return NULL;
5959
ok = PyObject_IsTrue(x);
6060
if (ok < 0)

0 commit comments

Comments
 (0)