cpython/Doc/c-api
Petr Viktorin 49f6beb56a
[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 <ericsnowcurrently@gmail.com>
2024-09-27 13:28:48 -07:00
..
abstract.rst bpo-41103: Resurrect the old buffer protocol. (GH-27437) 2021-07-29 12:46:47 +02:00
allocation.rst [3.12] gh-98154: Clarify Usage of "Reference Count" In the Docs (gh-107552) (#107752) 2023-08-08 16:25:35 +02:00
apiabiversion.rst [3.12] gh-107298: Fix Sphinx warnings in the C API doc (GH-107302) (GH-107375) 2023-07-28 06:40:16 +00:00
arg.rst [3.12] gh-121905: Consistently use "floating-point" instead of "floating point" (GH-121907) (GH-122013) 2024-07-19 09:08:33 +00:00
bool.rst [3.12] gh-101100: Sphinx warnings: pick the low hanging fruits (GH-107386) (GH-107419) 2023-07-29 09:16:27 +03:00
buffer.rst [3.12] gh-119467: Fix Py_buffer.format type and correct documentation typo (GH-119475) (#119603) 2024-05-27 12:22:23 +00:00
bytearray.rst [3.12] gh-122623: Improve c-api/bytearray.rst with error handling info (GH-122624) (#122659) 2024-08-03 22:02:24 +00:00
bytes.rst [3.12] docs: specify that PyBytes_AsStringAndSize returns 0 on success (GH-110888) (GH-111690) 2023-12-25 20:24:22 +02:00
call.rst [3.12] Add a version added note for PY_VECTORCALL_ARGUMENTS_OFFSET (GH-110963) (#111219) 2023-10-23 16:09:59 +00:00
capsule.rst [3.12] gh-107298: Fix yet more Sphinx warnings in the C API doc (GH-107345) (GH-107380) 2023-07-28 08:20:53 +00:00
cell.rst [3.12] gh-121533: Improve PyCell_[Get,Set] docs: mention the exceptions (GH-121534) (#121540) 2024-07-09 19:03:12 +03:00
code.rst [3.12] Use pep role instead of url (GH-121611) (#124173) 2024-09-17 13:08:38 +00:00
codec.rst [3.12] gh-107298: Fix doc references to undocumented modules (GH-107300) (GH-107370) 2023-07-28 06:17:49 +00:00
complex.rst [3.12] gh-121834: Improve complex C-API docs (GH-121835) (#121897) 2024-07-17 07:30:18 +00:00
concrete.rst GH-97950: Use new-style index directive ('object') (#104158) 2023-05-04 13:04:41 +03:00
contextvars.rst [3.12] gh-115664: Fix ordering of more versionadded and versionchanged directives (GH-116298) (GH-116450) 2024-03-07 08:21:25 +00:00
conversion.rst [3.12] Document PyOS_strtoul and PyOS_strtol (GH-114048) (GH-114618) 2024-01-26 17:50:33 +00:00
coro.rst bpo-42528: Improve the docs of most Py*_Check{,Exact} API calls (GH-23602) 2021-01-06 12:38:26 +01:00
datetime.rst [3.12] gh-123517: Remove unnecessary :meth: parentheses (gh-123518) (GH-123576) 2024-09-02 16:02:39 +02:00
descriptor.rst bpo-40170: Convert PyDescr_IsData() to static inline function (GH-24535) 2021-02-16 08:50:00 +01:00
dict.rst [3.12] Improve some C API documentation (GH-108768) (#108785) 2023-09-02 23:43:41 +02:00
exceptions.rst [3.12] gh-101100: Add a table of class attributes to the "Custom classes" section of the data model docs (#124480) (#124558) 2024-09-25 23:01:19 +00:00
file.rst [3.12] gh-115172: Fix explicit index extries for the C API (GH-115173) (GH-115292) 2024-02-11 10:34:41 +00:00
float.rst [3.12] gh-121905: Consistently use "floating-point" instead of "floating point" (GH-121907) (GH-122013) 2024-07-19 09:08:33 +00:00
frame.rst [3.12] gh-101100: Properly document frame object attributes (GH-112735) (#112772) 2023-12-05 19:34:06 +00:00
function.rst [3.12] gh-101100: Further improve docs on function attributes (GH-113001) (#113030) 2023-12-12 20:28:10 +00:00
gcsupport.rst [3.12] gh-101100: Fix sphinx warnings in c-api/gcsupport.rst (GH-114786) (#115263) 2024-02-13 14:25:54 -07:00
gen.rst bpo-42528: Improve the docs of most Py*_Check{,Exact} API calls (GH-23602) 2021-01-06 12:38:26 +01:00
hash.rst [3.12] gh-101100: Fix Sphinx warnings in whatsnew/3.2.rst (GH-115580) (#115589) 2024-02-17 14:18:30 -07:00
import.rst [3.12] gh-123492: Remove unnecessary :func: parentheses (gh-123493) (#123512) 2024-08-30 20:09:55 +03:00
index.rst bpo-36763: Implement the PEP 587 (GH-13592) 2019-05-27 16:39:22 +02:00
init.rst [3.12] gh-120838: Add a Note in the Docs About Expectations for Py_Finalize() (gh-120853) 2024-06-21 19:12:08 +00:00
init_config.rst [3.12] Swap the and from in sentence in init_config.rst (GH-120086) (#123743) 2024-09-05 20:11:25 +00:00
intro.rst [3.12] gh-115172: Fix explicit index extries for the C API (GH-115173) (GH-115292) 2024-02-11 10:34:41 +00:00
iter.rst More minor fixes to C API docs (GH-31525) 2022-04-02 12:31:05 -07:00
iterator.rst [3.12] gh-107298: Fix Sphinx warnings in the C API doc (GH-107302) (GH-107375) 2023-07-28 06:40:16 +00:00
list.rst GH-97950: Use new-style index directive ('builtin') (#104164) 2023-05-06 06:54:08 +03:00
long.rst [3.12] gh-122613: Document PyLong_GetInfo() (part of Limited API) (GH-122280) (#122644) 2024-08-05 20:11:42 +02:00
mapping.rst [3.12] Improve some C API documentation (GH-108768) (#108785) 2023-09-02 23:43:41 +02:00
marshal.rst [3.12] gh-121905: Consistently use "floating-point" instead of "floating point" (GH-121907) (GH-122013) 2024-07-19 09:08:33 +00:00
memory.rst [3.12] gh-115172: Fix explicit index extries for the C API (GH-115173) (GH-115292) 2024-02-11 10:34:41 +00:00
memoryview.rst [3.12] gh-101100: Fix sphinx warnings in Doc/c-api/memoryview.rst (GH-114669) (GH-114704) 2024-01-29 10:02:06 +00:00
method.rst [3.12] gh-107091: Fix the use of some C domain roles (GH-107092) (GH-107113) 2023-07-23 14:50:21 +03:00
module.rst [3.12] gh-113993: Make interned strings mortal (GH-120520, GH-121364, GH-121903, GH-122303) (#123065) 2024-09-27 13:28:48 -07:00
none.rst [3.12] gh-107298: Fix yet more Sphinx warnings in the C API doc (GH-107345) (GH-107380) 2023-07-28 08:20:53 +00:00
number.rst [3.12] gh-121905: Consistently use "floating-point" instead of "floating point" (GH-121907) (GH-122013) 2024-07-19 09:08:33 +00:00
objbuffer.rst bpo-41103: Resurrect the old buffer protocol. (GH-27437) 2021-07-29 12:46:47 +02:00
object.rst [3.12] gh-113993: Make interned strings mortal (GH-120520, GH-121364, GH-121903, GH-122303) (#123065) 2024-09-27 13:28:48 -07:00
objimpl.rst Doc: Replace the deprecated highlightlang directive by highlight. (#13377) 2019-05-17 15:25:34 +05:30
perfmaps.rst gh-103295: expose API for writing perf map files (#103546) 2023-05-21 11:12:24 +01:00
refcounting.rst [3.12] Use pep role instead of url (GH-121611) (#124173) 2024-09-17 13:08:38 +00:00
reflection.rst bpo-46836: Add Doc/c-api/frame.rst (GH-32051) 2022-03-23 13:19:13 +01:00
sequence.rst [3.12] gh-101100: Fix some broken sphinx references (GH-107095) (#107103) 2023-07-23 12:00:31 +02:00
set.rst [3.12] gh-101100: Fix Sphinx reference warnings (GH-112416) (#112420) 2023-11-26 05:22:13 -07:00
slice.rst [3.12] gh-121567: Improve slice C-API docs by mentioning exceptions (GH-121568) (#121579) 2024-07-10 11:03:22 +00:00
stable.rst [3.12] gh-112092: clarify unstable ABI recompilation requirements (GH-112093) (#114260) 2024-01-18 21:28:04 +01:00
structures.rst [3.12] Fix the PyGetSetDef documentation (GH-116056) (GH-116363) 2024-03-05 16:16:50 +00:00
sys.rst [3.12] gh-101100: Clean up Doc/c-api/exceptions.rst and Doc/c-api/sys.rst (GH-114825) (GH-115308) 2024-02-11 19:02:21 +00:00
tuple.rst [3.12] gh-123254: Improve tuple C API docs with more info about errors (GH-123255) (#123415) 2024-08-28 07:37:06 +00:00
type.rst [3.12] gh-101100: Add a table of class attributes to the "Custom classes" section of the data model docs (#124480) (#124558) 2024-09-25 23:01:19 +00:00
typehints.rst [3.12] gh-107298: Fix Sphinx warnings in the C API doc (GH-107302) (GH-107375) 2023-07-28 06:40:16 +00:00
typeobj.rst [3.12] gh-101100: Add a table of class attributes to the "Custom classes" section of the data model docs (#124480) (#124558) 2024-09-25 23:01:19 +00:00
unicode.rst [3.12] gh-113993: Make interned strings mortal (GH-120520, GH-121364, GH-121903, GH-122303) (#123065) 2024-09-27 13:28:48 -07:00
utilities.rst [3.12] gh-101100: Fix Sphinx warnings in whatsnew/3.2.rst (GH-115580) (#115589) 2024-02-17 14:18:30 -07:00
veryhigh.rst [3.12] gh-115172: Fix explicit index extries for the C API (GH-115173) (GH-115292) 2024-02-11 10:34:41 +00:00
weakref.rst [3.12] docs: fix a few typos identified by codespell (GH-119516) (#119571) 2024-05-26 08:15:57 +00:00