-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Handling warnings from C++ #4535
base: master
Are you sure you want to change the base?
Conversation
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.
Looks like a nice feature! Just a couple of comments.
if (result == 1) { | ||
return true; | ||
} | ||
if (result == -1) { |
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.
We should probably have a more visible error / warning here right? Isn't -1 from IsSubclass supposed to be propagated to users?
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.
Is new commit OK? I am just not sure what "guidelines" on errors are applied for this specific scenario.
include/pybind11/pytypes.h
Outdated
PYBIND11_NAMESPACE_BEGIN(warnings) | ||
|
||
// Warning class | ||
static PyObject *warning_base = PyExc_Warning; |
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.
Why aren't these const?
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.
Or even constexpr
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.
Good point. However, from what I understand constexpr won't work out of the box for non-const pointers. Changed to consts.
tests/test_warnings.py
Outdated
assert m.CustomWarning is not None | ||
assert issubclass(m.CustomWarning, DeprecationWarning) | ||
|
||
import warnings |
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.
Why isn't this import at the top of the file?
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.
It was put there as local import for this specific test. Sure it can be moved around! Done.
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.
I think there was a bit of a misunderstanding. You made those pointer to constants (which is incorrect, as shown by those incorrect const casts) instead of constant pointers.
See
include/pybind11/pybind11.h
Outdated
"PyExc_Warning!"); | ||
} | ||
|
||
if (PyErr_WarnEx(const_cast<PyObject *>(category), message, stack_level) == -1) { |
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.
These const casts aren't safe
Description
Providing a simple interface to handle warnings on the pybind side. Following up #601 issue.
py::warning
class that provide wrapper for custom warnings.py::warnings
namespace containing shorthand versions of Python C API warning categories.raise_warning
that allow to use warnings directly frompy::warnings
namespace or created by the user.PS It is my first time contributing something "bigger" (finally! :)). Please, look at "TODO" section, there are some high-level decisions to be made. These are ones I have spotted. All reviews are welcome!
TODO:
stack_level
value is more relevant? C API is not defining defaultstack_level
. According to Python docs on warnings.warn the default value is1
. However, there is also a good point on the default to be set to2
:py::warnings
namespace a good name? Maybepy::warning_categories
is more descriptive and not conflicting withpy::warning
? ... or should it be removed entirely?Suggested changelog entry: