New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Structure sub-subclass does not initialize with base class positional arguments #49292
Comments
When trying to create a sub-subclass of a ctypes.Structure, the behavior An executable example that describes the problem in detail and The failing behavior is near the beginning, following the description. Three unsuccessful workarounds follow. The expected behavior is found at the end, where a final workaround has Can this behavior be improved in subsequent releases of ctypes? If not, |
The problem is the implementation of the current __init__ method, in 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 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 ;-). |
Heh. Me neither. Is it possible to use something like Cython to generate the C code? |
That was easier than I thought. The generated code is _ugly_ but it did I created init.pyx {{{ Then, ran cython init.pyx and got the attached file. Is that any help? |
Here is a patch I just wrote - it must still be checked for correctness, |
I've commited a slightly changed patch plus a test. trunk: 74917, py3k: 74918, release26-maint: 74919, release31-maint: 74920. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: