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.

Title: Extend struct.unpack to produce nested tuples
Type: Stage:
Components: Extension Modules Versions: Python 2.5
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: fabe3k, loewis, mfbarnes, rhettinger
Priority: normal Keywords: patch

Created on 2003-11-23 22:24 by mfbarnes, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
structmodule.diff mfbarnes, 2003-11-23 22:24
Messages (5)
msg44920 - (view) 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)')

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.
msg44921 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-12-20 03:57
Logged In: YES 

What are the use cases to warrant this change?
msg44922 - (view) Author: Matthew Barnes (mfbarnes) Date: 2004-12-20 06:43
Logged In: YES 

I suggested this change on Python-Dev last year
 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.
msg44923 - (view) Author: Fabian Bieler (fabe3k) Date: 2005-07-09 17:28
Logged In: YES 

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 
msg44924 - (view) Author: Martin v. Löwis (loewis) * (Python committer) 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.
Date User Action Args
2022-04-11 14:56:01adminsetgithub: 39590
2003-11-23 22:24:00mfbarnescreate