gh-107954: Allow setting cpu_count in PyConfig_Set() (#132954)
* gh-107954: Allow setting cpu_count in PyConfig_Set() * Update the doc
This commit is contained in:
parent
17718b0503
commit
070d866567
@ -320,7 +320,7 @@ Configuration Options
|
|||||||
* - ``"cpu_count"``
|
* - ``"cpu_count"``
|
||||||
- :c:member:`cpu_count <PyConfig.cpu_count>`
|
- :c:member:`cpu_count <PyConfig.cpu_count>`
|
||||||
- ``int``
|
- ``int``
|
||||||
- Read-only
|
- Public
|
||||||
* - ``"dev_mode"``
|
* - ``"dev_mode"``
|
||||||
- :c:member:`dev_mode <PyConfig.dev_mode>`
|
- :c:member:`dev_mode <PyConfig.dev_mode>`
|
||||||
- ``bool``
|
- ``bool``
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""
|
"""
|
||||||
Tests PyConfig_Get() and PyConfig_Set() C API (PEP 741).
|
Tests PyConfig_Get() and PyConfig_Set() C API (PEP 741).
|
||||||
"""
|
"""
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import sysconfig
|
import sysconfig
|
||||||
import types
|
import types
|
||||||
@ -374,12 +375,23 @@ class CAPITests(unittest.TestCase):
|
|||||||
finally:
|
finally:
|
||||||
config_set(name, old_value)
|
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):
|
def test_config_set_read_only(self):
|
||||||
# Test PyConfig_Set() on read-only options
|
# Test PyConfig_Set() on read-only options
|
||||||
config_set = _testcapi.config_set
|
config_set = _testcapi.config_set
|
||||||
for name, value in (
|
for name, value in (
|
||||||
("allocator", 0), # PyPreConfig member
|
("allocator", 0), # PyPreConfig member
|
||||||
("cpu_count", 8),
|
("perf_profiling", 8),
|
||||||
("dev_mode", True),
|
("dev_mode", True),
|
||||||
("filesystem_encoding", "utf-8"),
|
("filesystem_encoding", "utf-8"),
|
||||||
):
|
):
|
||||||
|
@ -110,6 +110,7 @@ static const PyConfigSpec PYCONFIG_SPEC[] = {
|
|||||||
SPEC(base_executable, WSTR_OPT, PUBLIC, SYS_ATTR("_base_executable")),
|
SPEC(base_executable, WSTR_OPT, PUBLIC, SYS_ATTR("_base_executable")),
|
||||||
SPEC(base_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("base_prefix")),
|
SPEC(base_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("base_prefix")),
|
||||||
SPEC(bytes_warning, UINT, PUBLIC, SYS_FLAG(9)),
|
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(exec_prefix, WSTR_OPT, PUBLIC, SYS_ATTR("exec_prefix")),
|
||||||
SPEC(executable, WSTR_OPT, PUBLIC, SYS_ATTR("executable")),
|
SPEC(executable, WSTR_OPT, PUBLIC, SYS_ATTR("executable")),
|
||||||
SPEC(inspect, BOOL, PUBLIC, SYS_FLAG(1)),
|
SPEC(inspect, BOOL, PUBLIC, SYS_FLAG(1)),
|
||||||
@ -138,7 +139,6 @@ static const PyConfigSpec PYCONFIG_SPEC[] = {
|
|||||||
SPEC(check_hash_pycs_mode, WSTR, READ_ONLY, NO_SYS),
|
SPEC(check_hash_pycs_mode, WSTR, READ_ONLY, NO_SYS),
|
||||||
SPEC(code_debug_ranges, BOOL, READ_ONLY, NO_SYS),
|
SPEC(code_debug_ranges, BOOL, READ_ONLY, NO_SYS),
|
||||||
SPEC(configure_c_stdio, 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(dev_mode, BOOL, READ_ONLY, NO_SYS), // sys.flags.dev_mode
|
||||||
SPEC(dump_refs, BOOL, READ_ONLY, NO_SYS),
|
SPEC(dump_refs, BOOL, READ_ONLY, NO_SYS),
|
||||||
SPEC(dump_refs_file, WSTR_OPT, READ_ONLY, NO_SYS),
|
SPEC(dump_refs_file, WSTR_OPT, READ_ONLY, NO_SYS),
|
||||||
@ -4524,6 +4524,18 @@ error:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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
|
int
|
||||||
PyConfig_Set(const char *name, PyObject *value)
|
PyConfig_Set(const char *name, PyObject *value)
|
||||||
{
|
{
|
||||||
@ -4632,17 +4644,21 @@ PyConfig_Set(const char *name, PyObject *value)
|
|||||||
Py_UNREACHABLE();
|
Py_UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (spec->sys.attr != NULL) {
|
if (spec->sys.attr != NULL) {
|
||||||
// Set the sys attribute, but don't set PyInterpreterState.config
|
// Set the sys attribute, but don't set PyInterpreterState.config
|
||||||
// to keep the code simple.
|
// to keep the code simple.
|
||||||
return PySys_SetObject(spec->sys.attr, value);
|
return PySys_SetObject(spec->sys.attr, value);
|
||||||
}
|
}
|
||||||
else if (spec->sys.flag_index >= 0 && has_int_value) {
|
else if (has_int_value) {
|
||||||
return config_set_sys_flag(spec, 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 && has_int_value) {
|
}
|
||||||
return _PySys_SetIntMaxStrDigits(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:
|
cannot_set:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user