This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: ctypes.Structure constructor arguments
Type: behavior Stage:
Components: Extension Modules Versions: Python 2.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: theller Nosy List: Carl.Friedrich.Bolz, amaury.forgeotdarc, arigo, fijal, theller
Priority: normal Keywords: easy

Created on 2008-01-15 13:47 by arigo, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit arigo, 2008-01-15 13:47 example
ctypes-struct.patch theller, 2008-01-15 18:29 ctypes-struct-3.patch
Messages (11)
msg59964 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2008-01-15 13:47
The constructor of ctypes structures should probably not silently accept
the bogus arguments shown in the attached example.
msg59966 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2008-01-15 14:53
Do you have any idea how this could be implemented?
msg59967 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-01-15 14:58
What about PyArg_ParseTupleAndKeywords()?
msg59968 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2008-01-15 15:03
This would require to build the 'char *format' string at runtime,
the 'char *keywords[]' array too, and pass a variable number of arguments
into the call.
msg59969 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2008-01-15 15:12
The pure Python implementation we just wrote in PyPy is:

    for name, arg in zip(names, args):
        if name in kwds:
            raise TypeError("duplicate value for argument %r" % (
        self.__setattr__(name, arg)
    for name, arg in kwds.items():
        self.__setattr__(name, arg)

It's the same logic as in _ctypes.c:Struct_init(), where you can add the
C equivalent of "if name in kwds" after getting the name.
msg59971 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2008-01-15 15:21
Yes, I came up with a similar solution in the meantime; see the attached
msg59972 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2008-01-15 15:23
Oops, uploaded the wrong file.
msg59973 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2008-01-15 16:54
The patch is missing Py_DECREF(name).  Also, I'd raise TypeError
instead of ValueError, just like function calls do in a similar
msg59981 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2008-01-15 18:29
Ok, added the missing Py_DECREF, changed to TypeError, and added a test.
msg59997 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2008-01-16 15:40
For the "too many arguments" case... Clearly (IMHO) it should also
be a TypeError.  I have no clue about backward compatibility issues
msg60001 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2008-01-16 19:39
Committed as rev. 60003 in trunk.  I'll also change the "too many
arguments..." exception to a TypeError.

Date User Action Args
2022-04-11 14:56:29adminsetgithub: 46156
2008-01-16 19:39:02thellersetstatus: open -> closed
assignee: theller
resolution: fixed
messages: + msg60001
2008-01-16 15:41:00arigosetmessages: + msg59997
2008-01-15 18:31:53thellersetfiles: - ctypes-struct.patch
2008-01-15 18:29:46thellersetfiles: + ctypes-struct.patch
messages: + msg59981
2008-01-15 16:54:01arigosetmessages: + msg59973
2008-01-15 15:23:06thellersetfiles: + ctypes-struct.patch
messages: + msg59972
2008-01-15 15:22:28thellersetfiles: - ctypes-struct.patch
2008-01-15 15:21:46thellersetfiles: + ctypes-struct.patch
messages: + msg59971
2008-01-15 15:12:33arigosetkeywords: + easy
messages: + msg59969
2008-01-15 15:03:45thellersetmessages: + msg59968
2008-01-15 14:58:27amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg59967
2008-01-15 14:53:57thellersetnosy: + theller
messages: + msg59966
2008-01-15 13:48:03arigosettype: behavior
2008-01-15 13:47:08arigocreate