summaryrefslogtreecommitdiffstats
path: root/Modules/_testcapi
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2023-10-25 08:33:12 (GMT)
committerGitHub <noreply@github.com>2023-10-25 08:33:12 (GMT)
commitb2ba2985275d1200e5c44c3f224d754141fc5292 (patch)
treee1610ec77d986d4eb34c24a598fc2260f4cf960d /Modules/_testcapi
parent0d1cbff833f761f80383f4ce5fe31f686f3f04eb (diff)
downloadcpython-b2ba2985275d1200e5c44c3f224d754141fc5292.zip
cpython-b2ba2985275d1200e5c44c3f224d754141fc5292.tar.gz
cpython-b2ba2985275d1200e5c44c3f224d754141fc5292.tar.bz2
gh-111065: Add more tests for the C API with the PySys_ prefix (GH-111067)
* Move existing tests for PySys_GetObject() and PySys_SetObject() into specialized files. * Add test for PySys_GetXOptions() using _testcapi. * Add tests for PySys_FormatStdout(), PySys_FormatStderr(), PySys_WriteStdout() and PySys_WriteStderr() using ctypes.
Diffstat (limited to 'Modules/_testcapi')
-rw-r--r--Modules/_testcapi/parts.h1
-rw-r--r--Modules/_testcapi/sys.c56
2 files changed, 57 insertions, 0 deletions
diff --git a/Modules/_testcapi/parts.h b/Modules/_testcapi/parts.h
index e5dbb9c..4fa77a8 100644
--- a/Modules/_testcapi/parts.h
+++ b/Modules/_testcapi/parts.h
@@ -49,6 +49,7 @@ int _PyTestCapi_Init_PyAtomic(PyObject *module);
int _PyTestCapi_Init_PyOS(PyObject *module);
int _PyTestCapi_Init_Immortal(PyObject *module);
int _PyTestCapi_Init_GC(PyObject *mod);
+int _PyTestCapi_Init_Sys(PyObject *);
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
int _PyTestCapi_Init_HeaptypeRelative(PyObject *module);
diff --git a/Modules/_testcapi/sys.c b/Modules/_testcapi/sys.c
new file mode 100644
index 0000000..aa40e3c
--- /dev/null
+++ b/Modules/_testcapi/sys.c
@@ -0,0 +1,56 @@
+#include "parts.h"
+#include "util.h"
+
+
+static PyObject *
+sys_getobject(PyObject *Py_UNUSED(module), PyObject *arg)
+{
+ const char *name;
+ Py_ssize_t size;
+ if (!PyArg_Parse(arg, "z#", &name, &size)) {
+ return NULL;
+ }
+ PyObject *result = PySys_GetObject(name);
+ if (result == NULL) {
+ result = PyExc_AttributeError;
+ }
+ return Py_NewRef(result);
+}
+
+static PyObject *
+sys_setobject(PyObject *Py_UNUSED(module), PyObject *args)
+{
+ const char *name;
+ Py_ssize_t size;
+ PyObject *value;
+ if (!PyArg_ParseTuple(args, "z#O", &name, &size, &value)) {
+ return NULL;
+ }
+ NULLABLE(value);
+ RETURN_INT(PySys_SetObject(name, value));
+}
+
+static PyObject *
+sys_getxoptions(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(ignored))
+{
+ PyObject *result = PySys_GetXOptions();
+ return Py_XNewRef(result);
+}
+
+
+static PyMethodDef test_methods[] = {
+ {"sys_getobject", sys_getobject, METH_O},
+ {"sys_setobject", sys_setobject, METH_VARARGS},
+ {"sys_getxoptions", sys_getxoptions, METH_NOARGS},
+ {NULL},
+};
+
+int
+_PyTestCapi_Init_Sys(PyObject *m)
+{
+ if (PyModule_AddFunctions(m, test_methods) < 0) {
+ return -1;
+ }
+
+ return 0;
+}