Skip to content

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

Open
henryiii wants to merge 1 commit into
pybind:masterfrom
henryiii:fix/int-cast-overflow
Open

henryiii wants to merge 1 commit 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. bool keeps truthiness semantics (no range check).

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.

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

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

1 participant