Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[C API] Add PyDict_ContainsString() function #108314

Closed
Labels
@vstinner

Description

@vstinner

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 thePyDict_Contains() API is missing such variant.

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

The newly addedPyDict_GetItemStringRef() can be used to check if a dictionary has a key and report errors, but it requires callingPy_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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp