Skip to content

Commit 40a1dce

Browse files
committed
Do suggested changes, add more tests
1 parent 9c9a65e commit 40a1dce

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

Lib/test/test_ctypes/test_prototypes.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,24 @@ def test_invalid_setattr_argtypes(self):
8181
proto = CFUNCTYPE(c_int, c_char_p)
8282
func = proto(("myprintf", testdll), ((1, "fmt"),))
8383

84-
self.assertRaisesRegex(TypeError, "_argtypes_ must be a sequence of types",
85-
setattr, func, "argtypes", 123)
84+
with self.assertRaisesRegex(TypeError, "_argtypes_ must be a sequence of types"):
85+
func.argtypes = 123
8686
self.assertEqual(func.argtypes, (c_char_p,))
8787

88-
self.assertRaisesRegex(ValueError, "paramflags must have the same length as argtypes",
89-
setattr, func, "argtypes", (c_char_p, c_int))
88+
with self.assertRaisesRegex(ValueError, "paramflags must have the same length as argtypes"):
89+
func.argtypes = (c_char_p, c_int)
9090
self.assertEqual(func.argtypes, (c_char_p,))
9191

92+
def test_paramflags_outarg(self):
93+
proto = CFUNCTYPE(c_int, c_char_p, c_int)
94+
with self.assertRaisesRegex(TypeError, "must be a pointer type"):
95+
func = proto(("myprintf", testdll), ((1, "fmt"), (2, "out")))
96+
97+
proto = CFUNCTYPE(c_int, c_char_p, c_void_p)
98+
func = proto(("myprintf", testdll), ((1, "fmt"), (2, "out")))
99+
with self.assertRaisesRegex(TypeError, "must be a pointer type"):
100+
func.argtypes = (c_char_p, c_int)
101+
92102
def test_int_pointer_arg(self):
93103
func = testdll._testfunc_p_p
94104
if sizeof(c_longlong) == sizeof(c_void_p):

Modules/_ctypes/_ctypes.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,21 +3763,14 @@ static int
37633763
_ctypes_CFuncPtr_argtypes_set_impl(PyCFuncPtrObject *self, PyObject *value)
37643764
/*[clinic end generated code: output=596a36e2ae89d7d1 input=c4627573e980aa8b]*/
37653765
{
3766-
PyObject *converters;
3767-
3768-
PyTypeObject *type = Py_TYPE(self);
3769-
ctypes_state *st = get_module_state_by_def(Py_TYPE(type));
3770-
37713766
if (value == NULL || value == Py_None) {
3772-
/* Verify paramflags again due to constraints with argtypes */
3773-
if (!_validate_paramflags(st, type, self->paramflags, value)) {
3774-
return -1;
3775-
}
3776-
37773767
atomic_xsetref(&self->argtypes, NULL);
37783768
atomic_xsetref(&self->converters, NULL);
37793769
} else {
3780-
converters = converters_from_argtypes(st, value);
3770+
PyTypeObject *type = Py_TYPE(self);
3771+
ctypes_state *st = get_module_state_by_def(Py_TYPE(type));
3772+
3773+
PyObject *converters = converters_from_argtypes(st, value);
37813774
if (!converters)
37823775
return -1;
37833776

@@ -3932,8 +3925,9 @@ _validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags,
39323925
argtypes = info->argtypes;
39333926
}
39343927

3935-
if (paramflags == NULL || argtypes == NULL)
3928+
if (paramflags == NULL || argtypes == NULL) {
39363929
return 1;
3930+
}
39373931

39383932
if (!PyTuple_Check(paramflags)) {
39393933
PyErr_SetString(PyExc_TypeError,

0 commit comments

Comments
 (0)