Skip to content

fix: raise OverflowError in py::int_ integer conversion - #6257

Open
henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:fix/int-cast-overflow
Open

henryiii wants to merge 2 commits into
pybind:masterfrom
henryiii:fix/int-cast-overflow

Conversation

@henryiii

@henryiii henryiii commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

Fixes #2786. Supersedes the stale draft, close #2802.

int_::operator T() did not check for errors from PyLong_AsLong and related calls. On overflow it returned -1 with the Python error still set, which later gave a SystemError. For types narrower than long (for example int on LP64), out-of-range values were silently truncated.

The conversion now throws error_already_set when the read fails, and raises OverflowError when the value does not fit in T. Implicit conversion to bool uses truthiness (PyObject_IsTrue), so it has no range limit.

Suggested changelog entry:

  • py::int_ conversion to C++ integer types now raises OverflowError for out-of-range values instead of truncating or leaving a Python error set.

📚 Documentation preview 📚: https://pybind11--6257.org.readthedocs.build/

int_::operator T() ignored errors from PyLong_As* and truncated values
that did not fit in types narrower than long. It now throws
error_already_set on failure and raises OverflowError when the value is
out of range for T.

Fixes pybind#2786.

Assisted-by: ClaudeCode:claude-opus-5-5
@rwgk

rwgk commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

Clarify or implement the advertised bool conversion. The new comment and PR description say that conversion to bool uses truthiness. It skips the final range check after unsigned extraction, which already fails on negative values or values beyond unsigned long. A binding with bool result = value; for a py::int_ raised OverflowError for both -1 and 2**100 on the PR head, on with-GIL and free-threaded CPython 3.15.2. The parent left a Python error set for those inputs, and the PR now throws it directly. If numeric truthiness is the intended contract, handle bool before unsigned extraction and cover zero, negative, and large nonzero integers. Otherwise, describe the range restriction explicitly. Integer extraction and bool branch

Ralf has yet to review these findings.

The bool conversion read the value as unsigned long first, so negative
and very large integers raised OverflowError.

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii

Copy link
Copy Markdown
Collaborator Author

🤖 AI text below 🤖

Confirmed. bool result = value; read the value with PyLong_AsUnsignedLong first, so -1 and 2**100 raised OverflowError.

Fixed in 167ce1d: the bool conversion now uses PyObject_IsTrue before any integer extraction. A new test, test_int_conversion_bool, covers 0, 1, -1, 2**64 - 1, 2**100, and -(2**100). It failed for the negative and large values before the fix. The PR description now states this.

One related item, not changed here: static_cast<bool>(value) and if (value) do not use this conversion. They select the non-template handle::operator bool, which is a null-pointer check, so py::int_(0) gives true. This behavior is the same on master. The test uses copy-initialization for that reason.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Missing error checking for overflowing integer casts

2 participants