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.

classification
Title: ctypes: subclassing an already subclassed ArrayType generates AttributeError
Type: behavior Stage: resolved
Components: ctypes Versions: Python 3.1, Python 3.2, Python 3.3, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Steve.Thompson, amaury.forgeotdarc, belopolsky, meador.inge, python-dev
Priority: normal Keywords: patch

Created on 2011-02-17 23:59 by Steve.Thompson, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue-11241.patch meador.inge, 2011-04-18 01:07 patch against main branch
issue11241.patch meador.inge, 2011-08-12 20:23 review
Messages (9)
msg128769 - (view) Author: Steve Thompson (Steve.Thompson) Date: 2011-02-17 23:59
Consider the following:
python code:
class my_array( ctypes.Array ):
    _type_    = ctypes.c_uint8
    _length_  = 256

class my_array2( my_array ):
    pass

Output:
class my_array2( my_array ):
AttributeError: class must define a '_length_' attribute, which must be a positive integer


This is analogous to the C code
typedef char my_array[ 256 ];
typedef my_array my_array2;

However, the python code raises exceptions claiming _type_ and _length_ have not been defined.  This seems like a bug.  I shouldn't need to redefine _type_ and _length_, otherwise there was no point in subclassing my_array.

I tried to step into this using pdb but didn't have any luck.
msg133948 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-04-18 01:07
Verified that this is still broken in the main development branch.  
The base type should be checked for '_type_' or '_length_' before throwing
an error.  Attached is a patch that fixes the problem and adds covering
tests.  The full test suite passed on OS X 10.6.5 and I ran
'./python.exe -m test -R : -v test_ctype' to verify that I didn't
introduce any resource leaks.
msg133952 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-04-18 06:42
In the patch, _length_ is searched in the class and its base class. But what happens with a third level?
class my_array3(my_array2):
    pass
msg133968 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-04-18 14:03
> But what happens with a third level?

That doesn't work.  I have a test case for that, but the test case has
a typo that caused it to pass.  I will fix the test case and the code.
Thanks.
msg141989 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-08-12 20:23
Amaury, how about this patch?  I got rid of querying the type dictionary and hoisted the creation of the type instance earlier.  Then 'PyObject_GetAttrString' can be used to lookup '_length_' and '_type_' by the regular Python attribute lookup rules.  I extended the test cases to cover more error paths as well.
msg142033 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-08-13 18:53
Yes, the patch looks good!
msg142997 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-08-26 03:02
This patch was marked as accepted, but it doesn't seem to be committed.  Will someone commit it?
msg143233 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-08-30 20:07
New changeset 5e23532f694d by Amaury Forgeot d'Arc in branch '3.2':
Issue #11241: subclasses of ctypes.Array can now be subclassed.
http://hg.python.org/cpython/rev/5e23532f694d
msg143246 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-08-31 03:43
Thanks a lot for committing this for me Amaury.
History
Date User Action Args
2022-04-11 14:57:13adminsetgithub: 55450
2011-08-31 03:43:02meador.ingesetmessages: + msg143246
2011-08-30 22:09:03amaury.forgeotdarcsetstatus: open -> closed
resolution: accepted -> fixed
stage: patch review -> resolved
2011-08-30 20:07:25python-devsetnosy: + python-dev
messages: + msg143233
2011-08-26 03:02:22meador.ingesetmessages: + msg142997
2011-08-13 18:53:47amaury.forgeotdarcsetresolution: accepted
messages: + msg142033
2011-08-12 20:23:05meador.ingesetfiles: + issue11241.patch

messages: + msg141989
2011-04-18 14:03:17meador.ingesetmessages: + msg133968
2011-04-18 06:42:49amaury.forgeotdarcsetmessages: + msg133952
2011-04-18 01:07:45meador.ingesetfiles: + issue-11241.patch

assignee: theller ->
versions: + Python 3.1, Python 2.7, Python 3.2, Python 3.3
keywords: + patch
nosy: + amaury.forgeotdarc, belopolsky, meador.inge, - theller

messages: + msg133948
stage: patch review
2011-02-17 23:59:25Steve.Thompsoncreate