From 2ac6901e4351a837180d928b96e460bd53aafe3c Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Fri, 22 Sep 2023 08:24:13 +0900 Subject: [PATCH] Address code review --- Doc/using/cmdline.rst | 2 +- Doc/whatsnew/3.13.rst | 2 +- Modules/posixmodule.c | 1 + Python/initconfig.c | 3 ++- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index b939302286ed83..4e170ccb230b42 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -547,7 +547,7 @@ Miscellaneous options will do nothing if is not supported on the current system. The default value is "off". See also :envvar:`PYTHONPERFSUPPORT` and :ref:`perf_profiling`. * :samp:`-X cpu_count={n}` overrides :func:`os.cpu_count`. - And *n* must be greater than 0. + *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`. diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index 629858fa48de91..2550ecd2cc7b09 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -167,7 +167,7 @@ os -- * :func:`os.cpu_count` can be overrided through the new - environment variable :envvar:`PYTHONCPUCOUNT`, the new command-line option + environment variable :envvar:`PYTHONCPUCOUNT` or the new command-line option :option:`-X cpu_count <-X>`. This option is useful for users who need to limit CPU resources of a container system.(Contributed by Donghee Na in :gh:`109595`) diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 78c261e134e01d..4b9b03510d7795 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -14353,6 +14353,7 @@ os_cpu_count_impl(PyObject *module) if (config->cpu_count > 0) { return PyLong_FromLong(config->cpu_count); } + int ncpu = 0; #ifdef MS_WINDOWS #ifdef MS_WINDOWS_DESKTOP diff --git a/Python/initconfig.c b/Python/initconfig.c index d5966cef60b059..d38cadcaadc33f 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -1690,9 +1690,9 @@ config_read_env_vars(PyConfig *config) static PyStatus config_init_cpu_count(PyConfig *config) { - int cpu_count = -1; const char *env = config_get_env(config, "PYTHONCPUCOUNT"); if (env) { + int cpu_count = -1; if (_Py_str_to_int(env, &cpu_count) != 0) { cpu_count = -1; } @@ -1702,6 +1702,7 @@ config_init_cpu_count(PyConfig *config) } const wchar_t *xoption = config_get_xoption(config, L"cpu_count"); if (xoption) { + int cpu_count = -1; const wchar_t *sep = wcschr(xoption, L'='); if (sep) { if (config_wstr_to_int(sep + 1, &cpu_count) < 0 || cpu_count < 1) {