[issue5042] Structure sub-subclass does not initialize with base class positional arguments

Thomas Heller report at bugs.python.org
Thu Sep 17 21:53:39 CEST 2009


Thomas Heller <theller at 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 <report at bugs.python.org>
<http://bugs.python.org/issue5042>
_______________________________________


More information about the Python-bugs-list mailing list