[issue17378] Document that ctypes automatically applies byref() when argtypes declares POINTER
New submission from Eli Bendersky: While playing with ctypes a bit, I noticed a feature that doesn't appear to be documented. Suppose I import the readdir_r function (assuming DIRENT is a correctly declared ctypes.Structure): DIR_p = c_void_p DIRENT_p = POINTER(DIRENT) DIRENT_pp = POINTER(DIRENT_p) readdir_r = lib.readdir_r readdir_r.argtypes = [DIR_p, DIRENT_p, DIRENT_pp] readdir_r.restype = c_int It seems that I can then call it as follows: dirent = DIRENT() result = DIRENT_p() readdir_r(dir_fd, dirent, result) Note that while readdir_r takes DIRENT_p and DIRENT_pp as its second and third args, I pass in just DIRENT and DIRENT_p, accordingly. What I should have done is use byref() on both, but ctypes seems to have some magic applied when argtypes declares pointer types. If I use byref, it still works. However, if I keep the same call and comment out the argtypes declaration, I get a segfault. This behavior of ctypes should be documented. ---------- assignee: docs@python components: Documentation keywords: easy messages: 183661 nosy: docs@python, eli.bendersky priority: normal severity: normal stage: needs patch status: open title: Document that ctypes automatically applies byref() when argtypes declares POINTER type: behavior versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue17378> _______________________________________
Eli Bendersky added the comment: Doc patch for 3.2 ---------- keywords: +patch Added file: http://bugs.python.org/file29340/issue17378.1.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue17378> _______________________________________
Changes by Eli Bendersky <eliben@gmail.com>: ---------- nosy: +amaury.forgeotdarc, theller _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue17378> _______________________________________
Thomas Heller added the comment: Patch looks good. Please apply. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue17378> _______________________________________
Roundup Robot added the comment: New changeset 76be5efa0d86 by Eli Bendersky in branch '3.2': Issue #17378: ctypes documentation fix. http://hg.python.org/cpython/rev/76be5efa0d86 New changeset 2cd2d8f8f72f by Eli Bendersky in branch '3.3': Issue #17378: ctypes documentation fix. http://hg.python.org/cpython/rev/2cd2d8f8f72f New changeset 58c07ba40926 by Eli Bendersky in branch 'default': Issue #17378: ctypes documentation fix. http://hg.python.org/cpython/rev/58c07ba40926 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue17378> _______________________________________
Roundup Robot added the comment: New changeset 2dd77a12e7bf by Eli Bendersky in branch '2.7': Closing #17378: ctypes documentation fix. http://hg.python.org/cpython/rev/2dd77a12e7bf ---------- resolution: -> fixed stage: needs patch -> committed/rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue17378> _______________________________________
participants (3)
-
Eli Bendersky -
Roundup Robot -
Thomas Heller