Skip to content

gh-146636: Add Free-threaded Stable ABI migration guide#148453

Open
clin1234 wants to merge 10 commits intopython:mainfrom
clin1234:patch-6
Open

gh-146636: Add Free-threaded Stable ABI migration guide#148453
clin1234 wants to merge 10 commits intopython:mainfrom
clin1234:patch-6

Conversation

@clin1234
Copy link
Copy Markdown
Contributor

@clin1234 clin1234 commented Apr 12, 2026

@bedevere-app bedevere-app bot added awaiting review docs Documentation in the Doc dir skip news labels Apr 12, 2026
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Apr 12, 2026
@bedevere-app bedevere-app bot mentioned this pull request Apr 12, 2026
8 tasks
This document provides detailed instructions on how to use the Free Threading Stable ABI in CPython, including guidelines for module initialization, API usage, and thread safety considerations.
Clarified access restrictions on PyObject members and recommended functions for type and reference count manipulation.
Clarified that direct access to PyObject members is prohibited.
Copy link
Copy Markdown
Contributor

@da-woods da-woods left a comment

Choose a reason for hiding this comment

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

There's a lot that isn't right here (or is mostly about freethreading generally rather than about the stable ABI).

:c:func:`PyDict_SetItem`, or explicitly using macros), it attempts to acquire
the underlying mutex.

Using Critical Sections
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The weakness right now is that I don't think they're part of the stable ABI (but hopefully will be)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you were referring to https://docs.python.org/3/c-api/dict.html#c.PyDict_SetItem, it's part of the Stable ABI: https://docs.python.org/3/c-api/dict.html#c.PyDict_SetItem

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No - I was referring to critical sections. Which I really hope are added before 3.15 comes out but I don't think currently are. Sorry if that was ambiguous.

Co-authored-by: da-woods <dw-git@d-woods.co.uk>
@da-woods
Copy link
Copy Markdown
Contributor

I think the way most people will want to proceed is:

  1. Switch to PEP 793 module initialization (https://peps.python.org/pep-0793/). This will hopefully be a fairly trivial change for most people.
  2. Switch to using opaque objects and accessing all their type data through PyObject_GetTypeData. This will be a painful change for most people (and especially if they're starting from static types).
  3. Follow the general free-threading C API advice in https://docs.python.org/3/howto/free-threading-extensions.html#container-thread-safety (e.g. avoid borrowed reference, use critical sections once they become available). My personal opinion is that it isn't worth duplicating the advice.
  4. Decide if they want to mark their extension as thread-safe.

But steps 1 and 2 are the main ones and are almost not covered here.

@clin1234
Copy link
Copy Markdown
Contributor Author

clin1234 commented Apr 12, 2026

Based on WIP PR: #148013

@picnixz
Copy link
Copy Markdown
Member

picnixz commented Apr 12, 2026

I don't understand why we are having this PR when the reference documentation is not merged. Can we revisit this later? I would have expected the PEP's author to write this.

@picnixz picnixz marked this pull request as draft April 12, 2026 17:03
Updated documentation to clarify the identification of free-threaded limited API builds in C, including changes to macros and initialization methods.
Updated the documentation to clarify the use of the stable ABI and GIL management in Python extensions, including changes to member access and initialization methods.
@clin1234 clin1234 marked this pull request as ready for review April 12, 2026 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants