Skip to content

Commit b758414

Browse files
gh-144161: Clarify immortality of interned strings
Clarify build-dependent interning behavior in the Python and C APIs and the free-threading HOWTO. Follow the discussion and earlier work in gh-144176 while retaining the advice to keep a reference to the result. Co-authored-by: Hernani Samuel Diniz <[email protected]>
1 parent 036d309 commit b758414

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

‎Doc/c-api/unicode.rst‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1663,9 +1663,14 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
16631663
:c:expr:`PyUnicode_CheckExact(*p_unicode)` must be true. If it is not,
16641664
then -- as with any other error -- the argument is left unchanged.
16651665
1666-
Note that interned strings are not “immortal”.
1666+
Strings interned by this function are not necessarily :term:`immortal`.
16671667
You must keep a reference to the result to benefit from interning.
16681668
1669+
.. note::
1670+
1671+
In the :term:`free-threaded build`, all interned strings are immortal.
1672+
This behavior may change in future Python versions.
1673+
16691674
16701675
.. c:function:: PyObject* PyUnicode_InternFromString(const char *str)
16711676

‎Doc/howto/free-threading-python.rst‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,13 @@ This section describes known limitations of the free-threaded CPython build.
9696
Immortalization
9797
---------------
9898

99-
In the free-threaded build, some objects are :term:`immortal`.
100-
Immortal objects are not deallocated and have reference counts that are
101-
never modified. This is done to avoid reference count contention that would
102-
prevent efficient multi-threaded scaling.
99+
The free-threaded build makes more objects :term:`immortal` than the default
100+
GIL-enabled build. Immortal objects are not deallocated and have reference
101+
counts that are never modified. This is done to avoid reference count
102+
contention that would prevent efficient multi-threaded scaling.
103103

104-
As of the 3.14 release, immortalization is limited to:
104+
As of the 3.14 release, the following objects are immortalized in addition to
105+
those immortalized by the default build:
105106

106107
* Code constants: numeric literals, string literals, and tuple literals
107108
composed of other constants.
@@ -174,11 +175,10 @@ build. There are multiple reasons for this, mostly due to design decisions.
174175
All interned strings are immortal
175176
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
176177

177-
For modern Python versions (since version 2.3), interning a string (e.g. with
178-
:func:`sys.intern`) does not cause it to become immortal. Instead, if the last
179-
reference to that string disappears, it will be removed from the interned
180-
string table. This is not the case for the free-threaded build and any interned
181-
string will become immortal, surviving until interpreter shutdown.
178+
In the default GIL-enabled build, mortal interned strings are removed from the
179+
interned string table when their last reference disappears. In the
180+
free-threaded build, all interned strings are immortal and remain alive until
181+
interpreter shutdown.
182182

183183

184184
Non-GC objects have a larger object header

‎Doc/library/sys.rst‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,8 +1375,13 @@ always available. Unless explicitly noted otherwise, all variables are read-only
13751375
names used in Python programs are automatically interned, and the dictionaries
13761376
used to hold module, class or instance attributes have interned keys.
13771377

1378-
Interned strings are not :term:`immortal`; you must keep a reference to the
1379-
return value of :func:`intern` around to benefit from it.
1378+
Interned strings are not necessarily :term:`immortal`; you must keep a
1379+
reference to the return value of :func:`intern` around to benefit from it.
1380+
1381+
.. impl-detail::
1382+
1383+
In the :term:`free-threaded build`, all interned strings are immortal.
1384+
This behavior may change in future Python versions.
13801385

13811386

13821387
.. function:: _is_gil_enabled()

0 commit comments

Comments
 (0)