New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
memoryview + bytes fails #60149
Comments
Python 3.3.0rc2+ (default:9def2209a839, Sep 10 2012, 08:44:51)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> memoryview(b'foo') + b'bar'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'memoryview' and 'bytes'
>>> b'bar' + memoryview(b'foo')
b'barfoo'
>>> |
What is the expected outcome? memoryviews can't be resized, so >>> bytearray([1,2,3]) + b'123'
bytearray(b'\x01\x02\x03123') |
Just prepend the empty bytestring if you want to make sure the result is a bytes object: >>> b'' + memoryview(b'foo') + b'bar'
b'foobar' I think the following limitation may be more annoying, though: >>> b''.join([memoryview(b'foo'), b'bar'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected bytes, memoryview found |
The same as
Or I could explicitly convert the memoryview to a bytes object. That strikes me as rather preferable. However, this defeats one use of memoryview, which is to avoid unnecessary copying. So it might be suitable workaround for some cases, but not all. |
Indeed, that's why I think it would be good to fix the bytes.join() |
Opened bpo-15958 for the bytes.join enhancement. |
It's worth noting that the "buffer()" built-in in Python2 had this behavior, and it enabled a copy-reduction optimization within Twisted's outgoing transport buffer. There are of course other ways to do this, but it seems like it would be nice to restore this handy optimization; it seems like a bug, or at least an oversight, that the convenience 'bytes+memoryview' (which cannot provide a useful optimization) works, but 'memoryview+bytes' (which would be equally helpful from a convenience perspective _could_ provide a reduction in copying) doesn't. Despite the bytes.join optimization (which, don't get me wrong, is also very helpful, almost necessary) this remains very useful. |
I'm not sure what you're talking about since: >>> b = buffer("abc")
>>> b + "xyz"
'abcxyz'
>>> (b + "xyz") is b
False ... doesn't look like it avoid copies to me. |
Le Oct 11, 2012 à 12:13 PM, Antoine Pitrou <report@bugs.python.org> a écrit :
The case where copies are avoided is documented here: <http://twistedmatrix.com/trac/browser/trunk/twisted/internet/abstract.py?rev=35733#L20\> |
... which would be handled nicely by bpo-15958. |
Yes, it would be *possible* to fix it with that alone, but that still makes it a pointless 'gotcha' in differing behavior between memoryview and buffer, especially given that bytes+memoryview does something semantically different than memoryview+bytes for no reason. |
Well, the fact that memoryview + bytes wouldn't return you a memoryview object might be a good reason to disallow it. Compare with: >>> bytearray(b"x") + b"y"
bytearray(b'xy')
>>> b"x" + bytearray(b"y")
b'xy' |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: