-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-108511: Add C API functions which do not silently ignore errors #109025
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -27,6 +27,24 @@ Object Protocol | |||||
instead of the :func:`repr`. | ||||||
|
||||||
|
||||||
.. c:function:: int PyObject_HasAttrWithError(PyObject *o, const char *attr_name) | ||||||
|
||||||
Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. | ||||||
This is equivalent to the Python expression ``hasattr(o, attr_name)``. | ||||||
On failure, return ``-1``. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the relevance of error handling for these new functions, I think the docs should mention explicitly under which conditions users must expect exceptions.
Suggested change
|
||||||
|
||||||
.. versionadded:: 3.13 | ||||||
|
||||||
|
||||||
.. c:function:: int PyObject_HasAttrStringWithError(PyObject *o, const char *attr_name) | ||||||
|
||||||
This is the same as :c:func:`PyObject_HasAttrWithError`, but *attr_name* is | ||||||
specified as a :c:expr:`const char*` UTF-8 encoded bytes string, | ||||||
rather than a :c:expr:`PyObject*`. | ||||||
|
||||||
.. versionadded:: 3.13 | ||||||
|
||||||
|
||||||
.. c:function:: int PyObject_HasAttr(PyObject *o, PyObject *attr_name) | ||||||
|
||||||
Returns ``1`` if *o* has the attribute *attr_name*, and ``0`` otherwise. This | ||||||
|
@@ -37,8 +55,8 @@ Object Protocol | |||||
|
||||||
Exceptions that occur when this calls :meth:`~object.__getattr__` and | ||||||
:meth:`~object.__getattribute__` methods are silently ignored. | ||||||
For proper error handling, use :c:func:`PyObject_GetOptionalAttr` or | ||||||
:c:func:`PyObject_GetAttr` instead. | ||||||
For proper error handling, use :c:func:`PyObject_HasAttrWithError`, | ||||||
:c:func:`PyObject_GetOptionalAttr` or :c:func:`PyObject_GetAttr` instead. | ||||||
|
||||||
|
||||||
.. c:function:: int PyObject_HasAttrString(PyObject *o, const char *attr_name) | ||||||
|
@@ -52,7 +70,8 @@ Object Protocol | |||||
Exceptions that occur when this calls :meth:`~object.__getattr__` and | ||||||
:meth:`~object.__getattribute__` methods or while creating the temporary | ||||||
:class:`str` object are silently ignored. | ||||||
For proper error handling, use :c:func:`PyObject_GetOptionalAttrString` | ||||||
For proper error handling, use :c:func:`PyObject_HasAttrStringWithError`, | ||||||
:c:func:`PyObject_GetOptionalAttrString` | ||||||
or :c:func:`PyObject_GetAttrString` instead. | ||||||
|
||||||
|
||||||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Add functions :c:func:`PyObject_HasAttrWithError`, | ||
:c:func:`PyObject_HasAttrStringWithError`, | ||
:c:func:`PyMapping_HasKeyWithError` and | ||
:c:func:`PyMapping_HasKeyStringWithError`. |
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.
Given the relevance of error handling for these new functions, I think the docs should mention explicitly under which conditions users must expect exceptions.
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.
Sometimes I write "On error, raise an exception and return -1." Even if in C, technically, it's more "setting" an exception, I like to use Python "raise" semantics in C, it's easy for me to map C/Python this way :-)