Skip to content
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

[C API] Add PyDict_ContainsString() function #108314

Closed
vstinner opened this issue Aug 22, 2023 · 4 comments
Closed

[C API] Add PyDict_ContainsString() function #108314

vstinner opened this issue Aug 22, 2023 · 4 comments
Labels
topic-C-API type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

vstinner commented Aug 22, 2023

Feature or enhancement

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Proposal:

Most, if not all, PyDict C APIs have a "String" flavor where the key argument is expressed as a UTF-8 encoded bytes string. But the PyDict_Contains() API is missing such variant.

I suppose that it was not proposed before since PyDict_GetItemString(dict, key) != NULL can already be used. My problem is that PyDict_GetItemString() ignores errors: I would like to report errors.

The newly added PyDict_GetItemStringRef() can be used to check if a dictionary has a key and report errors, but it requires calling Py_DECREF() which is not convenient. Example:

PyObject *value;
if (PyDict_GetItemStringRef(dict, key, &value) < 0) {
    // ... handle error ...
}
int has_value = (value != NULL);
Py_XDECREF(value);
// ... use has_value ...

I would like to be able to replace this code with:

int has_value = PyDict_ContainsString(dict, key);
if (has_value < 0) {    
    // ... handle error ...
}
// ... use has_value ...

There is no need to INCREF/DECREF just to check if a dictionary has a key.

Linked PRs

@vstinner vstinner added the type-feature A feature request or enhancement label Aug 22, 2023
vstinner added a commit to vstinner/cpython that referenced this issue Aug 22, 2023
Use PyDict_ContainsString() in pylifecycle.c and pythonrun.c.
vstinner added a commit to vstinner/cpython that referenced this issue Aug 22, 2023
Use PyDict_ContainsString() in pylifecycle.c and pythonrun.c.
vstinner added a commit to vstinner/cpython that referenced this issue Aug 22, 2023
Use PyDict_ContainsString() in pylifecycle.c and pythonrun.c.
@vstinner
Copy link
Member Author

There is already an abstract PyMapping_HasKeyString() function. Sadly, on error, this function clears the exception and returns 0 :-(

@corona10
Copy link
Member

Has this already been discussed elsewhere?

FYI, there was the discussion with adding this API.
capi-workgroup/problems#23

@encukou
Copy link
Member

encukou commented Aug 23, 2023

And here: #100100

vstinner added a commit to vstinner/cpython that referenced this issue Aug 23, 2023
* The new function is not part of the limited C API.
* Use PyDict_ContainsString() in pylifecycle.c and pythonrun.c.
vstinner added a commit to vstinner/cpython that referenced this issue Aug 24, 2023
PyDict_GetItemString(), PyDict_SetItemString() and
PyDict_DelItemString() expects a UTF-8 encoding string for the key.
@vstinner
Copy link
Member Author

I added the function to pythoncapi-compat: python/pythoncapi-compat@4734c8e

Yhg1s pushed a commit that referenced this issue Aug 25, 2023
gh-108314: PyDict_GetItemString() mentions UTF-8

PyDict_GetItemString(), PyDict_SetItemString() and
PyDict_DelItemString() expects a UTF-8 encoding string for the key.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 25, 2023
…GH-108448)

pythongh-108314: PyDict_GetItemString() mentions UTF-8

PyDict_GetItemString(), PyDict_SetItemString() and
PyDict_DelItemString() expects a UTF-8 encoding string for the key.
(cherry picked from commit 9a225d7)

Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this issue Aug 25, 2023
…8448) (#108489)

[3.12] gh-108314: PyDict_GetItemString() mentions UTF-8 (GH-108448)

gh-108314: PyDict_GetItemString() mentions UTF-8

PyDict_GetItemString(), PyDict_SetItemString() and
PyDict_DelItemString() expects a UTF-8 encoding string for the key.
(cherry picked from commit 9a225d7)

Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-C-API type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants