Skip to content

Add a new C API tutorial for reference counting and error handling#150090

Open
ZeroIntensity wants to merge 6 commits into
python:mainfrom
ZeroIntensity:c-api-tutorial
Open

Add a new C API tutorial for reference counting and error handling#150090
ZeroIntensity wants to merge 6 commits into
python:mainfrom
ZeroIntensity:c-api-tutorial

Conversation

@ZeroIntensity
Copy link
Copy Markdown
Member

No description provided.

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented May 19, 2026

Copy link
Copy Markdown
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

These look good, but now for the hard part: Could you remove the topics covered here from "Assorted topics"?

Comment thread Doc/extending/reference-counting.rst Outdated
if (bytes == NULL) {
return NULL;
}
const char *command = PyBytes_AS_STRING(bytes);
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.

Could you use PyBytes_AsString, to stay within the Limited API?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I could, but I'm worried it would send the wrong message:

  1. We know that the result will always be a bytes object here, so it's not a great idea to encourage the user to do needless error checking in order to stay compatible with an API layer they haven't learned about yet.
  2. It complicates the example, because it now needs a new error path with a Py_DECREF in it.

}

If :c:func:`PyUnicode_AsUTF8AndSize` is successful, *command* will point to the
If :c:func:`PyUnicode_AsUTF8` is successful, *command* will point to the
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.

Please keep the Stable ABI function here.

Copy link
Copy Markdown
Member Author

@ZeroIntensity ZeroIntensity May 21, 2026

Choose a reason for hiding this comment

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

The code and description here don't match PyUnicode_AsUTF8AndSize. It'd require some rewriting to be correct.

Comment thread Doc/extending/error-handling.rst Outdated
***************************

This chapter covers the details about how Python's C API expresses errors
and how to interact with Python exceptions.
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.

Could you mention that the tutorial builds on the module from "Your first C API extension module"?

return NULL;
}

// Py_NewRef() is just a shorthand for Py_INCREF() with an expression
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.

Would it make sense to introduce Py_NewRef in the refcounting chapter?

ZeroIntensity and others added 2 commits May 20, 2026 21:56
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
@ZeroIntensity
Copy link
Copy Markdown
Member Author

Could you remove the topics covered here from "Assorted topics"?

Just by deleting the section? Or do you want me to do some refactoring?

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

Projects

Status: Todo
Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants