Thomas Heller <thel...@ctypes.org> added the comment:

The problem is the implementation of the current __init__ method, in
Modules/_ctypes/_ctypes.c, line 4024.  Rewritten in Python, and slightly
simplified it looks like this:

    def __init__(self, *args, **kw):
        """The current BUGGY __init__ method"""
        names = [f[0] for f in self._fields_]
        for n, v in zip(names, args):
            setattr(self, n, v)
        for n, v in kw.iteritems():
            setattr(self, n, v)

It assigns positional parameters to the names found in the _fields_ list
of the subclass, but it should look up all the _fields_ definitions in
all the superclasses, too.  Working pseudo Python code:

    def __init__(self, *args, **kw):
        """This is how the Structure's __init__method SHOULD be"""
        if args:
            names = []
            for tp in self.__class__.mro()[2::-1]:
                for n, _ in tp._fields_:
                    names.append(n)
            for n, v in zip(names, args):
                setattr(self, n, v)
        for n, v in kw.iteritems():
            setattr(self, n, v)

Now, I don't really want to write the above code in C ;-).

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue5042>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to