cherry-picked from #32432
Depends on #32637
Depends on #32665
CC: @kliem
Component: refactoring
Author: Matthias Koeppe
Branch/Commit: 8679256
Reviewer: Jonathan Kliem
Issue created by migration from https://trac.sagemath.org/ticket/32725