Repository navigation
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
Merged
serhiy-storchaka
merged 2 commits into
python:main
from
serhiy-storchaka:PyObject_HasAttrWithError-PyMapping_HasKeyWithError
Sep 17, 2023
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
gh-108511: Add C API functions which do not silently ignore errors
Add the following functions: * PyObject_HasAttrWithError() * PyObject_HasAttrStringWithError() * PyMapping_HasKeyWithError() * PyMapping_HasKeyStringWithError()
- Loading branch information
commit ae6e78904ce014135837ed134d6451127d443e74
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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``. | ||||||
|
Contributor
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. | ||||||
|
|
||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/C API/2023-09-01-16-28-09.gh-issue-108511.gg-QDG.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 :-)