8000 gh-107954: Allow setting cpu_count in PyConfig_Set() by vstinner · Pull Request #132954 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-107954: Allow setting cpu_count in PyConfig_Set() #132954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/c-api/init_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ Configuration Options
* - ``"cpu_count"``
- :c:member:`cpu_count <PyConfig.cpu_count>`
- ``int``
- Read-only
- Public
* - ``"dev_mode"``
- :c:member:`dev_mode <PyConfig.dev_mode>`
- ``bool``
Expand Down
14 changes: 13 additions & 1 deletion Lib/test/test_capi/test_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Tests PyConfig_Get() and PyConfig_Set() C API (PEP 741).
"""
import os
import sys
import sysconfig
import types
Expand Down Expand Up @@ -374,12 +375,23 @@ def expect_bool_not(value):
finally:
config_set(name, old_value)

def test_config_set_cpu_count(self):
config_get = _testcapi.config_get
config_set = _testcapi.config_set

old_value = config_get('cpu_count')
try:
config_set('cpu_count', 123)
self.assertEqual(os.cpu_count(), 123)
finally:
config_set('cpu_count', old_value)

def test_config_set_read_only(self):
# Test PyConfig_Set() on read-only options
config_set = _testcapi.config_set
for name, value in (
("allocator", 0), # PyPreConfig member
("cpu_count", 8),
("perf_profiling", 8),
("dev_mode", True),
("filesystem_encoding", "utf-8"),
):
Expand Down
30 changes: 23 additions & 7 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ static const PyConfigSpec PYCONFIG_SPEC[] = {
SPEC(base_executable, WSTR_OPT, PUBLIC, SYS_ATTR("_base_executable")),
SPEC(base_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("base_prefix")),
SPEC(bytes_warning, UINT, PUBLIC, SYS_FLAG(9)),
SPEC(cpu_count, INT, PUBLIC, NO_SYS),
SPEC(exec_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("exec_prefix")),
SPEC(executable, WSTR_OPT, PUBLIC, SYS_ATTR("executable")),
SPEC(inspect, BOOL, PUBLIC, SYS_FLAG(1)),
Expand Down Expand Up @@ -138,7 +139,6 @@ static const PyConfigSpec PYCONFIG_SPEC[] = {
SPEC(check_hash_pycs_mode, WSTR, READ_ONLY, NO_SYS),
SPEC(code_debug_ranges, BOOL, READ_ONLY, NO_SYS),
SPEC(configure_c_stdio, BOOL, READ_ONLY, NO_SYS),
SPEC(cpu_count, INT, READ_ONLY, NO_SYS),
SPEC(dev_mode, BOOL, READ_ONLY, NO_SYS), // sys.flags.dev_mode
SPEC(dump_refs, BOOL, READ_ONLY, NO_SYS),
SPEC(dump_refs_file, WSTR_OPT, READ_ONLY, NO_SYS),
Expand Down Expand Up @@ -4524,6 +4524,18 @@ config_set_sys_flag(const PyConfigSpec *spec, int int_value)
}


// Set PyConfig.ATTR integer member
static int
config_set_int_attr(const PyConfigSpec *spec, int value)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
PyConfig *config = &interp->config;
int *member = config_get_spec_member(config, spec);
*member = value;
return 0;
}


int
PyConfig_Set(const char *name, PyObject *value)
{
Expand Down Expand Up @@ -4632,17 +4644,21 @@ PyConfig_Set(const char *name, PyObject *value)
Py_UNREACHABLE();
}


if (spec->sys.attr != NULL) {
// Set the sys attribute, but don't set PyInterpreterState.config
// to keep the code simple.
return PySys_SetObject(spec->sys.attr, value);
}
else if (spec->sys.flag_index >= 0 && has_int_value) {
return config_set_sys_flag(spec, int_value);
}
else if (strcmp(spec->name, "int_max_str_digits") == 0 && has_int_value) {
return _PySys_SetIntMaxStrDigits(int_value);
else if (has_int_value) {
if (spec->sys.flag_index >= 0) {
return config_set_sys_flag(spec, int_value);
}
else if (strcmp(spec->name, "int_max_str_digits") == 0) {
return _PySys_SetIntMaxStrDigits(int_value);
}
else {
return config_set_int_attr(spec, int_value);
}
}

cannot_set:
Expand Down
Loading
0