Issue 19014: Allow memoryview.cast() for empty views (original) (raw)

Created on 2013-09-13 20:52 by serhiy.storchaka, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
zerocast.patch pitrou,2013-09-13 21:34 review
Messages (10)
msg197655 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) 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) * (Python committer) 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) * (Python committer) Date: 2013-09-13 21:07
Cool. Therefore I can hope on fixing this in maintained releases?
msg197663 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) (Python triager) 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) * (Python committer) Date: 2013-10-03 18:02
Applied Stefan's suggestion. Thanks for the review :)
msg198905 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-10-03 18:26
Thank you Antoine.
History
Date User Action Args
2022-04-11 14:57:50 admin set github: 63214
2013-10-03 18:26:51 serhiy.storchaka set messages: +
2013-10-03 18:02:37 pitrou set status: open -> closedresolution: fixedmessages: + stage: needs patch -> resolved
2013-10-03 17:59:02 python-dev set nosy: + python-devmessages: +
2013-10-03 11:32:59 skrah set messages: +
2013-10-02 12:54:56 ncoghlan unlink issue17839 dependencies
2013-10-01 21:48:18 serhiy.storchaka link issue17839 dependencies
2013-09-15 09:55:52 skrah set messages: +
2013-09-13 21:34:51 pitrou set files: + zerocast.patchkeywords: + patchmessages: +
2013-09-13 21:10:10 pitrou set messages: +
2013-09-13 21:07:50 serhiy.storchaka set messages: +
2013-09-13 20:55:34 pitrou set versions: + Python 3.3nosy: + ncoghlanmessages: + type: enhancement -> behaviorstage: needs patch
2013-09-13 20:52:29 serhiy.storchaka create