17db96d56Sopenharmony_ci####################################### 27db96d56Sopenharmony_ci# C Globals and CPython Runtime State. 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ciCPython's C code makes extensive use of global variables. Each global 57db96d56Sopenharmony_cifalls into one of several categories: 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci* (effectively) constants (incl. static types) 87db96d56Sopenharmony_ci* globals used exclusively in main or in the REPL 97db96d56Sopenharmony_ci* freelists, caches, and counters 107db96d56Sopenharmony_ci* process-global state 117db96d56Sopenharmony_ci* module state 127db96d56Sopenharmony_ci* Python runtime state 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ciThe ignored-globals.txt file is organized similarly. Of the different 157db96d56Sopenharmony_cicategories, the last two are problematic and generally should not exist 167db96d56Sopenharmony_ciin the codebase. 177db96d56Sopenharmony_ci 187db96d56Sopenharmony_ciGlobals that hold module state (i.e. in Modules/*.c) cause problems 197db96d56Sopenharmony_ciwhen multiple interpreters are in use. For more info, see PEP 3121, 207db96d56Sopenharmony_ciwhich addresses the situation for extension modules in general. 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ciGlobals in the last category should be avoided as well. The problem 237db96d56Sopenharmony_ciisn't with the Python runtime having state. Rather, the problem is with 247db96d56Sopenharmony_cithat state being spread throughout the codebase in dozens of individual 257db96d56Sopenharmony_ciglobals. Unlike the other globals, the runtime state represents a set 267db96d56Sopenharmony_ciof values that are constantly shifting in a complex way. When they are 277db96d56Sopenharmony_cispread out it's harder to get a clear picture of what the runtime 287db96d56Sopenharmony_ciinvolves. Furthermore, when they are spread out it complicates efforts 297db96d56Sopenharmony_cithat change the runtime. 307db96d56Sopenharmony_ci 317db96d56Sopenharmony_ciConsequently, the globals for Python's runtime state have been 327db96d56Sopenharmony_ciconsolidated under a single top-level _PyRuntime global. No new globals 337db96d56Sopenharmony_cishould be added for runtime state. Instead, they should be added to 347db96d56Sopenharmony_ci_PyRuntimeState or one of its sub-structs. The check-c-globals script 357db96d56Sopenharmony_cishould be run to ensure that no new globals have been added: 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ci python3 Tools/c-analyzer/check-c-globals.py 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_ciYou can also use the more generic tool: 407db96d56Sopenharmony_ci 417db96d56Sopenharmony_ci python3 Tools/c-analyzer/c-analyzer.py 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ciIf it reports any globals then they should be resolved. If the globals 447db96d56Sopenharmony_ciare runtime state then they should be folded into _PyRuntimeState. 457db96d56Sopenharmony_ciOtherwise they should be added to ignored-globals.txt. 46