# HG changeset patch # User digitalxero # Date 1298784434 18000 # Node ID e3d64b671a5e50441d49dadf31489a9c8a9e6730 # Parent 8cdddac5ad283061d33061849b78572cd0f771f4 Create Patch for Bug #10880 Add a boolion format option to Py_BuildValue and Py_VaBuildValue Also added some tests to test_capi that verifies Py_BuildValue is building the right type. I did not add tests for every possible type as it has lived this long without, but I did want to test my new code diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -1778,6 +1778,91 @@ #undef CHECK_1_FORMAT } +static PyObject * +test_py_buildvalue(PyObject *self) { + PyObject *test_var; + va_list va; + + 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_unicode_compare_with_ascii(PyObject *self) { @@ -2253,6 +2338,7 @@ {"test_datetime_capi", test_datetime_capi, METH_NOARGS}, {"test_list_api", (PyCFunction)test_list_api, METH_NOARGS}, {"test_dict_iteration", (PyCFunction)test_dict_iteration,METH_NOARGS}, + {"test_py_buildvalue", (PyCFunction)test_py_buildvalue, METH_NOARGS}, {"test_lazy_hash_inheritance", (PyCFunction)test_lazy_hash_inheritance,METH_NOARGS}, {"test_broken_memoryview", (PyCFunction)test_broken_memoryview,METH_NOARGS}, {"test_long_api", (PyCFunction)test_long_api, METH_NOARGS}, diff --git a/Python/modsupport.c b/Python/modsupport.c --- a/Python/modsupport.c +++ b/Python/modsupport.c @@ -399,6 +399,14 @@ return v; } + case '?': + { + if (va_arg(*p_va, int) == 0) + Py_RETURN_FALSE; + else + Py_RETURN_TRUE; + } + case ':': case ',': case ' ':