GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [C API] Add PyDict_ContainsString() function · Issue #108314 · python/cpython

[C API] Add PyDict_ContainsString() function #108314

New issue
Copy link
New issue
Copy link
Closed
Closed
[C API] Add PyDict_ContainsString() function#108314
Copy link
Labels
topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

Description

@vstinner
vstinner
opened on Aug 22, 2023
Issue body actions

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

  • gh-108314: Add PyDict_ContainsString() function #108323
  • [3.12] gh-108314: PyDict_GetItemString() mentions UTF-8 #108448
  • [3.11] [3.12] gh-108314: PyDict_GetItemString() mentions UTF-8 (GH-108448) #108489
Reacted by Kirill Podoprigora and Serhiy Storchaka

Activity

  1. vstinner
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Aug 22, 2023
  2. Eclips4
    added
    topic-C-API
    on Aug 22, 2023
  3. vstinner
    added 3 commits that reference this issue on Aug 22, 2023

    pythongh-108314: Add PyDict_ContainsString() function

    debcd39

    pythongh-108314: Add PyDict_ContainsString() function

    0b41b00

    pythongh-108314: Add PyDict_ContainsString() function

    b142bf9
  4. vstinner commented on Aug 22, 2023

    @vstinner
    vstinner
    on Aug 22, 2023
    MemberAuthor
    More actions

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

  5. corona10 commented on Aug 23, 2023

    @corona10
    corona10
    on Aug 23, 2023
    Member
    More actions

    Has this already been discussed elsewhere?

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

  6. encukou commented on Aug 23, 2023

    @encukou
    encukou
    on Aug 23, 2023
    Member
    More actions

    And here: #100100

  7. vstinner
    added 3 commits that reference this issue on Aug 23, 2023

    pythongh-108314: Add PyDict_ContainsString() function

    4916d67

    gh-108314: Add PyDict_ContainsString() function (#108323)

    6726626

    pythongh-108314: PyDict_GetItemString() mentions UTF-8

    2164d65
  8. vstinner commented on Aug 24, 2023

    @vstinner
    vstinner
    on Aug 24, 2023
    MemberAuthor
    More actions

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

  9. vstinner
    closed this as completedon Aug 24, 2023
  10. Yhg1s
    added a commit that references this issue on Aug 25, 2023

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

    9a225d7
  11. miss-islington
    added a commit that references this issue on Aug 25, 2023

    [3.12] pythongh-108314: PyDict_GetItemString() mentions UTF-8 (python…

    9609601
  12. vstinner
    added a commit that references this issue on Aug 25, 2023

    [3.11] [3.12] gh-108314: PyDict_GetItemString() mentions UTF-8 (GH-10…

    cc81f5b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.