This is a similar change to gh-102925, but applied to the global cache of PyModuleDefs instead of the interned strings. Note that the cache is only used for legacy extension modules and not for multi-phase init modules.
(Until gh-102925 is merged, this is based on top of it.)