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.
Created on 2012年09月15日 10:27 by exarkun, last changed 2022年04月11日 14:57 by admin.
| Messages (12) | |||
|---|---|---|---|
| msg170511 - (view) | Author: Jean-Paul Calderone (exarkun) * (Python committer) | Date: 2012年09月15日 10:27 | |
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' >>> |
|||
| msg170512 - (view) | Author: Stefan Krah (skrah) * (Python committer) | Date: 2012年09月15日 12:24 | |
What is the expected outcome? memoryviews can't be resized, so this scenario isn't possible: >>> bytearray([1,2,3]) + b'123' bytearray(b'\x01\x02\x03123') |
|||
| msg170513 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2012年09月15日 12:37 | |
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 |
|||
| msg170579 - (view) | Author: Jean-Paul Calderone (exarkun) * (Python committer) | Date: 2012年09月16日 23:23 | |
> What is the expected outcome? memoryviews can't be resized, so this scenario isn't possible: The same as `view.tobytes() + bytes`, but without the extra copy implied by `view.tobytes()`. > Just prepend the empty bytestring if you want to make sure the result is a bytes object: 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. |
|||
| msg170580 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2012年09月16日 23:36 | |
> 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() method (which is precisely meant to minimize copying and resizing). |
|||
| msg170619 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2012年09月17日 18:03 | |
Opened issue15958 for the bytes.join enhancement. |
|||
| msg172676 - (view) | Author: Glyph Lefkowitz (glyph) (Python triager) | Date: 2012年10月11日 19:05 | |
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. |
|||
| msg172678 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2012年10月11日 19:13 | |
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.
|
|||
| msg172687 - (view) | Author: Glyph Lefkowitz (glyph) (Python triager) | Date: 2012年10月11日 20:10 | |
Le Oct 11, 2012 à 12:13 PM, Antoine Pitrou <report@bugs.python.org> a écrit : > > Antoine Pitrou added the comment: > > 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. The case where copies are avoided is documented here: <http://twistedmatrix.com/trac/browser/trunk/twisted/internet/abstract.py?rev=35733#L20> |
|||
| msg172688 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2012年10月11日 20:16 | |
> The case where copies are avoided is documented here ... which would be handled nicely by issue15958. |
|||
| msg172689 - (view) | Author: Glyph Lefkowitz (glyph) (Python triager) | Date: 2012年10月11日 20:27 | |
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. |
|||
| msg172690 - (view) | Author: Antoine Pitrou (pitrou) * (Python committer) | Date: 2012年10月11日 20:32 | |
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' |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:36 | admin | set | github: 60149 |
| 2015年03月24日 13:29:08 | Jean-Paul Calderone | set | nosy:
- exarkun |
| 2015年03月24日 02:57:03 | martin.panter | set | nosy:
+ martin.panter |
| 2014年10月14日 15:07:39 | skrah | set | nosy:
- skrah |
| 2012年10月11日 20:32:23 | pitrou | set | messages: + msg172690 |
| 2012年10月11日 20:27:14 | glyph | set | messages: + msg172689 |
| 2012年10月11日 20:16:16 | pitrou | set | messages: + msg172688 |
| 2012年10月11日 20:10:53 | glyph | set | messages: + msg172687 |
| 2012年10月11日 19:13:52 | pitrou | set | messages: + msg172678 |
| 2012年10月11日 19:05:13 | glyph | set | nosy:
+ glyph messages: + msg172676 |
| 2012年09月18日 03:22:52 | Arfrever | set | nosy:
+ Arfrever |
| 2012年09月17日 18:03:51 | pitrou | set | messages: + msg170619 |
| 2012年09月16日 23:36:39 | pitrou | set | messages: + msg170580 |
| 2012年09月16日 23:23:01 | exarkun | set | messages: + msg170579 |
| 2012年09月15日 12:37:25 | pitrou | set | nosy:
+ pitrou messages: + msg170513 |
| 2012年09月15日 12:24:15 | skrah | set | nosy:
+ skrah messages: + msg170512 |
| 2012年09月15日 10:27:58 | exarkun | create | |