There seems to have been a bug in the libregrtest code which unloaded
modules between tests. The previous state was calculated using
`sys.modules.keys()`, which is actually a mutable object that is updated
as the underlying `sys.modules` is updated. The result was that modules
were not unloaded between tests, which is the root cause for
`test_unittest` failing when run after `test_import` and
`test_importlib`.
This code is copied from 3.12. Ideally all of `libregrtest` should
probably be updated as it seems wildly out of date, but that's a lot
more work.