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: Allow memoryview.cast() for empty views
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.3, Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: ncoghlan, pitrou, python-dev, serhiy.storchaka, skrah, teoliphant
Priority: normal Keywords: patch

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 "<stdin>", line 1, in <module>
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:50adminsetgithub: 63214
2013-10-03 18:26:51serhiy.storchakasetmessages: + msg198905
2013-10-03 18:02:37pitrousetstatus: open -> closed
resolution: fixed
messages: + msg198904

stage: needs patch -> resolved
2013-10-03 17:59:02python-devsetnosy: + python-dev
messages: + msg198903
2013-10-03 11:32:59skrahsetmessages: + msg198890
2013-10-02 12:54:56ncoghlanunlinkissue17839 dependencies
2013-10-01 21:48:18serhiy.storchakalinkissue17839 dependencies
2013-09-15 09:55:52skrahsetmessages: + msg197759
2013-09-13 21:34:51pitrousetfiles: + zerocast.patch
keywords: + patch
messages: + msg197666
2013-09-13 21:10:10pitrousetmessages: + msg197663
2013-09-13 21:07:50serhiy.storchakasetmessages: + msg197662
2013-09-13 20:55:34pitrousetversions: + Python 3.3
nosy: + ncoghlan

messages: + msg197659

type: enhancement -> behavior
stage: needs patch
2013-09-13 20:52:29serhiy.storchakacreate