Issue 847857: Extend struct.unpack to produce nested tuples (original) (raw)
Created on 2003-11-23 22:24 by mfbarnes, last changed 2022-04-11 14:56 by admin. This issue is now closed.
Messages (5)
Author: Matthew Barnes (mfbarnes)
Date: 2003-11-23 22:24
This patch extends the struct.unpack format notation to be able to express groups of data with parentheses.
For example:
data = struct.pack('iiii', 1, 2, 3, 4) struct.unpack('i(ii)i', data) (1, (2, 3), 4)
Integral repeat counts can also be applied to groups.
struct.unpack('2(ii)', data) ((1, 2), (3, 4))
In addition, struct.calcsize also handles parentheses in format strings correctly.
struct.calcsize('4(ii)') 32
No changes were made to struct.pack. It still treats parentheses as illegal format characters.
I have not yet updated any documentation or tests associated with struct.calcsize or struct.unpack.
Author: Raymond Hettinger (rhettinger) *
Date: 2004-12-20 03:57
Logged In: YES user_id=80475
What are the use cases to warrant this change?
Author: Matthew Barnes (mfbarnes)
Date: 2004-12-20 06:43
Logged In: YES user_id=843448
I suggested this change on Python-Dev last year (http://mail.python.org/pipermail/python-dev/2003-November/040391.html). The use case I put forth at the time was as follows:
Use Case: I have a program written in C that contains a bunch of aggregate data structures (arrays of structs, structs containing arrays, etc.) and I'm transmitting these structures over a socket connection to a Python program that then unpacks the data using the struct module. Problem is that I have to unpack the incoming data as a flat sequence of data elements, and then repartition the sequence into nested sequences to better reflect how the data is structured in the C program. It would be more convenient to express these groupings as I'm unpacking the raw data.
Author: Fabian Bieler (fabe3k)
Date: 2005-07-09 17:28
Logged In: YES user_id=615150
I would like to see this patch applied to Python. This functionality would be very usefull for opening binary files with a given format. Is there a particular reason why this patch was not accepted?
Author: Martin v. Löwis (loewis) *
Date: 2006-11-19 08:41
This patch is now unfortunately out-of-date, since the struct module was heavily rewritten. Also, the original patch lacked documentation changes, and test cases.
mfbarnes, if you are still interested in this feature, please submit a new patch, which hopefully gets reviewed faster this time.
Rejecting this patch as out-of-date.