WIP: Remove fragile use of __array_interface__ in ctypeslib.as_array#10970
WIP: Remove fragile use of __array_interface__ in ctypeslib.as_array#10970charris merged 4 commits intonumpy:masterfrom
__array_interface__ in ctypeslib.as_array#10970Conversation
There was a problem hiding this comment.
Monkey-patching ctypes was undocumented, and has confused me in the past!
There was a problem hiding this comment.
This is already encoded in the implementation of dtype.str.__get__
|
While we're talking about PEP3118 and ctypes - could I ask someone who's worked with PEP3118 (@ahaldane?) to give a review to python/cpython#5561 and python/cpython#5576? It would be great if numpy would work with ctype structs as well, and those PRs have been sitting idle for a while |
There was a problem hiding this comment.
it would be safer to have shape=None and in the body of the code if shape is None: shape = ()
While in the current version shape is read-only, this might future-proof the function
There was a problem hiding this comment.
Safer from what perspective?
There was a problem hiding this comment.
whoops, shape is a zero-length-tuple so immutable. Never mind, sorry for the noise.
There was a problem hiding this comment.
You've made me realize that defaulting to None and erroring is probably closer to the old behaviour
There was a problem hiding this comment.
Updated to reject shape=None
7038d28 to
2bb3d77
Compare
|
I suspect this fixes gh-6511 too |
|
I'm hoping that this also fixes the daily wheels builds that have been broken since #10968 went in. |
|
It looks like you've truncated that error message by at least one character (a I don't predict this patch will fix that. Strange that it works fine on travis, but not the wheel builds. |
|
Also works on Mac and Windows, only Yeah, I kind of thought this PR wouldn't help, but wanted to get the ball rolling ... |
|
@charris: First step would be to not replace the error message coming from the internal PEP3118 parser with that less useful one. |
No, but maybe we should. Python 2.7.7 came out about 4 years ago and these days it is pretty easy to upgrade. Be worth raising the issue on the list. There were several ctypes fixes in the recently released Python 2.7.15, so if we do pick a version, it should only be new enough that our tests pass and otherwise recommend that folks stay current. |
There was a problem hiding this comment.
I think this would read better if the } was at the end of the previous line. This looks too much like C.
There was a problem hiding this comment.
Maybe do the dictionary creation separately from the return statement.
There was a problem hiding this comment.
I think we have different stylistic opinions here. How about I just put it on one line, since it fits nicely anyway?
There was a problem hiding this comment.
What is el_type? A complete docstring would help here.
There was a problem hiding this comment.
I wonder if we still need this. Much of the code here is ancient and predates the inclusion of ctypes into the standard library in Python 2.5.
There was a problem hiding this comment.
A standard docstring would help here.
There was a problem hiding this comment.
I'm going to argue this is out of scope for this patch
There was a problem hiding this comment.
I'm not a huge fan of using attribute testing here, which will give a false-positive for np.recarray with the wrong dtype. I suppose I could switch it back to how the detection was done before, if you'd prefer that.
There was a problem hiding this comment.
See above, ctypes should be available in the Python versions we support.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Blank line after imports is standard.
|
The presence of |
I disagree - we've had bug reports about this in the past. It seems that python can be built without ctypes on some platforms? |
|
See this comment for a summary of how to end up without ctypes |
|
Hmm, it actually seems reasonable that ctypes may not be available on all platforms/environments. It certainly doesn't hurt to deal with that case. |
2bb3d77 to
f6a6009
Compare
|
Some nits addressed. I think this does not change the patch-version-worry introduced by #10882 |
Everything behaves a lot better if we let the array constructor handle it, which will use the ctypes PEP3118 support. Bugs this fixes: * Stale state being attached to pointer objects (fixes numpygh-2671, closes numpygh-6214) * Weird failure modes on structured arrays (fixes-10978) * A regression in numpygh-10882 (fixes numpygh-10968)
f6a6009 to
8a8be50
Compare
|
Thanks Eric. |
__array_interface__ in ctypeslib.as_array
At this point, we can rely on PEP3118 support within ctypes and numpy
Fixes gh-10968
Fixes gh-2671
Closes gh-6214 (commit is cherry-picked, thanks @tynn!)