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

Skip to content

Commit 66d2be8

Browse files
committed
Issue 12647: Add __bool__() method to the None object.
1 parent a2250e6 commit 66d2be8

3 files changed

Lines changed: 48 additions & 5 deletions

File tree

Lib/test/test_descr.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,9 +2068,6 @@ def getdict(self):
20682068
# Two essentially featureless objects, just inheriting stuff from
20692069
# object.
20702070
self.assertEqual(dir(NotImplemented), dir(Ellipsis))
2071-
if support.check_impl_detail():
2072-
# None differs in PyPy: it has a __nonzero__
2073-
self.assertEqual(dir(None), dir(Ellipsis))
20742071

20752072
# Nasty test case for proxied objects
20762073
class Wrapper(object):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Core and Builtins
1313
- Verify the types of AST strings and identifiers provided by the user before
1414
compiling them.
1515

16+
- Issue #12647: The None object now has a __bool__() method that returns False.
17+
Formerly, bool(None) returned False only because of special case logic
18+
in PyObject_IsTrue().
19+
1620
- Issue #12579: str.format_map() now raises a ValueError if used on a
1721
format string that contains positional fields. Initial patch by
1822
Julian Berman.

Objects/object.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ PyObject_Dir(PyObject *obj)
12551255
}
12561256

12571257
/*
1258-
None is as a non-NULL undefined value.
1258+
None is a non-NULL undefined value.
12591259
There is (and should be!) no way to create other objects of this type,
12601260
so there is exactly one (which is indestructible, by the way).
12611261
*/
@@ -1277,6 +1277,48 @@ none_dealloc(PyObject* ignore)
12771277
Py_FatalError("deallocating None");
12781278
}
12791279

1280+
static int
1281+
none_bool(PyObject *v)
1282+
{
1283+
return 0;
1284+
}
1285+
1286+
static PyNumberMethods none_as_number = {
1287+
0, /* nb_add */
1288+
0, /* nb_subtract */
1289+
0, /* nb_multiply */
1290+
0, /* nb_remainder */
1291+
0, /* nb_divmod */
1292+
0, /* nb_power */
1293+
0, /* nb_negative */
1294+
0, /* nb_positive */
1295+
0, /* nb_absolute */
1296+
(inquiry)none_bool, /* nb_bool */
1297+
0, /* nb_invert */
1298+
0, /* nb_lshift */
1299+
0, /* nb_rshift */
1300+
0, /* nb_and */
1301+
0, /* nb_xor */
1302+
0, /* nb_or */
1303+
0, /* nb_int */
1304+
0, /* nb_reserved */
1305+
0, /* nb_float */
1306+
0, /* nb_inplace_add */
1307+
0, /* nb_inplace_subtract */
1308+
0, /* nb_inplace_multiply */
1309+
0, /* nb_inplace_remainder */
1310+
0, /* nb_inplace_power */
1311+
0, /* nb_inplace_lshift */
1312+
0, /* nb_inplace_rshift */
1313+
0, /* nb_inplace_and */
1314+
0, /* nb_inplace_xor */
1315+
0, /* nb_inplace_or */
1316+
0, /* nb_floor_divide */
1317+
0, /* nb_true_divide */
1318+
0, /* nb_inplace_floor_divide */
1319+
0, /* nb_inplace_true_divide */
1320+
0, /* nb_index */
1321+
};
12801322

12811323
static PyTypeObject PyNone_Type = {
12821324
PyVarObject_HEAD_INIT(&PyType_Type, 0)
@@ -1289,7 +1331,7 @@ static PyTypeObject PyNone_Type = {
12891331
0, /*tp_setattr*/
12901332
0, /*tp_reserved*/
12911333
none_repr, /*tp_repr*/
1292-
0, /*tp_as_number*/
1334+
&none_as_number, /*tp_as_number*/
12931335
0, /*tp_as_sequence*/
12941336
0, /*tp_as_mapping*/
12951337
0, /*tp_hash */

0 commit comments

Comments
 (0)