summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-09-02 21:25:08 (GMT)
committerGitHub <noreply@github.com>2024-09-02 21:25:08 (GMT)
commit33b790978d8b817a66a4a117a8c38a857b6103f0 (patch)
treeea11817b7267715e2b19e944e42233c7a4a28399 /Modules
parentdb42934270c5c23be9f6804cad98dfd8234caf6f (diff)
downloadcpython-33b790978d8b817a66a4a117a8c38a857b6103f0.zip
cpython-33b790978d8b817a66a4a117a8c38a857b6103f0.tar.gz
cpython-33b790978d8b817a66a4a117a8c38a857b6103f0.tar.bz2
gh-107954, PEP 741: Add PyConfig_Get()/Set() functions (#123472)
Add PyConfig_Get(), PyConfig_GetInt(), PyConfig_Set() and PyConfig_Names() functions to get and set the current runtime Python configuration. Add visibility and "sys spec" to config and preconfig specifications. _PyConfig_AsDict() now converts PyConfig.xoptions as a dictionary. Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/Setup.stdlib.in2
-rw-r--r--Modules/_testcapi/config.c68
-rw-r--r--Modules/_testcapi/parts.h1
-rw-r--r--Modules/_testcapimodule.c3
4 files changed, 73 insertions, 1 deletions
diff --git a/Modules/Setup.stdlib.in b/Modules/Setup.stdlib.in
index 9121a8c..9aa398a 100644
--- a/Modules/Setup.stdlib.in
+++ b/Modules/Setup.stdlib.in
@@ -162,7 +162,7 @@
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
-@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/monitoring.c
+@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/monitoring.c _testcapi/config.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/tuple.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c
diff --git a/Modules/_testcapi/config.c b/Modules/_testcapi/config.c
new file mode 100644
index 0000000..bb3b7e8
--- /dev/null
+++ b/Modules/_testcapi/config.c
@@ -0,0 +1,68 @@
+#include "parts.h"
+
+
+static PyObject *
+_testcapi_config_get(PyObject *module, PyObject *name_obj)
+{
+ const char *name;
+ if (PyArg_Parse(name_obj, "s", &name) < 0) {
+ return NULL;
+ }
+
+ return PyConfig_Get(name);
+}
+
+
+static PyObject *
+_testcapi_config_getint(PyObject *module, PyObject *name_obj)
+{
+ const char *name;
+ if (PyArg_Parse(name_obj, "s", &name) < 0) {
+ return NULL;
+ }
+
+ int value;
+ if (PyConfig_GetInt(name, &value) < 0) {
+ return NULL;
+ }
+ return PyLong_FromLong(value);
+}
+
+
+static PyObject *
+_testcapi_config_names(PyObject *module, PyObject* Py_UNUSED(args))
+{
+ return PyConfig_Names();
+}
+
+
+static PyObject *
+_testcapi_config_set(PyObject *module, PyObject *args)
+{
+ const char *name;
+ PyObject *value;
+ if (PyArg_ParseTuple(args, "sO", &name, &value) < 0) {
+ return NULL;
+ }
+
+ int res = PyConfig_Set(name, value);
+ if (res < 0) {
+ return NULL;
+ }
+ Py_RETURN_NONE;
+}
+
+
+static PyMethodDef test_methods[] = {
+ {"config_get", _testcapi_config_get, METH_O},
+ {"config_getint", _testcapi_config_getint, METH_O},
+ {"config_names", _testcapi_config_names, METH_NOARGS},
+ {"config_set", _testcapi_config_set, METH_VARARGS},
+ {NULL}
+};
+
+int
+_PyTestCapi_Init_Config(PyObject *mod)
+{
+ return PyModule_AddFunctions(mod, test_methods);
+}
diff --git a/Modules/_testcapi/parts.h b/Modules/_testcapi/parts.h
index 41d1909..65ba775 100644
--- a/Modules/_testcapi/parts.h
+++ b/Modules/_testcapi/parts.h
@@ -60,5 +60,6 @@ int _PyTestCapi_Init_Hash(PyObject *module);
int _PyTestCapi_Init_Time(PyObject *module);
int _PyTestCapi_Init_Monitoring(PyObject *module);
int _PyTestCapi_Init_Object(PyObject *module);
+int _PyTestCapi_Init_Config(PyObject *mod);
#endif // Py_TESTCAPI_PARTS_H
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 7ffa87d..5966eb6 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -4172,6 +4172,9 @@ PyInit__testcapi(void)
if (_PyTestCapi_Init_Object(m) < 0) {
return NULL;
}
+ if (_PyTestCapi_Init_Config(m) < 0) {
+ return NULL;
+ }
PyState_AddModule(m, &_testcapimodule);
return m;