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