You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[C API] Add PyDict_ContainsString() function #108314
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.
Activity
pythongh-108314: Add PyDict_ContainsString() function
pythongh-108314: Add PyDict_ContainsString() function
pythongh-108314: Add PyDict_ContainsString() function
vstinner commentedon Aug 22, 2023
There is already an abstract PyMapping_HasKeyString() function. Sadly, on error, this function clears the exception and returns 0 :-(
corona10 commentedon Aug 23, 2023
FYI, there was the discussion with adding this API.
capi-workgroup/problems#23
encukou commentedon Aug 23, 2023
And here: #100100
pythongh-108314: Add PyDict_ContainsString() function
gh-108314: Add PyDict_ContainsString() function (#108323)
pythongh-108314: PyDict_GetItemString() mentions UTF-8
vstinner commentedon Aug 24, 2023
I added the function to pythoncapi-compat: python/pythoncapi-compat@4734c8e
[3.12] gh-108314: PyDict_GetItemString() mentions UTF-8 (#108448)
[3.12] pythongh-108314: PyDict_GetItemString() mentions UTF-8 (python…
[3.11] [3.12] gh-108314: PyDict_GetItemString() mentions UTF-8 (GH-10…