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 guidelines for C APIs with output params · Issue #1125 · python/devguide

C API: Add guidelines for C APIs with output params #1125

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
C API: Add guidelines for C APIs with output params#1125
Copy link
Labels
topic-c apitype-featureAdditions; New content or section neededAdditions; New content or section needed

Description

@erlend-aasland
erlend-aasland
opened on Jun 26, 2023
Issue body actions

One other issue is whether the output parameter should be touched if there is no meaningful value.

Take PyDict_GetItemSuffixToBeDecided(PyDictObject *op, PyObject *key, PyObject **pvalue).
Should we touch *pvalue if returning -1 or 0.
In other words, in

   PyObject *value;
   int err = PyDict_GetItemSuffixToBeDecided(op, key, &value);

Should value be undefined if err <= 0, or should it be set to NULL?

It is more efficient to not touch it, since the return value specifies that it has no meaning.
But, do we trust all C extension authors not to read the value, which may point to a real object if they are unlucky? Setting *pvalue = NULL is safer.

Originally posted by @markshannon in #1121 (comment)

Activity

  1. erlend-aasland commented on Jun 26, 2023

    @erlend-aasland
    erlend-aasland
    on Jun 26, 2023
    ContributorAuthor
    More actions

    Quoting Victor's reply, #1121 (comment):

    In my PyDict_GetItemRef() PR, I chose to always initialized *pvalue, especially in the error case (return -1). IMO there is a minor performance overhead, but it closes a whole class of issue: undefined behavior if the function returns -1. Some C compilers try to evaluate all code paths and say that a &value argument "can be undefined" when the code is correct. A common workaround makes me sad: initialize the value in the calling site: PyObject *value = NULL; :-(

    For the "return 0" case (missing key), I also prefer to set *pvalue to NULL for the same reason, but also because people upgrading their code from PyDict_GetItem() and PyDict_GetItemWithError() may want to check if value is NULL or not, rather than checking the return value:

    PyObject *value;  // OMG! undefined value
    if (PyDict_GetItemRef(dict, key, &value) < 0) ... error ...
    else if (value == NULL) ... missing key ...
    else ... key is present ...
    

    Here the function result is not used for the second code path: ... missing key ....

    I'm not comfortable to document that *pvalue is intialized for the error case (return -1). But I wrote an unit test for that.

  2. erlend-aasland commented on Jun 26, 2023

    @erlend-aasland
    erlend-aasland
    on Jun 26, 2023
    ContributorAuthor
    More actions

    cc. @vstinner @markshannon

  3. erlend-aasland
    added
    type-featureAdditions; New content or section neededAdditions; New content or section needed
    on Jun 26, 2023
  4. erlend-aasland commented on Jun 26, 2023

    @erlend-aasland
    erlend-aasland
    on Jun 26, 2023
    ContributorAuthor
    More actions

    After thinking about this for some days, I've landed on the conclusion that API exposed in Python.h should explicitly set output params to NULL1; let's try to avoid segfaults bco. API misuse :) I would expect that for most APIs, the minor performance impact would be lost in benchmark noise.

    Footnotes

    1. I agree with Victor that we don't think we need to document that fact in the C API docs. ↩

  5. erlend-aasland commented on Jun 26, 2023

    @erlend-aasland
    erlend-aasland
    on Jun 26, 2023
    ContributorAuthor
    More actions

    [...] I've landed on the conclusion that API exposed in Python.h should explicitly set output params to NULL1; let's try to avoid segfaults bco. API misuse :) [...]

    IOW, we should ensure that there is no UB when API users check the output parameters.

  6. erlend-aasland
    linked a pull request that will close this issueAdd guidelines for C API with output parameters #1128on Jun 26, 2023
  7. willingc
    added
    topic-c api
    guide-new content
    type-featureAdditions; New content or section neededAdditions; New content or section needed
    and removed
    type-featureAdditions; New content or section neededAdditions; New content or section needed
    on Oct 11, 2023
  8. erlend-aasland
    closed this as not plannedon Oct 11, 2023
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-featureAdditions; New content or section neededAdditions; New content or section needed

    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.