Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/test/test_ctypes/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,27 @@

self.assertEqual(_testfunc_callback_int_to_longlong(cb), -1)

def test_mismatched_callback_type_error_includes_signature(self):
# gh-125788: mismatched callback types used to say
# "expected CFunctionType instance instead of CFunctionType".
Expected = CFUNCTYPE(c_int, c_void_p, c_void_p)

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Ubuntu (installed) / build, install and test

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Lazy imports enabled / Run Tests with lazy_imports=all

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, tail-call)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / macOS / build and test (macos-26)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-26.04-arm)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-26.04-arm)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, tail-call)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-26.04)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-26.04)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Sanitizers / TSan

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Emscripten / build and test

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (x64, switch-case)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-26.04)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Sanitizers / ASan

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Windows / Build and test (arm64, switch-case)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Sanitizers / UBSan

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / macOS / build and test (macos-26-intel)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / macOS (free-threading) / build and test (macos-26)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Windows / Build and test (x64, switch-case)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Windows / Build and test (Win32, switch-case)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Sanitizers / TSan (free-threading)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined

Check failure on line 565 in Lib/test/test_ctypes/test_callbacks.py

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / Build and test (arm64, switch-case)

test_mismatched_callback_type_error_includes_signature (test.test_ctypes.test_callbacks.SampleCallbacksTestCase.test_mismatched_callback_type_error_includes_signature)

NameError: name 'c_void_p' is not defined
Actual = CFUNCTYPE(c_int, c_int, c_int)

def callback(a, b):
return 0

actual = Actual(callback)
with self.assertRaises(TypeError) as cm:
Expected.from_param(actual)
msg = str(cm.exception)
self.assertIn("signature", msg)
self.assertIn("restype", msg)
self.assertIn("argtypes", msg)
self.assertNotEqual(
msg,
f"expected {Expected.__name__} instance instead of {type(actual).__name__}",
)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:mod:`ctypes` :exc:`TypeError` messages for mismatched callback function
types now include the expected and actual signatures (``restype`` and
``argtypes``). Previously both types often appeared only as
``CFunctionType`` or ``WinFunctionType``.
26 changes: 26 additions & 0 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,32 @@ CDataType_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
_Py_LeaveRecursiveCall();
return value;
}
/* Callback function pointer types often share the same tp_name
("CFunctionType" / "WinFunctionType"), so include signatures when
both the expected type and the value are callback types (#125788). */
if (PyCFuncPtrTypeObject_Check(st, type) &&
PyCFuncPtrObject_Check(st, value)) {
StgInfo *expected_info;
StgInfo *actual_info;
if (PyStgInfo_FromType(st, type, &expected_info) < 0) {
return NULL;
}
if (PyStgInfo_FromType(st, (PyObject *)Py_TYPE(value), &actual_info) < 0) {
return NULL;
}
if (expected_info != NULL && actual_info != NULL) {
PyErr_Format(PyExc_TypeError,
"expected %s with signature (restype=%R, argtypes=%R) "
"but received %s with signature (restype=%R, argtypes=%R)",
((PyTypeObject *)type)->tp_name,
expected_info->restype ? expected_info->restype : Py_None,
expected_info->argtypes ? expected_info->argtypes : Py_None,
Py_TYPE(value)->tp_name,
actual_info->restype ? actual_info->restype : Py_None,
actual_info->argtypes ? actual_info->argtypes : Py_None);
return NULL;
}
}
PyErr_Format(PyExc_TypeError,
"expected %s instance instead of %s",
((PyTypeObject *)type)->tp_name,
Expand Down
Loading