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: complete support for struct packing/unpacking
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, belopolsky, skrah
Priority: normal Keywords:

Created on 2012-09-03 16:24 by belopolsky, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg169771 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2012-09-03 16:24
Starting with the example in memoryview documentation:

>>> from ctypes import BigEndianStructure, c_long
>>> class BEPoint(BigEndianStructure):
...     _fields_ = [("x", c_long), ("y", c_long)]
...
>>> point = BEPoint(100, 200)
>>> a = memoryview(point)

I am trying to unpack the resulting view:

>>> a.tolist()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NotImplementedError: memoryview: unsupported format T{>l:x:>l:y:}

>>> struct.unpack_from(a.format,a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: bad char in struct format

>>> struct.unpack_from('>ll',a)
(0, 100)


It looks like there is one or more bugs in play here.
msg169777 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-09-03 16:53
It's deliberately not implemented (and documented): Since memoryview is a 
complete rewrite, I tried go easy on new features in order to facilitate
review.

My plan was to implement struct packing/unpacking in 3.3.1 in the same
manner as in the new equality function.


The struct error you see is tracked in #3132.
msg169782 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2012-09-03 16:59
What about

>>> struct.unpack_from('>ll',a)
(0, 100)

shouldn't that return (100, 200)?
msg169789 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2012-09-03 17:43
'>' in struct syntax implies "standard size" for 'l', which is 4 bytes.
ctypes uses machine size for c_long, so on an LP64 machine you can unpack 
the data as:

>>> struct.unpack_from('>qq', a)
(100, 200)
msg229296 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2014-10-14 14:58
Closing, since the main request is tracked in #3132.
History
Date User Action Args
2022-04-11 14:57:35adminsetgithub: 60061
2014-10-14 15:21:10skrahsetstatus: open -> closed
2014-10-14 14:58:02skrahsetresolution: duplicate
messages: + msg229296
stage: needs patch ->
2012-09-04 07:51:36Arfreversetnosy: + Arfrever
2012-09-03 17:43:36skrahsetmessages: + msg169789
2012-09-03 16:59:35belopolskysetmessages: + msg169782
2012-09-03 16:53:53skrahsetversions: - Python 3.4
title: memoryview of a ctypes struct has incompatible invalid format -> memoryview: complete support for struct packing/unpacking
messages: + msg169777

components: + Interpreter Core
type: behavior -> enhancement
stage: needs patch
2012-09-03 16:24:57belopolskycreate