cpython/Include/internal
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
..
pycore_abstract.h
pycore_asdl.h gh-87347: Add parenthesis around macro arguments (#93915) 2022-06-20 16:04:52 +02:00
pycore_ast.h [3.12] gh-104799: Move location of type_params AST fields (GH-104828) (#104974) 2023-05-30 14:33:01 +00:00
pycore_ast_state.h [3.12] gh-106905: Use separate structs to track recursion depth in each PyAST_mod2obj call. (GH-113035) (GH-113472) 2023-12-25 19:20:07 +00:00
pycore_atexit.h [3.12] gh-100227: Lock Around Use of the Global "atexit" State (gh-105514) (gh-105517) 2023-06-08 19:27:44 +00:00
pycore_atomic.h Fix typo in internal/pycore_atomic.h (GH-95939) 2022-08-13 11:40:41 +08:00
pycore_atomic_funcs.h
pycore_bitutils.h
pycore_blocks_output_buffer.h
pycore_bytes_methods.h
pycore_bytesobject.h gh-94673: Ensure Builtin Static Types are Readied Properly (gh-103940) 2023-04-27 16:19:43 -06:00
pycore_call.h gh-98003: Inline call frames for CALL_FUNCTION_EX (GH-98004) 2023-04-30 21:08:26 +08:00
pycore_ceval.h [3.12] gh-112367: Only free perf trampoline arenas at shutdown (GH-112368) (#112590) 2023-12-01 16:05:31 +00:00
pycore_ceval_state.h [3.12] gh-104812: Run Pending Calls in any Thread (gh-104813) (gh-105752) 2023-06-14 00:50:08 +00:00
pycore_code.h [3.12] gh-106550: Fix sign conversion in pycore_code.h (GH-112613) (#112696) 2023-12-04 11:14:13 +00:00
pycore_compile.h gh-104615: don't make unsafe swaps in apply_static_swaps (#104620) 2023-05-18 21:22:03 +00:00
pycore_condvar.h
pycore_context.h gh-81057: Move contextvars-related Globals to _PyRuntimeState (gh-99400) 2022-11-16 09:54:28 -07:00
pycore_descrobject.h gh-93911: Specialize LOAD_ATTR_PROPERTY (GH-93912) 2022-06-17 23:13:17 +08:00
pycore_dict.h gh-100227: Move dict_state.global_version to PyInterpreterState (gh-102338) 2023-03-09 08:16:30 -07:00
pycore_dict_state.h gh-104252: Immortalize Py_EMPTY_KEYS (gh-104253) 2023-05-10 07:28:40 -06:00
pycore_dtoa.h [3.12] gh-119447: Fix build with _PY_SHORT_FLOAT_REPR == 0 (#121178) (#121180) 2024-06-30 10:02:08 +00:00
pycore_emscripten_signal.h
pycore_exceptions.h
pycore_faulthandler.h gh-81057: Move faulthandler Globals to _PyRuntimeState (gh-100152) 2022-12-12 09:58:46 -07:00
pycore_fileutils.h [3.12] gh-106242: Fix path truncation in os.path.normpath (GH-106816) (#107981) 2023-08-17 01:19:48 +02:00
pycore_fileutils_windows.h gh-102765: Update ntpath.isdir/isfile/islink/exists to use GetFileInformationByName when available (GH-103485) 2023-04-27 14:23:26 +00:00
pycore_floatobject.h gh-81057: Move More Globals in Core Code to _PyRuntimeState (gh-99516) 2022-11-16 09:37:14 -07:00
pycore_flowgraph.h [3.12] gh-113297: Fix segfault in compiler for with statement with 19 context managers (#113327) (#113404) 2023-12-23 13:29:11 +00:00
pycore_format.h gh-104018: remove unused format "z" handling in string formatfloat() (#104107) 2023-05-07 10:11:42 +05:30
pycore_frame.h [3.12] Fix typos in comments (GH-120481) (#120775) 2024-06-20 03:59:31 +00:00
pycore_function.h gh-103763: Implement PEP 695 (#103764) 2023-05-15 20:36:23 -07:00
pycore_gc.h gh-97922: Run the GC only on eval breaker (#97920) 2022-10-08 07:57:09 -07:00
pycore_genobject.h GH-99005: More intrinsics (GH-100774) 2023-01-06 14:47:57 +00:00
pycore_getopt.h
pycore_gil.h
pycore_global_objects.h [3.12] gh-106931: Intern Statically Allocated Strings Globally (gh-107272) (gh-110713) 2023-11-27 23:51:12 +00:00
pycore_global_objects_fini_generated.h [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
pycore_global_strings.h [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
pycore_hamt.h gh-81057: Move contextvars-related Globals to _PyRuntimeState (gh-99400) 2022-11-16 09:54:28 -07:00
pycore_hashtable.h [3.12] gh-106931: Intern Statically Allocated Strings Globally (gh-107272) (gh-110713) 2023-11-27 23:51:12 +00:00
pycore_import.h [3.12] gh-105699: Use a _Py_hashtable_t for the PyModuleDef Cache (gh-106974) (gh-107412) 2023-07-28 23:16:12 +00:00
pycore_initconfig.h gh-98608: Stop Treating All Errors from _Py_NewInterpreterFromConfig() as Fatal (gh-102657) 2023-03-21 10:49:12 -06:00
pycore_instruments.h [3.12] chore: fix typos (#116345) (#116370) 2024-03-05 18:51:17 +00:00
pycore_interp.h [3.12] gh-110310: Add a Per-Interpreter XID Registry for Heap Types (gh-110311) (gh-110714) 2023-11-28 02:36:29 +00:00
pycore_intrinsics.h gh-103763: Implement PEP 695 (#103764) 2023-05-15 20:36:23 -07:00
pycore_list.h gh-100146: Steal references from stack when building a list (#100147) 2023-01-03 10:49:49 -08:00
pycore_long.h GH-101291: Add low level, unstable API for pylong (GH-101685) 2023-05-21 14:45:48 +01:00
pycore_memoryobject.h gh-104223: Fix issues with inheriting from buffer classes (#104227) 2023-05-08 09:52:41 -07:00
pycore_moduleobject.h gh-104066: Improve performance of hasattr for module objects (#104063) 2023-05-04 08:50:26 -06:00
pycore_namespace.h
pycore_object.h [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
pycore_object_state.h [3.12] gh-107080: Fix Py_TRACE_REFS Crashes Under Isolated Subinterpreters (#107751) 2023-08-16 12:03:05 +02:00
pycore_obmalloc.h [3.12] gh-112867: fix for WITH_PYMALLOC_RADIX_TREE=0 (GH-112885) (#113068) 2023-12-13 13:06:43 -08:00
pycore_obmalloc_init.h gh-101659: Isolate "obmalloc" State to Each Interpreter (gh-101660) 2023-04-24 17:23:57 -06:00
pycore_opcode.h gh-103865: add monitoring support to LOAD_SUPER_ATTR (#103866) 2023-05-16 10:29:00 -06:00
pycore_opcode_utils.h gh-91276: remove unused _PyOpcode_RelativeJump (#103156) 2023-04-11 11:20:39 +01:00
pycore_parser.h gh-81057: Move the Cached Parser Dummy Name to _PyRuntimeState (#100277) 2022-12-16 13:48:03 +00:00
pycore_pathconfig.h
pycore_pyarena.h
pycore_pyerrors.h gh-77757: replace exception wrapping by PEP-678 notes in typeobject's __set_name__ (#103402) 2023-04-11 11:53:06 +01:00
pycore_pyhash.h gh-81057: Move More Globals in Core Code to _PyRuntimeState (gh-99516) 2022-11-16 09:37:14 -07:00
pycore_pylifecycle.h GH-101520: Move tracemalloc functionality into core, leaving interface in Modules. (#104508) 2023-05-17 14:17:16 +01:00
pycore_pymath.h Remove unused internal macros (#102415) 2023-03-04 12:20:14 +00:00
pycore_pymem.h [3.12] gh-101524: Only Use Public C-API in the _xxsubinterpreters Module (gh-105258) (gh-107303) 2023-07-27 13:15:47 -06:00
pycore_pymem_init.h gh-81057: Move the Allocators to _PyRuntimeState (gh-99217) 2022-11-11 16:30:46 -07:00
pycore_pystate.h [3.12] gh-105716: Support Background Threads in Subinterpreters Consistently (gh-109921) (gh-110707) 2023-11-27 19:01:05 -07:00
pycore_pythread.h gh-81057: Fix the wasm32-wasi Buildbot (gh-100139) 2022-12-09 10:17:54 -07:00
pycore_range.h gh-89189: More compact range iterator (GH-27986) 2022-11-30 23:04:30 +02:00
pycore_runtime.h [3.12] gh-110310: Add a Per-Interpreter XID Registry for Heap Types (gh-110311) (gh-110714) 2023-11-28 02:36:29 +00:00
pycore_runtime_init.h [3.12] gh-109894: Fix initialization of static MemoryError in subinterpreter (gh-110911) (gh-111238) 2023-11-27 19:41:53 -07:00
pycore_runtime_init_generated.h [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
pycore_signal.h gh-81057: Move Signal-Related Globals to _PyRuntimeState (gh-100085) 2022-12-12 16:50:19 -07:00
pycore_sliceobject.h GH-94163: Add BINARY_SLICE and STORE_SLICE instructions. (GH-94168) 2022-06-27 12:24:23 +01:00
pycore_strhex.h
pycore_structseq.h gh-94673: Properly Initialize and Finalize Static Builtin Types for Each Interpreter (gh-104072) 2023-05-01 19:36:00 -06:00
pycore_symtable.h [3.12] gh-119311: Fix name mangling with PEP 695 generic classes (#119464) (#119644) 2024-06-04 19:55:45 +00:00
pycore_sysmodule.h gh-101758: Clean Up Uses of Import State (gh-101919) 2023-02-15 15:32:31 -07:00
pycore_time.h gh-81057: Move time Globals to _PyRuntimeState (gh-100122) 2022-12-08 16:46:09 -07:00
pycore_token.h gh-102856: Python tokenizer implementation for PEP 701 (#104323) 2023-05-21 01:03:02 +01:00
pycore_traceback.h
pycore_tracemalloc.h gh-77532: Minor tweaks to allow compiling with PlatformToolset=ClangCL on Windows (GH-101352) 2023-01-27 14:45:08 +00:00
pycore_tuple.h gh-94673: Ensure Builtin Static Types are Readied Properly (gh-103940) 2023-04-27 16:19:43 -06:00
pycore_typeobject.h [3.12] gh-112125: Fix None.__ne__(None) returning NotImplemented instead of … (#112827) 2023-12-07 13:41:00 +00:00
pycore_typevarobject.h gh-104549: Set __module__ on TypeAliasType (#104550) 2023-05-18 15:56:15 -07:00
pycore_ucnhash.h
pycore_unicodeobject.h [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
pycore_unicodeobject_generated.h [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
pycore_unionobject.h gh-87347: Add parenthesis around PyXXX_Check() arguments (#92815) 2022-06-16 13:49:43 +02:00
pycore_warnings.h