aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_cmd_line.py
diff options
context:
space:
mode:
authorBrett Simmers <swtaarrs@users.noreply.github.com>2024-03-11 11:02:58 -0400
committerGitHub <noreply@github.com>2024-03-11 11:02:58 -0400
commit2731913dd5234ff5ab630a3b7f1c98ad79d4d9df (patch)
treec666bae1112581bf0fe4c23ced8d188082cbcefd /Lib/test/test_cmd_line.py
parent546eb7a3be241c5abd8a83cebbbab8c71107edcf (diff)
downloadcpython-2731913dd5234ff5ab630a3b7f1c98ad79d4d9df.tar.gz
cpython-2731913dd5234ff5ab630a3b7f1c98ad79d4d9df.zip
gh-116167: Allow disabling the GIL with `PYTHON_GIL=0` or `-X gil=0` (#116338)
In free-threaded builds, running with `PYTHON_GIL=0` will now disable the GIL. Follow-up issues track work to re-enable the GIL when loading an incompatible extension, and to disable the GIL by default. In order to support re-enabling the GIL at runtime, all GIL-related data structures are initialized as usual, and disabling the GIL simply sets a flag that causes `take_gil()` and `drop_gil()` to return early.
Diffstat (limited to 'Lib/test/test_cmd_line.py')
-rw-r--r--Lib/test/test_cmd_line.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py
index 6796dc6e357..c633f6493cf 100644
--- a/Lib/test/test_cmd_line.py
+++ b/Lib/test/test_cmd_line.py
@@ -869,6 +869,39 @@ class CmdLineTest(unittest.TestCase):
self.assertEqual(proc.stdout.rstrip(), 'True')
self.assertEqual(proc.returncode, 0, proc)
+ @unittest.skipUnless(support.Py_GIL_DISABLED,
+ "PYTHON_GIL and -X gil only supported in Py_GIL_DISABLED builds")
+ def test_python_gil(self):
+ cases = [
+ # (env, opt, expected, msg)
+ (None, None, 'None', "no options set"),
+ ('0', None, '0', "PYTHON_GIL=0"),
+ ('1', None, '1', "PYTHON_GIL=1"),
+ ('1', '0', '0', "-X gil=0 overrides PYTHON_GIL=1"),
+ (None, '0', '0', "-X gil=0"),
+ (None, '1', '1', "-X gil=1"),
+ ]
+
+ code = "import sys; print(sys.flags.gil)"
+ environ = dict(os.environ)
+
+ for env, opt, expected, msg in cases:
+ with self.subTest(msg, env=env, opt=opt):
+ environ.pop('PYTHON_GIL', None)
+ if env is not None:
+ environ['PYTHON_GIL'] = env
+ extra_args = []
+ if opt is not None:
+ extra_args = ['-X', f'gil={opt}']
+
+ proc = subprocess.run([sys.executable, *extra_args, '-c', code],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ text=True, env=environ)
+ self.assertEqual(proc.returncode, 0, proc)
+ self.assertEqual(proc.stdout.rstrip(), expected)
+ self.assertEqual(proc.stderr, '')
+
@unittest.skipUnless(sys.platform == 'win32',
'bpo-32457 only applies on Windows')
def test_argv0_normalization(self):