Skip to content

fix: do not discard unrelated errors in the arithmetic caster - #6297

Merged
henryiii merged 2 commits into
pybind:masterfrom
henryiii:fix/issue-2862
Nov 29, 2026
Merged

henryiii merged 2 commits into
pybind:masterfrom
henryiii:fix/issue-2862

Conversation

@henryiii

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

Part of #2862. This changes only the arithmetic caster.

The arithmetic type_caster::load called PyErr_Clear() in three places without checking the error. A KeyboardInterrupt or RuntimeError from __index__, __int__, or __float__ was lost: the call used the __int__ result or failed with "incompatible function arguments". A new helper clears only TypeError, ValueError, and OverflowError (the "does not convert" errors) and throws error_already_set for other errors.

Behavior changes:

  • An error that is not a conversion error now stops overload resolution and is raised to the caller.
  • py::cast<int>(obj) raises error_already_set instead of cast_error in that case.

ValueError is still cleared because an existing test expects a ValueError from __index__ to fall back to __int__. PyNumber_Long also raises ValueError for NaN.

Primary-error policy: this change removes paths that discard the primary error and adds none.

Suggested changelog entry:

  • The arithmetic type caster no longer discards errors other than TypeError, ValueError, and OverflowError (for example KeyboardInterrupt) raised during conversion.

The arithmetic type caster cleared every error raised during
conversion. A KeyboardInterrupt or RuntimeError from __index__,
__int__, or __float__ was lost: the call used the __int__ result or
failed with "incompatible function arguments". Clear only TypeError,
ValueError, and OverflowError, which mean "does not convert", and
propagate other errors.

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii
henryiii marked this pull request as ready for review November 23, 2026 22:34
@rwgk

rwgk commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

No actionable findings. The three error-clearing sites now preserve unrelated Python exceptions, while TypeError, ValueError, and OverflowError still let the caster reject the conversion or try its existing fallback. In a focused probe, the base swallowed a RuntimeError from __index__ and returned the __int__ result. At this head the original exception reached the caller on signed, unsigned, and floating-point paths, and a ValueError still allowed the integer fallback. All 43 built-in caster tests passed on regular and free-threaded CPython 3.14.10. Arithmetic caster

@rwgk rwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I pushed commit 9217520, mainly to clarify in the call sites that we're only using a heuristic.

It's definitely much better than before.

@henryiii
henryiii merged commit 6b61f1f into pybind:master Nov 29, 2026
94 checks passed
@henryiii
henryiii deleted the fix/issue-2862 branch November 29, 2026 02:20
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants