FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add guidelines for C API with output parameters by erlend-aasland · Pull Request #1128 · python/devguide · GitHub

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rst  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 26 additions & 0 deletions developer-workflow/c-api.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ Guidelines for expanding/changing the public API
- ``return 0``: lookup succeeded; no item was found
- ``return 1``: lookup succeeded; item was found

- APIs with output parameters should ensure that each output parameter is
initialized for all code paths.
Example:

.. code-block:: c

int
PyFoo_Bar(PyObject **out)
{
PyObject *value;
int rc = foo_bar(&value);

vstinner Jun 26, 2023
edited by terryjreedy
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Maybe you could mimic a legacy API call which returns NULL if not found and on error, and call PyErr_Occurred(). So the difference with the two APIs is even more obvious?

// Error case
if (rc < 0) {
Comment thread
erlend-aasland marked this conversation as resolved.
*out = NULL;
return -1;
}
// Not found (lesser result)
if (rc == 0) {
*out = NULL;
return 0;
}
// Found (greater result)
*out = Py_NewRef(value);
return 1;
}

Please start a public discussion if these guidelines won't work for your API.

.. note::
Expand Down

Back | FazBrowse Home | New Git URL