python / cpython

The Python programming language
https://www.python.org
Other
62.36k stars 29.95k forks source link

Struct module should accept arrays #68121

Open d0dd29c1-5052-4f55-a9c7-236065268d45 opened 9 years ago

d0dd29c1-5052-4f55-a9c7-236065268d45 commented 9 years ago
BPO 23933
Nosy @mdickinson, @meadori

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['type-feature'] title = 'Struct module should accept arrays' updated_at = user = 'https://bugs.python.org/gamaanderson' ``` bugs.python.org fields: ```python activity = actor = 'aeros' assignee = 'none' closed = False closed_date = None closer = None components = [] creation = creator = 'gamaanderson' dependencies = [] files = [] hgrepos = [] issue_num = 23933 keywords = [] message_count = 4.0 messages = ['240610', '240648', '240658', '240714'] nosy_count = 3.0 nosy_names = ['mark.dickinson', 'meador.inge', 'gamaanderson'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = 'enhancement' url = 'https://bugs.python.org/issue23933' versions = ['Python 2.7'] ```

d0dd29c1-5052-4f55-a9c7-236065268d45 commented 9 years ago

Correct me if I'm wrong, the struct module does not work with array of ints, floats etc (just work with char in the form of strings). I think it should since this are valid elements in C structs.

More specifically, consider I have this C struct

struct{
 int array[4];
};

I'm forced to do something like this: struct.pack('iiii', v1,v2,v3,v4) #'4i' is just the same as 'iiii'

I would like to do something like this: struct.pack('i[4]', [v1,v2,v3,v4])

Of course this is useful if I want to pack with zeros: struct.pack('i[4]', [0]*4)

db36b1c5-c904-4d78-ae55-de8a610fb115 commented 9 years ago

are you looking for:

struct.pack('i'*4, *[0]*4)

?

d0dd29c1-5052-4f55-a9c7-236065268d45 commented 9 years ago

@wolma, That would work in this simple example. But in a more complicated case this became inconvenient.

Actually I'm working with reading and writing in python an extremely C-oriented file-type (MDV). For that I represent C-structs as python dics (e.q {"array":[v1,v2,v3,v4]}), and because of this lack of arrays I'm forced to keep track if a key represents a single value or an array. It works, but I think everyone would benefit if struct could handle that simple task.

db36b1c5-c904-4d78-ae55-de8a610fb115 commented 9 years ago

I'm afraid you lost me and I do not see what your problem is here. Maybe you should raise this on one of the Python mailing lists (see https://www.python.org/community/lists/) ?