]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-113993: Allow interned strings to be mortal, and fix related issues (GH-120520)
authorPetr Viktorin <encukou@gmail.com>
Fri, 21 Jun 2024 15:19:31 +0000 (17:19 +0200)
committerGitHub <noreply@github.com>
Fri, 21 Jun 2024 15:19:31 +0000 (17:19 +0200)
commit6f1d448bc110633eda110310fd833bd46e7b30f2
tree39555edee103339beea386042b7ffcbcd56ddcda
parent7595e6743ac78ac0dd19418176f66d251668fafc
gh-113993: 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.

* For free-threaded builds, intern `_Py_LATIN1_CHR` singletons at startup.

* Beef up the tests. Cover internal details (marked with `@cpython_only`).

* Add lots of assertions

Co-Authored-By: Eric Snow <ericsnowcurrently@gmail.com>
42 files changed:
Include/internal/pycore_global_objects_fini_generated.h
Include/internal/pycore_global_strings.h
Include/internal/pycore_runtime_init_generated.h
Include/internal/pycore_unicodeobject.h
Include/internal/pycore_unicodeobject_generated.h
InternalDocs/string_interning.md [new file with mode: 0644]
Lib/test/clinic.test.c
Lib/test/libregrtest/refleak.py
Lib/test/test_clinic.py
Lib/test/test_sys.py
Misc/NEWS.d/next/Core and Builtins/2024-06-14-22-02-25.gh-issue-113993.MiA0vX.rst [new file with mode: 0644]
Modules/_io/stringio.c
Modules/_operator.c
Modules/_pickle.c
Modules/_sqlite/clinic/connection.c.h
Modules/clinic/_bisectmodule.c.h
Modules/clinic/_hashopenssl.c.h
Modules/clinic/_testclinic.c.h
Modules/clinic/_testclinic_depr.c.h
Modules/clinic/_testmultiphase.c.h
Modules/clinic/cmathmodule.c.h
Modules/clinic/itertoolsmodule.c.h
Modules/clinic/mathmodule.c.h
Modules/clinic/posixmodule.c.h
Objects/codeobject.c
Objects/dictobject.c
Objects/object.c
Objects/typeobject.c
Objects/unicodeobject.c
Parser/action_helpers.c
Parser/pegen.c
Python/ast_opt.c
Python/ast_unparse.c
Python/clinic/sysmodule.c.h
Python/codecs.c
Python/compile.c
Python/getargs.c
Python/import.c
Python/marshal.c
Python/sysmodule.c
Tools/build/generate_global_objects.py
Tools/clinic/libclinic/clanguage.py