• Home
Name Date Size #Lines LOC

..--

c_analyzer/04-Jul-2025-1,6171,288

c_common/04-Jul-2025-1,8191,424

c_parser/04-Jul-2025-5,3854,261

cpython/04-Jul-2025-3,7183,161

distutils/04-Jul-2025-2,3151,559

READMED04-Jul-20251.8 KiB4433

TODOD04-Jul-202591.5 KiB1,015986

c-analyzer.pyD04-Jul-2025195 85

check-c-globals.pyD04-Jul-2025896 4030

must-resolve.shD04-Jul-20252.4 KiB7640

table-file.pyD04-Jul-20254.3 KiB155126

README

1#######################################
2# C Globals and CPython Runtime State.
3
4CPython's C code makes extensive use of global variables.  Each global
5falls into one of several categories:
6
7* (effectively) constants (incl. static types)
8* globals used exclusively in main or in the REPL
9* freelists, caches, and counters
10* process-global state
11* module state
12* Python runtime state
13
14Of the different categories, the last two are problematic and
15generally should not exist in the codebase.
16
17Globals that hold module state (i.e. in Modules/*.c) cause problems
18when multiple interpreters are in use.  For more info, see PEP 3121,
19which addresses the situation for extension modules in general.
20
21Globals in the last category should be avoided as well.  The problem
22isn't with the Python runtime having state.  Rather, the problem is with
23that state being spread throughout the codebase in dozens of individual
24globals.  Unlike the other globals, the runtime state represents a set
25of values that are constantly shifting in a complex way.  When they are
26spread out it's harder to get a clear picture of what the runtime
27involves.  Furthermore, when they are spread out it complicates efforts
28that change the runtime.
29
30Consequently, the globals for Python's runtime state have been
31consolidated under a single top-level _PyRuntime global. No new globals
32should be added for runtime state.  Instead, they should be added to
33_PyRuntimeState or one of its sub-structs.  The check-c-globals script
34should be run to ensure that no new globals have been added:
35
36  python3 Tools/c-analyzer/check-c-globals.py
37
38You can also use the more generic tool:
39
40  python3 Tools/c-analyzer/c-analyzer.py
41
42If it reports any globals then they should be resolved.  If the globals
43are runtime state then they should be folded into _PyRuntimeState.
44