Skip to content

fix: throw from py::cast instead of returning null with an error set - #6245

Open
kroplabeskidu wants to merge 1 commit into
pybind:masterfrom
kroplabeskidu:fix-cast-null-result
Open

kroplabeskidu wants to merge 1 commit into
pybind:masterfrom
kroplabeskidu:fix-cast-null-result

Conversation

@kroplabeskidu

Copy link
Copy Markdown

Description

Item 9 from #6159, related to #4099. py::cast of an unregistered type returns a null object with a TypeError pending, although the docs say a failed cast throws. If the caller doesn't check, the error shows up later:

SystemError: <built-in method ...> returned a result with an exception set

py::cast now throws error_already_set when the caster returns null with an error set. A null result without an error stays as it was, so only the broken case changes. The caster behaviour used for function return values isn't affected, because that path doesn't go through py::cast.

This covers the missing exception reported in #4099. It doesn't address the ODR violation that caused the failed cast in that report, so I'm not closing the issue here.

Suggested changelog entry:

  • py::cast now throws error_already_set when the conversion fails with a Python error set, instead of returning a null object.

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.

1 participant