msg197655 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2013-09-13 20:52 |
This is one of most annoying things in Python to me. When you want accept any bytes-like object in you bytes-processing function you need special case empty input. def foo(data): data = memoryview(data) if data: data = data.cast('B') else: data = b'' You can't use just memoryview(data).cast('B') because it doesn't work for empty data. >>> memoryview(b'').cast('b') Traceback (most recent call last): File "", line 1, in TypeError: memoryview: cannot cast view with zeros in shape or strides It would be very nice to allow cast() for empty views. |
|
|
msg197659 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2013-09-13 20:55 |
This actually sounds like a bug to me. A view over the empty bytestring is a one-dimensional zero-length view, it isn't a special kind of entity. |
|
|
msg197662 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2013-09-13 21:07 |
Cool. Therefore I can hope on fixing this in maintained releases? |
|
|
msg197663 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2013-09-13 21:10 |
> Cool. Therefore I can hope on fixing this in maintained releases? Provided someone makes a patch :-) |
|
|
msg197666 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2013-09-13 21:34 |
Here is a patch. I can't tell whether it's right for sure, since the whole buffer thing has become so complicated. |
|
|
msg197759 - (view) |
Author: Stefan Krah (skrah) *  |
Date: 2013-09-15 09:55 |
I agree that this cast should work. Perhaps disallowing zero strides is enough -- I have to look at this more closely though. |
|
|
msg198890 - (view) |
Author: Stefan Krah (skrah) *  |
Date: 2013-10-03 11:32 |
Ok, I think the main reason for disallowing zeros in view->shape here was that casts are undefined if also the "shape" argument is given: x = memoryview(b'') x.cast('d', shape=[1]) Now, this case *is* already caught at a later stage, since there isn't enough space for the cast. Nevertheless, the code is tricky, so I'd prefer to be conservative and catch shape arguments earlier. I left a suggestion in Rietveld. I would commit it myself, but I'm moving and my infrastructure is a mess. It would be great if one of you could take this one. I'll try to review the general case for ndim > 1 later, but that's not particularly important right now. |
|
|
msg198903 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2013-10-03 17:59 |
New changeset b08e092df155 by Antoine Pitrou in branch '3.3': Issue #19014: memoryview.cast() is now allowed on zero-length views. http://hg.python.org/cpython/rev/b08e092df155 New changeset 1e13a58c1b92 by Antoine Pitrou in branch 'default': Issue #19014: memoryview.cast() is now allowed on zero-length views. http://hg.python.org/cpython/rev/1e13a58c1b92 |
|
|
msg198904 - (view) |
Author: Antoine Pitrou (pitrou) *  |
Date: 2013-10-03 18:02 |
Applied Stefan's suggestion. Thanks for the review :) |
|
|
msg198905 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2013-10-03 18:26 |
Thank you Antoine. |
|
|