Skip to content

Commit 62d370f

Browse files
fix: Explicitly disallow _as_parameter_ returning tuples for default conversions in ctypes and add a test for the TypeError it raises.
1 parent 2dc28eb commit 62d370f

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

Lib/test/test_ctypes/test_parameters.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,20 @@ def from_param(cls, value):
297297

298298
self.assertEqual(trace, [1, 2, 3, 4, 5])
299299

300+
def test_as_parameter_tuple(self):
301+
class Dangerous(object):
302+
@property
303+
def _as_parameter_(self):
304+
return ('i', 42)
305+
306+
func = CDLL(_ctypes_test.__file__)._testfunc_p_p
307+
func.restype = c_int
308+
# func.argtypes = [c_void_p] # Do not set argtypes to force default conversion
309+
310+
# Should raise TypeError because tuples are not supported in default conversion
311+
with self.assertRaisesRegex(TypeError, "Don't know how to convert parameter 1"):
312+
func(Dangerous(), 0)
313+
300314

301315
if __name__ == '__main__':
302316
unittest.main()

Modules/_ctypes/callproc.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,6 @@ PyType_Spec carg_spec = {
620620
* by value, or a 2-tuple or 3-tuple which will be used according
621621
* to point 2 above. The third item (if any), is ignored. It is normally
622622
* used to keep the object alive where this parameter refers to.
623-
* XXX This convention is dangerous - you can construct arbitrary tuples
624-
* in Python and pass them. Would it be safer to use a custom container
625-
* datatype instead of a tuple?
626623
*
627624
* 4. Other Python objects cannot be passed as parameters - an exception is raised.
628625
*

0 commit comments

Comments
 (0)