gh-36407: Support python 3.12
This PR adds support for running with python 3.12 from system. This has
been tested on the python 3.12 branch of void linux (x86_64, x86_64-musl
and i686).
The first two commits correspond to #36403. The rest is split is small
pieces and I tried to add reasonable explanations in the commit
messages. Reviewing by commit may be easier (ignoring the first two,
already reviewed).
See also: #36181
### :hourglass: Dependencies
#36403
URL: https://github.com/sagemath/sage/pull/36407
Reported by: Gonzalo Tornaría
Reviewer(s): Matthias Köppe