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: memoryview and struct.pack_into
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Arfrever, benjamin.peterson, kristjan.jonsson, pitrou, python-dev, serhiy.storchaka, stangelandcl, terry.reedy
Priority: normal Keywords: needs review, patch

Created on 2014-07-31 08:31 by stangelandcl, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
struct_pack_into.py stangelandcl, 2014-07-31 08:31 Similar to comment. Demonstrates struct.pack_into throwing an exception
struct_pack_into_memoryview.patch serhiy.storchaka, 2014-11-18 18:15 review
Messages (9)
msg224386 - (view) Author: Clayton Stangeland (stangelandcl) * Date: 2014-07-31 08:31
I expect struct.pack_into to work for a memoryview. Currently struct.pack_into throws an exception.

>>> import struct
>>> buf = bytearray(10)
>>> struct.pack_into("<B", buf, 0, 99)
>>> buf[0]
99
>>> view = memoryview(buf)
>>> view.readonly
False
>>> struct.pack_into("<B", view, 0, 88)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: expected a writeable buffer object
>>> view[0:1] = 'a'
>>> view[0]
'a'
>>> buf[0]
97
>>> chr(buf[0])
'a'

The memoryview is writeable and from what I can tell from the documentation it implements the buffer interface, but struct.pack_into will not use it.
msg224387 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2014-07-31 08:58
This works in Python 3.3+. It is a bug in 2.7, so we have to wait for
someone motivated enough to work on an outdated Python version.
msg231321 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-18 13:48
This issue is similar to issue10212. Here is a patch which makes struct.pack_into() support new buffer protocol.

Something similar should be applied to 3.x because PyObject_AsWriteBuffer() is deprecated and not safe.
msg231336 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-11-18 18:08
I think you forgot to upload your patch.
msg231337 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-18 18:15
Oh, sorry. Here is it.
msg232931 - (view) Author: Kristján Valur Jónsson (kristjan.jonsson) * (Python committer) Date: 2014-12-19 10:22
lgtm :)
msg236352 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-02-21 06:03
Benjamin, what are your thoughts as RM?

> Something similar should be applied to 3.x because PyObject_AsWriteBuffer() is deprecated and not safe.

Done in issue22896.
msg236371 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2015-02-21 16:09
I suppose this is okay.
msg236378 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2015-02-21 17:57
New changeset 4d8e37e54a7d by Serhiy Storchaka in branch '2.7':
Issue #22113: struct.pack_into() now supports new buffer protocol (in
https://hg.python.org/cpython/rev/4d8e37e54a7d
History
Date User Action Args
2022-04-11 14:58:06adminsetgithub: 66311
2015-02-21 17:59:52serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2015-02-21 17:57:16python-devsetnosy: + python-dev
messages: + msg236378
2015-02-21 16:09:39benjamin.petersonsetmessages: + msg236371
2015-02-21 06:03:16serhiy.storchakasetnosy: + benjamin.peterson
messages: + msg236352
2014-12-19 10:22:00kristjan.jonssonsetmessages: + msg232931
2014-12-19 00:05:47Arfreversetnosy: + Arfrever
2014-12-18 16:23:58serhiy.storchakasetkeywords: + needs review
2014-11-18 18:15:15serhiy.storchakasetfiles: + struct_pack_into_memoryview.patch
keywords: + patch
messages: + msg231337
2014-11-18 18:08:10pitrousetmessages: + msg231336
2014-11-18 13:48:36serhiy.storchakasetnosy: + terry.reedy, serhiy.storchaka, kristjan.jonsson, pitrou
messages: + msg231321

assignee: serhiy.storchaka
stage: needs patch -> patch review
2014-10-14 17:21:38skrahsetnosy: - skrah
2014-07-31 08:58:54skrahsetnosy: + skrah
messages: + msg224387

components: + Interpreter Core
stage: needs patch
2014-07-31 08:31:11stangelandclcreate