# Python

> Where the framework runs Python, why 3.11 is the floor, which parts are standard library only and which are not, how the build image's hash-locked test tools are regenerated, and what a new Python can break.

Source: https://omardev29.github.io/rmp-docs/framework/maintaining/python.html

Every build of every game runs Python: CMake calls the generator on each configure, and `rmp` is a Python program. That makes Python look like the riskiest dependency the framework has. It is not, because the part every build runs uses nothing but Python's standard library. The packages that are hard to keep pinned belong to the test campaign, inside the build image, and a game never runs them.

## Where Python runs

| What | When | Needs |
|---|---|---|
| `tools/configure.py`, the generator | every CMake configure, and before every Gradle and Xcode build | the standard library; Pillow for the icons, if it is there |
| `tools/rmp.py`, behind `rmp` | every command | the standard library |
| The gates in `tools/` written in Python, and the Python inside the shell gates | `rmp test`, the lint job | the standard library; PyYAML for the workflow check |
| The tests of the tools, `tests/*_test.py` | `rmp test` | the standard library; PyYAML for a few |
| The property tests, `tests/*_property_test.py` | `rmp test property`, and every night | Hypothesis |
| Mutation testing of the Python | `rmp test mutation`, every Sunday | mutmut, which drives pytest |
| The asset makers, `tools/make_example_art.py` and `tools/make_platformer_assets.py` | by hand, when an example's art is remade | Pillow |
| CI's own scripts, `.github/scripts/` | the canary and its report | the standard library |

## Why 3.11

The `.toml` is read with `tomllib`, which Python has had since 3.11, so that is the floor. `tools/rmp.py` is kept parseable by 3.8, so an older Python prints a sentence saying which version it is instead of a syntax error.

CMake does not take the first Python it finds. `cmake/find_python.cmake` asks each name in turn — `python3`, then the versioned names, then `python` and `py` — to import `tomllib`, and takes the first that can: on Windows `python3.exe` is often the Store's stub, and macOS's own `python3` is older than 3.11. The versioned names stop at the newest Python that existed when the list was written; a newer one is found through `python3`, which is what every system installs it as. Add its versioned name to the list, and to the POSIX launcher's, which is the same, when one is released.

## Standard library only

The generator says it in its first lines: standard library only, Pillow optional. The rule is what lets a game made today configure on a Python of years from now without a package manager. The standard library modules it and `rmp` use are core ones — `tomllib`, `json`, `re`, `pathlib`, `subprocess`, `argparse`, `hashlib`, `struct`, `shutil` — and the POSIX ones, `fcntl`, `pwd` and `grp`, imported inside the functions that only run on Linux, as `winreg` is inside the ones that only run on Windows.

Keeping it so Python removes old standard library modules after a deprecation of a few releases. None the build uses is on that path. When a new Python is released, run `rmp test configure` and `rmp test rmp` under it: those are the generator's and the command's own tests, and they need nothing installed.

## The packages that are not

| Package | Used for | In the build image | On a laptop without it |
|---|---|---|---|
| Pillow | the icons: the Windows `.ico`, Android's launcher icons, iOS's; the asset makers | from apt, at the snapshot's version | no icon is made, and the build goes on. The Windows jobs install it by hash (`pillow` and its `pillow_sha256_*` keys) |
| PyYAML | the workflow check, and the tests that read a workflow as YAML | from apt | skipped, and said; inside the image a missing PyYAML fails instead |
| Hypothesis | the Python properties | from the lock | each property module says so and is skipped; CI requires it, so a skip there is a failure |
| mutmut and pytest | mutation testing of the Python | from the lock | the mutation stage fails: it runs only in the image |
| clang-format, clang-tidy | formatting and linting; they are Python wheels that carry the binaries | from PyPI, by version | `pip install clang-format==… clang-tidy==…` at the `clang_format` version ([Setting up](https://omardev29.github.io/rmp-docs/framework/contributing/setup.html#clang-tools)) |

The clang tools are pinned by version and not by hash: the image installs them with `pip install` and the version, and `tools/versions_check.sh` compares the binary on `PATH` with the pin. A wheel republished under the same version would not be noticed. That is the one Python install in the image without a hash.

## The image's lock

The image's test tools come from two files in the image repository: `python/requirements-ci.in`, the top-level pins, and `python/requirements-ci.txt`, the lock made from it by `python/lock.py`, which the image installs with `--require-hashes --only-binary=:all:`. The lock pins the whole graph with `==` and keeps, for each package, the hash of every wheel pip can pick for the image's CPython on both of its architectures — downloaded and hashed by `lock.py` and checked against the digest PyPI publishes.

Three things make it harder than a plain lock, and each is why a regeneration fails:

1. **Two pins belong to apt.** PyYAML and Pygments are installed by apt (Pygments as a dependency of cppcheck), and pip cannot uninstall a Debian package, so the lock pins them to the exact versions the apt snapshot has. Move the snapshot and those two may move: read the new versions from the image (`dpkg -s python3-yaml python3-pygments`) and put them in `requirements-ci.in` before locking. Everything that depends on them is held back with them — that is why mutmut's interface library is not at its newest.
2. **Wheels only, for two architectures.** A release that has no wheel for aarch64, or that only ships a source archive, cannot be locked. `lock.py` refuses it; pin the newest release that has both.
3. **It is made inside the image.** The interpreter the lock is for is the image's, so `lock.py` runs in a build of the image, with the network, and pip-tools in a throw-away virtual environment. The command is in its docstring and in the image's README.

### Updating Hypothesis, mutmut or pytest

1. In the image repository, change the version in `python/requirements-ci.in`.
2. Regenerate the lock inside the current image:
   ```shell
   docker run --rm -v "$PWD":/w -w /w ghcr.io/omardev29/raylib-build:latest sh -c '
       apt-get update -qq && apt-get install -y -qq python3-venv >/dev/null &&
       python3 -m venv /tmp/v && /tmp/v/bin/pip install -q pip-tools==7.6.2 &&
       /tmp/v/bin/python python/lock.py'
   ```
3. Read the diff of `python/requirements-ci.txt`: every package that moved is a change in what the tests run.
4. Build the image ([the build image](https://omardev29.github.io/rmp-docs/framework/maintaining/build-image.html#bump)), whose smoke check runs a Hypothesis property and a mutmut mutant before it publishes.
5. In the framework, the same versions in the `hypothesis`, `mutmut` and `pytest` keys of `thirdparty/FROZEN_VERSIONS.md`, and the new digest.
6. Run the properties and one mutation shard in the new image. A new Hypothesis can find a counterexample the old one never generated: that is a bug found, and it is fixed in the code.

## This site's Python

This site is built by one standard library script, `build.py` in its `tools` folder, with the same floor. Its property tests need Hypothesis and its browser checks a hash-pinned requirements file of their own, beside them in its tests; neither is needed to build the site.

## If the test tools stop installing

They are a test campaign, not the build. If, years from now, mutmut or Hypothesis no longer installs on any Python the image can carry, the generator, `rmp`, every gate that is standard library only and every game are untouched: what stops is the properties of the tools and the mutation scores of the Python. Keep the last image that has them, by its digest, for the day a change needs them, and run the rest of the ladder on a newer one. What the floors and the waivers mean is in [Properties, fuzzing and mutation](https://omardev29.github.io/rmp-docs/framework/contributing/properties-fuzzing-mutation.html).
