Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Lib/test/test_bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,44 @@ class C:
__len__ = None
self.assertRaises(TypeError, bool, C())

def test_len_when_bool_raises_attributeerror(self):
# gh-162678
class ErrDescr:
def __get__(self, obj, typ=None):
raise AttributeError

def make(value):
class X:
__bool__ = ErrDescr()
def __len__(self):
return value
return X()

self.assertRaises(TypeError, bool, make('illegal'))
self.assertRaises(TypeError, bool, make(0.1))
self.assertRaises(ValueError, bool, make(-1))
self.assertRaises(OverflowError, bool, make(1 << 64))

self.assertIs(bool(make(0)), False)
self.assertIs(bool(make(1)), True)

class A:
def __bool__(self):
return True

def __index__(self):
return 0

class B:
def __bool__(self):
return False

def __index__(self):
return 1

self.assertIs(bool(make(A())), False)
self.assertIs(bool(make(B())), True)

def test_real_and_imag(self):
self.assertEqual(True.real, 1)
self.assertEqual(True.imag, 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix truth testing using the truth value of the :meth:`~object.__len__`
result without checking that it is a non-negative integer, when
:meth:`~object.__bool__` is defined but accessing it raises
:exc:`AttributeError`.
34 changes: 19 additions & 15 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10734,15 +10734,10 @@ FUNCNAME(PyObject *self, PyObject *other) \
SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, DUNDER, RDUNDER)

static Py_ssize_t
slot_sq_length(PyObject *self)
len_result_as_ssize_t(PyObject *res)
{
PyObject* stack[1] = {self};
PyObject *res = vectorcall_method(&_Py_ID(__len__), stack, 1);
Py_ssize_t len;

if (res == NULL)
return -1;

Py_SETREF(res, _PyNumber_Index(res));
if (res == NULL)
return -1;
Expand All @@ -10761,6 +10756,19 @@ slot_sq_length(PyObject *self)
return len;
}

static Py_ssize_t
slot_sq_length(PyObject *self)
{
PyObject* stack[1] = {self};
PyObject *res = vectorcall_method(&_Py_ID(__len__), stack, 1);

if (res == NULL) {
return -1;
}

return len_result_as_ssize_t(res);

@picnixz picnixz Nov 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes an additional call, and may slow down everyone else when they do len(seq). Can you benchmark this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes Python behavior correct. @etyu39 Could you measure the benchmark based on pyperf?

}

static PyObject *
slot_sq_item(PyObject *self, Py_ssize_t i)
{
Expand Down Expand Up @@ -10920,7 +10928,6 @@ SLOT0(slot_nb_absolute, __abs__)
static int
slot_nb_bool(PyObject *self)
{
int using_len = 0;
int attr_is_none = 0;
PyObject *value = maybe_call_special_no_args(self, &_Py_ID(__bool__),
&attr_is_none);
Expand All @@ -10932,22 +10939,19 @@ slot_nb_bool(PyObject *self)
}
else if (value == NULL && !PyErr_Occurred()) {
value = _PyObject_MaybeCallSpecialNoArgs(self, &_Py_ID(__len__));
if (value == NULL && !PyErr_Occurred()) {
return 1;
if (value == NULL) {
return PyErr_Occurred() ? -1 : 1;
}
using_len = 1;
Py_ssize_t len = len_result_as_ssize_t(value);
return len < 0 ? -1 : len > 0;
}

if (value == NULL) {
return -1;
}

int result;
if (using_len) {
/* bool type enforced by slot_nb_len */
result = PyObject_IsTrue(value);
}
else if (PyBool_Check(value)) {
if (PyBool_Check(value)) {
result = PyObject_IsTrue(value);
}
else {
Expand Down
Loading