Description
Bug report
Bug description:
The sys.intern
documentation explicitly says:
Interned strings are not immortal; you must keep a reference to the return value of
intern()
around to benefit from it.
However, they were made immortal in #19474 (Implement Immortal Objects -- implementation of PEP 683), without a documentation update. The 3.12 What's New also doesn't mention the change. [edit: it's now clear that the PR author (@eduardo-elizondo) intended it but the reviewer (@markshannon) did not]
PEP-683 itself only mentions the change as a possible optimization.
Meanwhile, pathlib
interns every path segment it processes, presumably depending on the documented behaviour. In CPython's test suite, that causes names of temporary directories to leak (stealthily, since interned strings are excluded from the total refcount).
[edit: Worse, type_setattro
, PyObject_SetAttr
or PyDict_SetItemString
immortalize keys, so strings used for key/attribute names this way are not reclaimed until interpreter shutdown.]
On the other hand, free-threading (PEP-703) seems to rely [edit: relies] on these being immortal.
What's the correct behaviour? [edit: specifically, in 3.12 and non-free-threading 3.13, should this change be reverted or documented?]
@eduardo-elizondo @ericsnowcurrently
Linked PRs
- gh-113993: Allow interned strings to be mortal, and fix related issues #120520
- [3.13] gh-113993: Allow interned strings to be mortal, and fix related issues (GH-120520) #120945
- gh-113993: For string interning, do not rely on (or assert) _Py_IsImmortal #121358
- gh-113993: Don't immortalize in PyUnicode_InternInPlace; keep immortalizing in other API #121364
- [3.13] gh-113993: For string interning, do not rely on (or assert) _Py_IsImmortal (GH-121358) #121851
- [3.13] gh-113993: Don't immortalize in PyUnicode_InternInPlace; keep immortalizing in other API (GH-121364) #121854
- [3.12] gh-113993: Make interned strings mortal (GH-120520, GH-121364, GH-121903, GH-122303) #123065
- [3.12] gh-113993: For string interning, do not rely on (or assert) _Py_IsImmortal (GH-121358) (GH-121851) #124938
- gh-113993: InternalDocs: Add String Interning to README #127250
Related fixes:
Metadata
Metadata
Assignees
Projects
Status