Skip to content

gh-162678: Validate __len__ result in slot_nb_bool fallback - #162865

Open
etyu39 wants to merge 1 commit into
python:mainfrom
etyu39:gh-162678-fix
Open

etyu39 wants to merge 1 commit into
python:mainfrom
etyu39:gh-162678-fix

Conversation

@etyu39

@etyu39 etyu39 commented Nov 30, 2026 •

Copy link
Copy Markdown

Make the __len__ fallback in slot_nb_bool validate the result of __len__ and check whether it is nonzero, instead of using its truth value.

This fixes a regression introduced in GH-131588.

cc @corona10

@etyu39
etyu39 requested a review from markshannon as a code owner November 30, 2026 11:09
@python-cla-bot

python-cla-bot Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Comment thread Objects/typeobject.c
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants