Skip to content

Commit 034eada

Browse files
committed
Validate __len__ result in slot_nb_bool fallback
1 parent c52b7a8 commit 034eada

3 files changed

Lines changed: 61 additions & 15 deletions

File tree

‎Lib/test/test_bool.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,44 @@ class C:
387387
__len__ = None
388388
self.assertRaises(TypeError, bool, C())
389389

390+
def test_len_when_bool_raises_attributeerror(self):
391+
# gh-162678
392+
class ErrDescr:
393+
def __get__(self, obj, typ=None):
394+
raise AttributeError
395+
396+
def make(value):
397+
class X:
398+
__bool__ = ErrDescr()
399+
def __len__(self):
400+
return value
401+
return X()
402+
403+
self.assertRaises(TypeError, bool, make('illegal'))
404+
self.assertRaises(TypeError, bool, make(0.1))
405+
self.assertRaises(ValueError, bool, make(-1))
406+
self.assertRaises(OverflowError, bool, make(1 << 64))
407+
408+
self.assertIs(bool(make(0)), False)
409+
self.assertIs(bool(make(1)), True)
410+
411+
class A:
412+
def __bool__(self):
413+
return True
414+
415+
def __index__(self):
416+
return 0
417+
418+
class B:
419+
def __bool__(self):
420+
return False
421+
422+
def __index__(self):
423+
return 1
424+
425+
self.assertIs(bool(make(A())), False)
426+
self.assertIs(bool(make(B())), True)
427+
390428
def test_real_and_imag(self):
391429
self.assertEqual(True.real, 1)
392430
self.assertEqual(True.imag, 0)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix truth testing using the truth value of the :meth:`~object.__len__`
2+
result without checking that it is a non-negative integer, when
3+
:meth:`~object.__bool__` is defined but accessing it raises
4+
:exc:`AttributeError`.

‎Objects/typeobject.c‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10734,15 +10734,10 @@ FUNCNAME(PyObject *self, PyObject *other) \
1073410734
SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, DUNDER, RDUNDER)
1073510735

1073610736
static Py_ssize_t
10737-
slot_sq_length(PyObject *self)
10737+
len_result_as_ssize_t(PyObject *res)
1073810738
{
10739-
PyObject* stack[1] = {self};
10740-
PyObject *res = vectorcall_method(&_Py_ID(__len__), stack, 1);
1074110739
Py_ssize_t len;
1074210740

10743-
if (res == NULL)
10744-
return -1;
10745-
1074610741
Py_SETREF(res, _PyNumber_Index(res));
1074710742
if (res == NULL)
1074810743
return -1;
@@ -10761,6 +10756,19 @@ slot_sq_length(PyObject *self)
1076110756
return len;
1076210757
}
1076310758

10759+
static Py_ssize_t
10760+
slot_sq_length(PyObject *self)
10761+
{
10762+
PyObject* stack[1] = {self};
10763+
PyObject *res = vectorcall_method(&_Py_ID(__len__), stack, 1);
10764+
10765+
if (res == NULL) {
10766+
return -1;
10767+
}
10768+
10769+
return len_result_as_ssize_t(res);
10770+
}
10771+
1076410772
static PyObject *
1076510773
slot_sq_item(PyObject *self, Py_ssize_t i)
1076610774
{
@@ -10920,7 +10928,6 @@ SLOT0(slot_nb_absolute, __abs__)
1092010928
static int
1092110929
slot_nb_bool(PyObject *self)
1092210930
{
10923-
int using_len = 0;
1092410931
int attr_is_none = 0;
1092510932
PyObject *value = maybe_call_special_no_args(self, &_Py_ID(__bool__),
1092610933
&attr_is_none);
@@ -10932,22 +10939,19 @@ slot_nb_bool(PyObject *self)
1093210939
}
1093310940
else if (value == NULL && !PyErr_Occurred()) {
1093410941
value = _PyObject_MaybeCallSpecialNoArgs(self, &_Py_ID(__len__));
10935-
if (value == NULL && !PyErr_Occurred()) {
10936-
return 1;
10942+
if (value == NULL) {
10943+
return PyErr_Occurred() ? -1 : 1;
1093710944
}
10938-
using_len = 1;
10945+
Py_ssize_t len = len_result_as_ssize_t(value);
10946+
return len < 0 ? -1 : len > 0;
1093910947
}
1094010948

1094110949
if (value == NULL) {
1094210950
return -1;
1094310951
}
1094410952

1094510953
int result;
10946-
if (using_len) {
10947-
/* bool type enforced by slot_nb_len */
10948-
result = PyObject_IsTrue(value);
10949-
}
10950-
else if (PyBool_Check(value)) {
10954+
if (PyBool_Check(value)) {
1095110955
result = PyObject_IsTrue(value);
1095210956
}
1095310957
else {

0 commit comments

Comments
 (0)