]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-153903: Copy docstrings and other metadata in `ctypes.util.wrap_dll_function`...
authorBrian Ward <bward@flatironinstitute.org>
Thu, 23 Jul 2026 16:57:55 +0000 (12:57 -0400)
committerGitHub <noreply@github.com>
Thu, 23 Jul 2026 16:57:55 +0000 (16:57 +0000)
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Doc/library/ctypes.rst
Lib/ctypes/util.py
Lib/test/test_ctypes/test_funcptr.py

index 34c8636abaa925d36a241f41dbb343dbb072d3cf..a4de7cb09cca9daf194b3409d53f4deb8d0aeb77 100644 (file)
@@ -708,8 +708,7 @@ Specifying function pointers using type annotations
 
       @wrap_dll_function(dll_to_wrap)
       def function_ptr_name(arg_name: ctypes_type, ...) -> ctypes_type:
-         # There should be no body
-         pass
+         """Optional docstring. There should be no function body."""
 
    The body of the decorated function is ignored, and any parameters that are
    missing type annotations are skipped. The names of the parameters are ignored
index a73422598b2cd96160282b49c5b12a6fcf435ffa..1f2b9cf0e3ce8dd46b9fa4b717b6604b3fbc9a4e 100644 (file)
@@ -590,6 +590,8 @@ def wrap_dll_function(dll):
 
         ptr.restype = restype
         ptr.argtypes = tuple(annotations.values())
+        functools.update_wrapper(ptr, func, updated=())
+
         return ptr
 
     return decorator
index 86699ad81098275c54340705266ec10a38e07c29..28ff34f9048454d79938076e38610c6b1aa2b894 100644 (file)
@@ -134,12 +134,14 @@ class CFuncPtrTestCase(unittest.TestCase, StructCheckMixin):
     def test_wrap_dll_function(self):
         @wrap_dll_function(ctypes.pythonapi)
         def PyObject_GetAttr(op: ctypes.py_object, attr: ctypes.py_object) -> ctypes.py_object:
+            """Call the PythonAPI function underlying getattr"""
             pass
 
         class Foo:
             a = "abc"
 
         self.assertEqual(PyObject_GetAttr(Foo, "a"), "abc")
+        self.assertEqual(PyObject_GetAttr.__doc__, "Call the PythonAPI function underlying getattr")
 
         with self.assertRaises(AttributeError):
             @wrap_dll_function(ctypes.pythonapi)