[Python-checkins] gh-64631: Test exception messages in cloned Argument Clinic funcs (#104167)
erlend-aasland
webhook-mailer at python.org
Fri May 5 08:08:31 EDT 2023
https://github.com/python/cpython/commit/d0b4abedfb8b0322df835065f85551d097cfecb8
commit: d0b4abedfb8b0322df835065f85551d097cfecb8
branch: main
author: Erlend E. Aasland <erlend.aasland at protonmail.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-05-05T14:08:24+02:00
summary:
gh-64631: Test exception messages in cloned Argument Clinic funcs (#104167)
files:
M Lib/test/test_clinic.py
M Modules/_testclinic.c
M Modules/clinic/_testclinic.c.h
diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py
index 4abf739cf52c..660f7a1e296d 100644
--- a/Lib/test/test_clinic.py
+++ b/Lib/test/test_clinic.py
@@ -1284,6 +1284,19 @@ def test_gh_99240_double_free(self):
with self.assertRaisesRegex(TypeError, expected_error):
ac_tester.gh_99240_double_free('a', '\0b')
+ def test_cloned_func_exception_message(self):
+ incorrect_arg = -1 # f1() and f2() accept a single str
+ with self.assertRaisesRegex(TypeError, "clone_f1"):
+ ac_tester.clone_f1(incorrect_arg)
+ with self.assertRaisesRegex(TypeError, "clone_f2"):
+ ac_tester.clone_f2(incorrect_arg)
+
+ def test_cloned_func_with_converter_exception_message(self):
+ for name in "clone_with_conv_f1", "clone_with_conv_f2":
+ with self.subTest(name=name):
+ func = getattr(ac_tester, name)
+ self.assertEqual(func(), name)
+
if __name__ == "__main__":
unittest.main()
diff --git a/Modules/_testclinic.c b/Modules/_testclinic.c
index 91fdee24d328..6ff55a2755cf 100644
--- a/Modules/_testclinic.c
+++ b/Modules/_testclinic.c
@@ -9,6 +9,19 @@
#include "Python.h"
+
+// Used for clone_with_conv_f1 and clone_with_conv_v2
+typedef struct {
+ const char *name;
+} custom_t;
+
+static int
+custom_converter(PyObject *obj, custom_t *val)
+{
+ return 1;
+}
+
+
#include "clinic/_testclinic.c.h"
@@ -1117,6 +1130,70 @@ gh_99240_double_free_impl(PyObject *module, char *a, char *b)
}
+/*[clinic input]
+_testclinic.clone_f1 as clone_f1
+ path: str
+[clinic start generated code]*/
+
+static PyObject *
+clone_f1_impl(PyObject *module, const char *path)
+/*[clinic end generated code: output=8c30b5620ba86715 input=9c614b7f025ebf70]*/
+{
+ Py_RETURN_NONE;
+}
+
+
+/*[clinic input]
+_testclinic.clone_f2 as clone_f2 = _testclinic.clone_f1
+[clinic start generated code]*/
+
+static PyObject *
+clone_f2_impl(PyObject *module, const char *path)
+/*[clinic end generated code: output=6aa1c39bec3f5d9b input=1aaaf47d6ed2324a]*/
+{
+ Py_RETURN_NONE;
+}
+
+
+/*[python input]
+class custom_t_converter(CConverter):
+ type = 'custom_t'
+ converter = 'custom_converter'
+
+ def pre_render(self):
+ self.c_default = f'''{{
+ .name = "{self.function.name}",
+ }}'''
+
+[python start generated code]*/
+/*[python end generated code: output=da39a3ee5e6b4b0d input=b2fb801e99a06bf6]*/
+
+
+/*[clinic input]
+_testclinic.clone_with_conv_f1 as clone_with_conv_f1
+ path: custom_t = None
+[clinic start generated code]*/
+
+static PyObject *
+clone_with_conv_f1_impl(PyObject *module, custom_t path)
+/*[clinic end generated code: output=f7e030ffd5439cb0 input=bc77bc80dec3f46d]*/
+{
+ return PyUnicode_FromString(path.name);
+}
+
+
+/*[clinic input]
+_testclinic.clone_with_conv_f2 as clone_with_conv_f2 = _testclinic.clone_with_conv_f1
+[clinic start generated code]*/
+
+static PyObject *
+clone_with_conv_f2_impl(PyObject *module, custom_t path)
+/*[clinic end generated code: output=9d7fdd6a75eecee4 input=cff459a205fa83bb]*/
+{
+ return PyUnicode_FromString(path.name);
+}
+
+
static PyMethodDef tester_methods[] = {
TEST_EMPTY_FUNCTION_METHODDEF
OBJECTS_CONVERTER_METHODDEF
@@ -1168,6 +1245,10 @@ static PyMethodDef tester_methods[] = {
GH_32092_KW_PASS_METHODDEF
GH_99233_REFCOUNT_METHODDEF
GH_99240_DOUBLE_FREE_METHODDEF
+ CLONE_F1_METHODDEF
+ CLONE_F2_METHODDEF
+ CLONE_WITH_CONV_F1_METHODDEF
+ CLONE_WITH_CONV_F2_METHODDEF
{NULL, NULL}
};
diff --git a/Modules/clinic/_testclinic.c.h b/Modules/clinic/_testclinic.c.h
index 831f58ca650a..cc69f5c3d2fe 100644
--- a/Modules/clinic/_testclinic.c.h
+++ b/Modules/clinic/_testclinic.c.h
@@ -2817,4 +2817,262 @@ gh_99240_double_free(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
exit:
return return_value;
}
-/*[clinic end generated code: output=e8211606b03d733a input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(clone_f1__doc__,
+"clone_f1($module, /, path)\n"
+"--\n"
+"\n");
+
+#define CLONE_F1_METHODDEF \
+ {"clone_f1", _PyCFunction_CAST(clone_f1), METH_FASTCALL|METH_KEYWORDS, clone_f1__doc__},
+
+static PyObject *
+clone_f1_impl(PyObject *module, const char *path);
+
+static PyObject *
+clone_f1(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 1
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(path), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"path", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "clone_f1",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[1];
+ const char *path;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!PyUnicode_Check(args[0])) {
+ _PyArg_BadArgument("clone_f1", "argument 'path'", "str", args[0]);
+ goto exit;
+ }
+ Py_ssize_t path_length;
+ path = PyUnicode_AsUTF8AndSize(args[0], &path_length);
+ if (path == NULL) {
+ goto exit;
+ }
+ if (strlen(path) != (size_t)path_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ return_value = clone_f1_impl(module, path);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(clone_f2__doc__,
+"clone_f2($module, /, path)\n"
+"--\n"
+"\n");
+
+#define CLONE_F2_METHODDEF \
+ {"clone_f2", _PyCFunction_CAST(clone_f2), METH_FASTCALL|METH_KEYWORDS, clone_f2__doc__},
+
+static PyObject *
+clone_f2_impl(PyObject *module, const char *path);
+
+static PyObject *
+clone_f2(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 1
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(path), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"path", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "clone_f2",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[1];
+ const char *path;
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!PyUnicode_Check(args[0])) {
+ _PyArg_BadArgument("clone_f2", "argument 'path'", "str", args[0]);
+ goto exit;
+ }
+ Py_ssize_t path_length;
+ path = PyUnicode_AsUTF8AndSize(args[0], &path_length);
+ if (path == NULL) {
+ goto exit;
+ }
+ if (strlen(path) != (size_t)path_length) {
+ PyErr_SetString(PyExc_ValueError, "embedded null character");
+ goto exit;
+ }
+ return_value = clone_f2_impl(module, path);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(clone_with_conv_f1__doc__,
+"clone_with_conv_f1($module, /, path=None)\n"
+"--\n"
+"\n");
+
+#define CLONE_WITH_CONV_F1_METHODDEF \
+ {"clone_with_conv_f1", _PyCFunction_CAST(clone_with_conv_f1), METH_FASTCALL|METH_KEYWORDS, clone_with_conv_f1__doc__},
+
+static PyObject *
+clone_with_conv_f1_impl(PyObject *module, custom_t path);
+
+static PyObject *
+clone_with_conv_f1(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 1
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(path), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"path", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "clone_with_conv_f1",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[1];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
+ custom_t path = {
+ .name = "clone_with_conv_f1",
+ };
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (!custom_converter(args[0], &path)) {
+ goto exit;
+ }
+skip_optional_pos:
+ return_value = clone_with_conv_f1_impl(module, path);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(clone_with_conv_f2__doc__,
+"clone_with_conv_f2($module, /, path=None)\n"
+"--\n"
+"\n");
+
+#define CLONE_WITH_CONV_F2_METHODDEF \
+ {"clone_with_conv_f2", _PyCFunction_CAST(clone_with_conv_f2), METH_FASTCALL|METH_KEYWORDS, clone_with_conv_f2__doc__},
+
+static PyObject *
+clone_with_conv_f2_impl(PyObject *module, custom_t path);
+
+static PyObject *
+clone_with_conv_f2(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
+
+ #define NUM_KEYWORDS 1
+ static struct {
+ PyGC_Head _this_is_not_used;
+ PyObject_VAR_HEAD
+ PyObject *ob_item[NUM_KEYWORDS];
+ } _kwtuple = {
+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
+ .ob_item = { &_Py_ID(path), },
+ };
+ #undef NUM_KEYWORDS
+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
+
+ #else // !Py_BUILD_CORE
+ # define KWTUPLE NULL
+ #endif // !Py_BUILD_CORE
+
+ static const char * const _keywords[] = {"path", NULL};
+ static _PyArg_Parser _parser = {
+ .keywords = _keywords,
+ .fname = "clone_with_conv_f2",
+ .kwtuple = KWTUPLE,
+ };
+ #undef KWTUPLE
+ PyObject *argsbuf[1];
+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
+ custom_t path = {
+ .name = "clone_with_conv_f2",
+ };
+
+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
+ if (!args) {
+ goto exit;
+ }
+ if (!noptargs) {
+ goto skip_optional_pos;
+ }
+ if (!custom_converter(args[0], &path)) {
+ goto exit;
+ }
+skip_optional_pos:
+ return_value = clone_with_conv_f2_impl(module, path);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=f58202a6e5df2d16 input=a9049054013a1b77]*/
More information about the Python-checkins
mailing list