Skip to content

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

Open
ZeroIntensity wants to merge 4 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 4 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

Documentation build overview

📚 cpython-previews | 🛠️ Build #32760958 | 📁 Comparing 182d190 against main (6372d48)

  🔍 Preview build  

4 files changed
+ extending/error-handling.html
+ extending/reference-counting.html
± extending/first-extension-module.html
± extending/index.html

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 on lines +103 to +104
the caller. This is also called "stealing" a reference (so, in the above
example, the caller steals our strong reference to ``result``).
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.

I'd rather not use the term "stealing" for cases where transferring ownership is the normal thing to do.
How about this?

Suggested change
the caller. This is also called "stealing" a reference (so, in the above
example, the caller steals our strong reference to ``result``).
the caller.
The caller is now responsible for making sure :c:macro:`Py_DECREF` is called --
either by calling it, or by delegating this responsibility.

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?

}

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.

=======================

Python has a thread-local indicator for the state of the current exception.
This indicator is just a ``PyObject *`` referencing an instance of
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.

Suggested change
This indicator is just a ``PyObject *`` referencing an instance of
This indicator is a ``PyObject *`` referencing an instance of

***************************

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?

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