[3.9] bpo-44050: Extension modules can share state when they don't support sub-interpreters. (GH-27794) by ambv · Pull Request #28741 · python/cpython
(cherry picked from commit b9bb748)
Co-authored-by: Hai Shi shihai1992@gmail.com
Note: this is a little different from 3.10 and 3.11 because in 3.10+ _Py_IsMainInterpreter takes PyInterpreterState instead of the entire PyThreadState as an argument (see GH-24577 for that).
Also, the new test uses PyModule_AddObjectRef which was only added in Python 3.10 (see GH-23122). I replaced this usage with the old PyModule_AddObject with additional increfs and decrefs per what BPO-42327 prescribes.