Skip to content

Commit

Permalink
Address Victor's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Sep 24, 2023
1 parent f0a3ebf commit 89d8bb2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
4 changes: 4 additions & 0 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 89d8bb2

Please sign in to comment.