Skip to content

Add additional info to TypeError when C++->Python casting fails - #3605

Merged
Skylion007 merged 4 commits into
pybind:masterfrom
Skylion007:raise_from_return_error
Jan 11, 2022
Merged

Add additional info to TypeError when C++->Python casting fails#3605
Skylion007 merged 4 commits into
pybind:masterfrom
Skylion007:raise_from_return_error

Conversation

@Skylion007

@Skylion007Skylion007 commented Jan 9, 2022

Copy link
Copy Markdown
Collaborator

Description

Adds additional information when raising a type error during function return. Inspired by the unexpected behavior from #3600 .

Suggested changelog entry:

* If possible, attach Python exception with ``py::raise_from`` to TypeError when casting from C++ to Python. This will give additional info if Python exceptions occur in the caster. Adds a test case of trying to convert a set from C++ to Python when the hash function is not defined in Python. 

As a meta note, this is an issue because we are a bit inconsistent in how we handle Python exceptions with py::set, py::dict, etc...). We currently not raising an exception from Python to C++ when interacting with them, maybe we should consider doing that.

rwgk
rwgk approved these changes Jan 9, 2022

@rwgkrwgk 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.

Thanks Aaron!

Comment threadtests/test_operator_overloading.cpp
Comment threadtests/test_operator_overloading.py
@Skylion007
Skylion007 merged commit ef070f7 into pybind:masterJan 11, 2022
@Skylion007
Skylion007 deleted the raise_from_return_error branch January 11, 2022 02:18
@github-actionsgithub-actionsBot added the needs changelog Possibly needs a changelog entry label Jan 11, 2022
@henryiiihenryiii removed the needs changelog Possibly needs a changelog entry label Feb 2, 2022
@rwgkrwgk mentioned this pull request Feb 10, 2023
Sign up for freeto 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.

3 participants

@Skylion007@rwgk@henryiii