python / python/cpython

Relax `PyUnstable_SetImmortal` to objects allocated in that thread?

オープン
#150,865 コメント 25 件 リアクション 0 件 担当者 1 名 GitHub で見る

@ZeroIntensity がすでに取り組んでいます。

2026年9月7日 から。

interpreter-core topic-C-API topic-free-threading type-feature
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Feature or enhancement

Proposal:

Currently PyUnstable_SetImmortal requires a uniquely referenced object. I think the docs could be a bit clearer about this but that isn't the tricky part.

The tricky part is that I think it doesn't seem uncommon to create objects which are not uniquely referenced. I have two overlapping places in NumPy where I was hope to use this while making types that were previously static to heaptypes:

  1. If you have non-trivial setup code that might fail you want to immortalize at the very end. In my case I wanted to add the object to a dict (as a last step). (Sure, if I am going to immortalize it, then I can re-order adding it to the dict with a lock or so.)
  2. I am using PyType_FromMetaType() and the returned object already has 3 references when created fresh.

Basically, I get the "err on the safe side" part, but from poking it a bit and looking at https://github.com/python/cpython/issues/113956 it seems that the real limitation seems to be that the current thread owns the object.

Ensuring the object is created on the current thread is trivial no matter how complicated object creation itself is.

CC @ngoldbaum

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-150956

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。