cpython/Tools/c-analyzer
Kirill Podoprigora bee112a94d
gh-124872: Replace enter/exit events with "switched" (#125532)
Users want to know when the current context switches to a different
context object.  Right now this happens when and only when a context
is entered or exited, so the enter and exit events are synonymous with
"switched".  However, if the changes proposed for gh-99633 are
implemented, the current context will also switch for reasons other
than context enter or exit.  Since users actually care about context
switches and not enter or exit, replace the enter and exit events with
a single switched event.

The former exit event was emitted just before exiting the context.
The new switched event is emitted after the context is exited to match
the semantics users expect of an event with a past-tense name.  If
users need the ability to clean up before the switch takes effect,
another event type can be added in the future.  It is not added here
because YAGNI.

I skipped 0 in the enum as a matter of practice.  Skipping 0 makes it
easier to troubleshoot when code forgets to set zeroed memory, and it
aligns with best practices for other tools (e.g.,
https://protobuf.dev/programming-guides/dos-donts/#unspecified-enum).

Co-authored-by: Richard Hansen <rhansen@rhansen.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
2024-10-16 13:53:21 +02:00
..
c_analyzer Fix typos in variable names, function names, and comments (GH-101868) 2023-12-01 09:37:40 +00:00
c_common gh-105407: Remove unused imports in Tools/c-analyzer/ (#105410) 2023-06-06 21:08:48 +00:00
c_parser Fix typos (#123775) 2024-09-09 14:58:26 +02:00
cpython gh-124872: Replace enter/exit events with "switched" (#125532) 2024-10-16 13:53:21 +02:00
distutils gh-105407: Remove unused imports in Tools/c-analyzer/ (#105410) 2023-06-06 21:08:48 +00:00
README gh-116265: Remove obsolete sentence. (#116284) 2024-03-04 16:49:42 -05:00
TODO gh-104169: Fix test_peg_generator after tokenizer refactoring (#110727) 2023-10-12 09:34:35 +02:00
c-analyzer.py
check-c-globals.py
must-resolve.sh
table-file.py gh-90110: Clean Up the C-analyzer Globals Lists (gh-100091) 2022-12-07 15:02:47 -07:00

README

#######################################
# C Globals and CPython Runtime State.

CPython's C code makes extensive use of global variables.  Each global
falls into one of several categories:

* (effectively) constants (incl. static types)
* globals used exclusively in main or in the REPL
* freelists, caches, and counters
* process-global state
* module state
* Python runtime state

Of the different categories, the last two are problematic and
generally should not exist in the codebase.

Globals that hold module state (i.e. in Modules/*.c) cause problems
when multiple interpreters are in use.  For more info, see PEP 3121,
which addresses the situation for extension modules in general.

Globals in the last category should be avoided as well.  The problem
isn't with the Python runtime having state.  Rather, the problem is with
that state being spread throughout the codebase in dozens of individual
globals.  Unlike the other globals, the runtime state represents a set
of values that are constantly shifting in a complex way.  When they are
spread out it's harder to get a clear picture of what the runtime
involves.  Furthermore, when they are spread out it complicates efforts
that change the runtime.

Consequently, the globals for Python's runtime state have been
consolidated under a single top-level _PyRuntime global. No new globals
should be added for runtime state.  Instead, they should be added to
_PyRuntimeState or one of its sub-structs.  The check-c-globals script
should be run to ensure that no new globals have been added:

  python3 Tools/c-analyzer/check-c-globals.py

You can also use the more generic tool:

  python3 Tools/c-analyzer/c-analyzer.py

If it reports any globals then they should be resolved.  If the globals
are runtime state then they should be folded into _PyRuntimeState.