-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add missing error handling to module_::def_submodule
#3973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
include/pybind11/pybind11.h
Outdated
if (this_name == nullptr) { | ||
throw error_already_set(); | ||
} | ||
std::string full_name = std::string(this_name) + "." + name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::string full_name = std::string(this_name) + "." + name; | |
std::string full_name = std::string(this_name) + '.' + name; |
if (!submodule) { | ||
throw error_already_set(); | ||
} | ||
auto result = reinterpret_borrow<module_>(submodule); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, wouldn't you also been able to do?
if(!result){
throw error_already_set();
}
This looks good either way though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIU your question correctly, that would assume reinterpret_borrow<module_>
is OK with nullptr
, and does not care about the active error. While that is usually true, what if someone temporarily adds debugging code to reinterpret_borrow
, or the module_
constructor in this case? Their fault, of course, but why build a trap like that?
Generally, I think it is bad style to separate error handling from the source of the error more than necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approve with minor nit: (Append a char instead of a 1char string literal, it's a faster overload.)
b8a16e3
to
dbbe9d1
Compare
Description
Extracted from PR #3964, where the missing error handling happened to cause some confusion.
Suggested changelog entry:
``module_::def_submodule`` was missing proper error handling. This is fixed now.