diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-08-07 19:29:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-07 19:29:01 (GMT) |
commit | bea5f93196d213d6fbf4ba8984caf4c3cd1da882 (patch) | |
tree | ba71488824126a2e2aef7f3d42d8092a175930df /Modules/_testcapimodule.c | |
parent | 430632d6f710c99879c5d1736f3b40ea09b11c4d (diff) | |
download | cpython-bea5f93196d213d6fbf4ba8984caf4c3cd1da882.zip cpython-bea5f93196d213d6fbf4ba8984caf4c3cd1da882.tar.gz cpython-bea5f93196d213d6fbf4ba8984caf4c3cd1da882.tar.bz2 |
gh-107735: Add C API tests for PySys_GetObject() and PySys_SetObject() (GH-107736)
Diffstat (limited to 'Modules/_testcapimodule.c')
-rw-r--r-- | Modules/_testcapimodule.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 23dd2cc..35599f8 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -44,6 +44,16 @@ // Include definitions from there. #include "_testcapi/parts.h" +#define NULLABLE(x) do { if (x == Py_None) x = NULL; } while (0); + +#define RETURN_INT(value) do { \ + int _ret = (value); \ + if (_ret == -1) { \ + return NULL; \ + } \ + return PyLong_FromLong(_ret); \ + } while (0) + // Forward declarations static struct PyModuleDef _testcapimodule; static PyObject *TestError; /* set to exception object in init */ @@ -3505,6 +3515,35 @@ error: } +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 PyMethodDef TestMethods[] = { {"set_errno", set_errno, METH_VARARGS}, {"test_config", test_config, METH_NOARGS}, @@ -3640,6 +3679,8 @@ static PyMethodDef TestMethods[] = { {"check_pyimport_addmodule", check_pyimport_addmodule, METH_VARARGS}, {"test_weakref_capi", test_weakref_capi, METH_NOARGS}, {"test_dict_capi", test_dict_capi, METH_NOARGS}, + {"sys_getobject", sys_getobject, METH_O}, + {"sys_setobject", sys_setobject, METH_VARARGS}, {NULL, NULL} /* sentinel */ }; |