summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_capi.py
diff options
context:
space:
mode:
authorHai Shi <shihai1992@gmail.com>2021-10-05 13:19:32 (GMT)
committerGitHub <noreply@github.com>2021-10-05 13:19:32 (GMT)
commitb9bb74871b27d9226df2dd3fce9d42bda8b43c2b (patch)
tree09af0bf4e5971a4ccaa0eec5a1060212e8fce2bc /Lib/test/test_capi.py
parent5146877623ebe8a2806411703b0de9c0aba179a1 (diff)
downloadcpython-b9bb74871b27d9226df2dd3fce9d42bda8b43c2b.zip
cpython-b9bb74871b27d9226df2dd3fce9d42bda8b43c2b.tar.gz
cpython-b9bb74871b27d9226df2dd3fce9d42bda8b43c2b.tar.bz2
bpo-44050: Extension modules can share state when they don't support sub-interpreters. (GH-27794)
Automerge-Triggered-By: GH:encukou
Diffstat (limited to 'Lib/test/test_capi.py')
-rw-r--r--Lib/test/test_capi.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index db029ef..bdb8f76 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -766,6 +766,37 @@ class SubinterpreterTest(unittest.TestCase):
self.assertFalse(hasattr(binascii.Error, "foobar"))
+ def test_module_state_shared_in_global(self):
+ """
+ bpo-44050: Extension module state should be shared between interpreters
+ when it doesn't support sub-interpreters.
+ """
+ r, w = os.pipe()
+ self.addCleanup(os.close, r)
+ self.addCleanup(os.close, w)
+
+ script = textwrap.dedent(f"""
+ import importlib.machinery
+ import importlib.util
+ import os
+
+ fullname = '_test_module_state_shared'
+ origin = importlib.util.find_spec('_testmultiphase').origin
+ loader = importlib.machinery.ExtensionFileLoader(fullname, origin)
+ spec = importlib.util.spec_from_loader(fullname, loader)
+ module = importlib.util.module_from_spec(spec)
+ attr_id = str(id(module.Error)).encode()
+
+ os.write({w}, attr_id)
+ """)
+ exec(script)
+ main_attr_id = os.read(r, 100)
+
+ ret = support.run_in_subinterp(script)
+ self.assertEqual(ret, 0)
+ subinterp_attr_id = os.read(r, 100)
+ self.assertEqual(main_attr_id, subinterp_attr_id)
+
class TestThreadState(unittest.TestCase):