Skip to content

Commit

Permalink
pythongh-64631: Test exception messages in cloned Argument Clinic fun…
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed May 5, 2023
1 parent 5245cb6 commit d0b4abe
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
81 changes: 81 additions & 0 deletions Modules/_testclinic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
};

Expand Down
260 changes: 259 additions & 1 deletion Modules/clinic/_testclinic.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d0b4abe

Please sign in to comment.