diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index 4e170ccb230b42..5c3ae98b54f6ca 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -550,6 +550,8 @@ Miscellaneous options *n* must be greater than or equal to 1. This option is useful for users who need to limit CPU resources of a container system. See also :envvar:`PYTHONCPUCOUNT`. + If *n* is "default", it follows default :func:`os.cpu_count` even if :envvar:`PYTHONCPUCOUNT` + is set. It also allows passing arbitrary values and retrieving them through the diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 0ad431c40c446b..cb178c13481f52 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -906,6 +906,11 @@ def test_cpu_count(self): res = assert_python_ok('-X', 'cpu_count=4321', '-c', code) self.assertEqual(res.out.strip().decode("utf-8"), '4321') + def test_cpu_count_default(self): + code = "import os; print(os.cpu_count())" + res = assert_python_ok('-X', 'cpu_count=default', '-c', code) + self.assertEqual(int(res.out.strip().decode("utf-8")), os.cpu_count()) + @unittest.skipIf(interpreter_requires_environment(), 'Cannot run -I tests when PYTHON env vars are required.') diff --git a/Python/initconfig.c b/Python/initconfig.c index 29df4003112d74..6e3cca142a4699 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -1706,6 +1706,10 @@ config_init_cpu_count(PyConfig *config) int cpu_count = -1; const wchar_t *sep = wcschr(xoption, L'='); if (sep) { + if (wcscmp(sep + 1, L"default") == 0) { + config->cpu_count = -1; + return _PyStatus_OK(); + } if (config_wstr_to_int(sep + 1, &cpu_count) < 0 || cpu_count < 1) { goto error; }