Skip to content

Commit 49f6beb

Browse files
[3.12] gh-113993: Make interned strings mortal (GH-120520, GH-121364, GH-121903, GH-122303) (#123065)
This backports several PRs for gh-113993, making interned strings mortal so they can be garbage-collected when no longer needed. * Allow interned strings to be mortal, and fix related issues (GH-120520) * Add an InternalDocs file describing how interning should work and how to use it. * Add internal functions to *explicitly* request what kind of interning is done: - `_PyUnicode_InternMortal` - `_PyUnicode_InternImmortal` - `_PyUnicode_InternStatic` * Switch uses of `PyUnicode_InternInPlace` to those. * Disallow using `_Py_SetImmortal` on strings directly. You should use `_PyUnicode_InternImmortal` instead: - Strings should be interned before immortalization, otherwise you're possibly interning a immortalizing copy. - `_Py_SetImmortal` doesn't handle the `SSTATE_INTERNED_MORTAL` to `SSTATE_INTERNED_IMMORTAL` update, and those flags can't be changed in backports, as they are now part of public API and version-specific ABI. * Add private `_only_immortal` argument for `sys.getunicodeinternedsize`, used in refleak test machinery. Make sure the statically allocated string singletons are unique. This means these sets are now disjoint: - `_Py_ID` - `_Py_STR` (including the empty string) - one-character latin-1 singletons Now, when you intern a singleton, that exact singleton will be interned. * Add a `_Py_LATIN1_CHR` macro, use it instead of `_Py_ID`/`_Py_STR` for one-character latin-1 singletons everywhere (including Clinic). * Intern `_Py_STR` singletons at startup. * Beef up the tests. Cover internal details (marked with `@cpython_only`). * Add lots of assertions * Don't immortalize in PyUnicode_InternInPlace; keep immortalizing in other API (GH-121364) * Switch PyUnicode_InternInPlace to _PyUnicode_InternMortal, clarify docs * Document immortality in some functions that take `const char *` This is PyUnicode_InternFromString; PyDict_SetItemString, PyObject_SetAttrString; PyObject_DelAttrString; PyUnicode_InternFromString; and the PyModule_Add convenience functions. Always point out a non-immortalizing alternative. * Don't immortalize user-provided attr names in _ctypes * Immortalize names in code objects to avoid crash (GH-121903) * Intern latin-1 one-byte strings at startup (GH-122303) There are some 3.12-specific changes, mainly to allow statically allocated strings in deepfreeze. (In 3.13, deepfreeze switched to the general `_Py_ID`/`_Py_STR`.) Co-authored-by: Eric Snow <[email protected]>
1 parent 2fa9ca5 commit 49f6beb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+26037
-27612
lines changed

Doc/c-api/module.rst

+15
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,14 @@ state:
526526
Note that ``Py_XDECREF()`` should be used instead of ``Py_DECREF()`` in
527527
this case, since *obj* can be ``NULL``.
528528
529+
The number of different *name* strings passed to this function
530+
should be kept small, usually by only using statically allocated strings
531+
as *name*.
532+
For names that aren't known at compile time, prefer calling
533+
:c:func:`PyUnicode_FromString` and :c:func:`PyObject_SetAttr` directly.
534+
For more details, see :c:func:`PyUnicode_InternFromString`, which may be
535+
used internally to create a key object.
536+
529537
.. versionadded:: 3.10
530538
531539
@@ -590,6 +598,9 @@ state:
590598
used from the module's initialization function.
591599
Return ``-1`` with an exception set on error, ``0`` on success.
592600
601+
This is a convenience function that calls :c:func:`PyLong_FromLong` and
602+
:c:func:`PyModule_AddObjectRef`; see their documentation for details.
603+
593604
594605
.. c:function:: int PyModule_AddStringConstant(PyObject *module, const char *name, const char *value)
595606
@@ -598,6 +609,10 @@ state:
598609
``NULL``-terminated.
599610
Return ``-1`` with an exception set on error, ``0`` on success.
600611
612+
This is a convenience function that calls
613+
:c:func:`PyUnicode_InternFromString` and :c:func:`PyModule_AddObjectRef`;
614+
see their documentation for details.
615+
601616
602617
.. c:macro:: PyModule_AddIntMacro(module, macro)
603618

Doc/c-api/object.rst

+15
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ Object Protocol
107107
If *v* is ``NULL``, the attribute is deleted, but this feature is
108108
deprecated in favour of using :c:func:`PyObject_DelAttrString`.
109109
110+
The number of different attribute names passed to this function
111+
should be kept small, usually by using a statically allocated string
112+
as *attr_name*.
113+
For attribute names that aren't known at compile time, prefer calling
114+
:c:func:`PyUnicode_FromString` and :c:func:`PyObject_SetAttr` directly.
115+
For more details, see :c:func:`PyUnicode_InternFromString`, which may be
116+
used internally to create a key object.
110117
111118
.. c:function:: int PyObject_GenericSetAttr(PyObject *o, PyObject *name, PyObject *value)
112119
@@ -132,6 +139,14 @@ Object Protocol
132139
specified as a :c:expr:`const char*` UTF-8 encoded bytes string,
133140
rather than a :c:expr:`PyObject*`.
134141
142+
The number of different attribute names passed to this function
143+
should be kept small, usually by using a statically allocated string
144+
as *attr_name*.
145+
For attribute names that aren't known at compile time, prefer calling
146+
:c:func:`PyUnicode_FromString` and :c:func:`PyObject_DelAttr` directly.
147+
For more details, see :c:func:`PyUnicode_InternFromString`, which may be
148+
used internally to create a key object for lookup.
149+
135150
136151
.. c:function:: PyObject* PyObject_GenericGetDict(PyObject *o, void *context)
137152

Doc/c-api/unicode.rst

+26-6
Original file line numberDiff line numberDiff line change
@@ -1461,15 +1461,35 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
14611461
existing interned string that is the same as :c:expr:`*p_unicode`, it sets :c:expr:`*p_unicode` to
14621462
it (releasing the reference to the old string object and creating a new
14631463
:term:`strong reference` to the interned string object), otherwise it leaves
1464-
:c:expr:`*p_unicode` alone and interns it (creating a new :term:`strong reference`).
1464+
:c:expr:`*p_unicode` alone and interns it.
1465+
14651466
(Clarification: even though there is a lot of talk about references, think
1466-
of this function as reference-neutral; you own the object after the call
1467-
if and only if you owned it before the call.)
1467+
of this function as reference-neutral. You must own the object you pass in;
1468+
after the call you no longer own the passed-in reference, but you newly own
1469+
the result.)
1470+
1471+
This function never raises an exception.
1472+
On error, it leaves its argument unchanged without interning it.
1473+
1474+
Instances of subclasses of :py:class:`str` may not be interned, that is,
1475+
:c:expr:`PyUnicode_CheckExact(*p_unicode)` must be true. If it is not,
1476+
then -- as with any other error -- the argument is left unchanged.
1477+
1478+
Note that interned strings are not “immortal”.
1479+
You must keep a reference to the result to benefit from interning.
14681480
14691481
14701482
.. c:function:: PyObject* PyUnicode_InternFromString(const char *str)
14711483
14721484
A combination of :c:func:`PyUnicode_FromString` and
1473-
:c:func:`PyUnicode_InternInPlace`, returning either a new Unicode string
1474-
object that has been interned, or a new ("owned") reference to an earlier
1475-
interned string object with the same value.
1485+
:c:func:`PyUnicode_InternInPlace`, meant for statically allocated strings.
1486+
1487+
Return a new ("owned") reference to either a new Unicode string object
1488+
that has been interned, or an earlier interned string object with the
1489+
same value.
1490+
1491+
Python may keep a reference to the result, or
1492+
prevent it from being garbage-collected promptly.
1493+
For interning an unbounded number of different strings, such as ones coming
1494+
from user input, prefer calling :c:func:`PyUnicode_FromString` and
1495+
:c:func:`PyUnicode_InternInPlace` directly.

0 commit comments

Comments
 (0)