From faf0592d54a20fc2bcc48dc11c9b1e10e7fe5412 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Wed, 1 Feb 2023 18:16:20 +0300 Subject: [PATCH 1/3] Add a boolean format option to do_mkvalue --- Modules/_testcapimodule.c | 87 ++++++++++++++++++++++++++++++++++++++- Python/modsupport.c | 7 ++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 46c025bf53404a..8f00eacbfdd49b 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -2149,7 +2149,7 @@ dict_get_version(PyObject *self, PyObject *args) return NULL; _Py_COMP_DIAG_PUSH - _Py_COMP_DIAG_IGNORE_DEPR_DECLS + _Py_COMP_DIAG_IGNORE_DEPR_DECLS version = dict->ma_version_tag; _Py_COMP_DIAG_POP @@ -3244,6 +3244,90 @@ function_set_kw_defaults(PyObject *self, PyObject *args) Py_RETURN_NONE; } +static PyObject * +test_py_buildvalue(PyObject *self) { + PyObject *test_var; + + test_var = Py_BuildValue("(i,i,i)", 2,3,4); + if (!PyTuple_CheckExact(test_var)) { + PyErr_SetString(TestError, "Failed to Create tuble"); + return NULL; + } + + test_var = Py_BuildValue("[i,i,i]", 2,3,4); + if (!PyList_CheckExact(test_var)) { + PyErr_SetString(TestError, "Failed to Create list"); + return NULL; + } + + test_var = Py_BuildValue("{s:i}", "test", 1); + if (!PyDict_CheckExact(test_var)) { + PyErr_SetString(TestError, "Failed to Create dict"); + return NULL; + } + + test_var = Py_BuildValue("b", -1); + if (!PyLong_CheckExact(test_var)) { + PyErr_SetString(TestError, "Failed to Create long"); + return NULL; + } + + test_var = Py_BuildValue("B", -1); + if (!PyLong_CheckExact(test_var)) { + PyErr_SetString(TestError, "Failed to Create long"); + return NULL; + } + + test_var = Py_BuildValue("H", -1); + if (!PyLong_CheckExact(test_var)) { + PyErr_SetString(TestError, "Failed to Create long"); + return NULL; + } + + test_var = Py_BuildValue("i", -1); + if (!PyLong_CheckExact(test_var)) { + PyErr_SetString(TestError, "Failed to Create long"); + return NULL; + } + + test_var = Py_BuildValue("i", -1); + if (!PyLong_CheckExact(test_var)) { + PyErr_SetString(TestError, "Failed to Create long"); + return NULL; + } + + test_var = Py_BuildValue("H", 1); + if (!PyLong_CheckExact(test_var)) { + PyErr_SetString(TestError, "Failed to Create long"); + return NULL; + } + + test_var = Py_BuildValue("I", -100); + if (!PyLong_CheckExact(test_var) && _PyLong_Sign(test_var) == 1) { + PyErr_SetString(TestError, "Failed to Create unsigned long"); + return NULL; + } + + /* + Skipping a bunch of the build values because I am lazy and they work with + no tests to date + */ + + test_var = Py_BuildValue("?", 0); + if (!PyBool_Check(test_var) || test_var != Py_False) { + PyErr_SetString(TestError, "Failed to Create boolean"); + return NULL; + } + + test_var = Py_BuildValue("?", 1); + if (!PyBool_Check(test_var) || test_var != Py_True) { + PyErr_SetString(TestError, "Failed to Create boolean"); + return NULL; + } + + Py_RETURN_NONE; +} + static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *); static PyMethodDef TestMethods[] = { @@ -3255,6 +3339,7 @@ static PyMethodDef TestMethods[] = { {"test_gc_control", test_gc_control, METH_NOARGS}, {"test_list_api", test_list_api, METH_NOARGS}, {"test_dict_iteration", test_dict_iteration, METH_NOARGS}, + {"test_py_buildvalue", (PyCFunction)test_py_buildvalue, METH_NOARGS}, {"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS}, {"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS}, {"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS}, diff --git a/Python/modsupport.c b/Python/modsupport.c index b9a10dc157e7e5..372d63bad6e245 100644 --- a/Python/modsupport.c +++ b/Python/modsupport.c @@ -489,6 +489,13 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags) return v; } + case '?': + { + if (va_arg(*p_va, int) == 0) + Py_RETURN_FALSE; + else + Py_RETURN_TRUE; + } case ':': case ',': case ' ': From fffc5b9ef3c622f85f078a651484ca83e42eff82 Mon Sep 17 00:00:00 2001 From: Furkan Onder Date: Thu, 2 Feb 2023 01:07:48 +0300 Subject: [PATCH 2/3] Fix typo in error string --- Modules/_testcapimodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 8f00eacbfdd49b..10d9243f5bc836 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -3250,7 +3250,7 @@ test_py_buildvalue(PyObject *self) { test_var = Py_BuildValue("(i,i,i)", 2,3,4); if (!PyTuple_CheckExact(test_var)) { - PyErr_SetString(TestError, "Failed to Create tuble"); + PyErr_SetString(TestError, "Failed to Create tuple"); return NULL; } From 965939ae0e7bcd50a759acbcaaae335a764900d3 Mon Sep 17 00:00:00 2001 From: furkanonder Date: Thu, 2 Feb 2023 13:43:01 +0300 Subject: [PATCH 3/3] Fix invalid function cast warnings --- Modules/_testcapimodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index 8f00eacbfdd49b..30d224ad17185b 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -3245,7 +3245,8 @@ function_set_kw_defaults(PyObject *self, PyObject *args) } static PyObject * -test_py_buildvalue(PyObject *self) { +test_py_buildvalue(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ PyObject *test_var; test_var = Py_BuildValue("(i,i,i)", 2,3,4);