[issue2981] confusing action of struct.pack and struct.unpack with fmt 'p'

2010-06-14 Thread Mark Dickinson

Mark Dickinson  added the comment:

I think you're misunderstanding how the 'p' format works.

> Otherwise, why people should use format 'p'?
> Either when you struct.pack or struct.unpack you have to know the size
> of string at first, why not turn to format 's'?

No;  you don't need to know the size of the string beforehand;  you just need 
to know the *maximum* size of the string;  the number of bytes allocated to 
store that string.  For example (Python 2.6):

>>> import struct
>>> s = struct.Struct('20p')  # variable-length string stored in 20 bytes
>>> s.pack('abc')
'\x03abc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> s.unpack(_)
('abc',)
>>> s.pack('abcdef')
'\x06abcdef\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>>> s.unpack(_)
('abcdef',)

Note that the packed sizes are the same (20 bytes each time), but you can pack 
and unpack any (byte)string of length up to 19 bytes, without needing to know 
its length beforehand.

Handling true variable-length fields is really outside the scope of the struct 
module.

--
resolution:  -> rejected
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2981] confusing action of struct.pack and struct.unpack with fmt 'p'

2010-06-14 Thread Mark Dickinson

Changes by Mark Dickinson :


--
nosy: +mark.dickinson

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2981] confusing action of struct.pack and struct.unpack with fmt 'p'

2009-05-13 Thread Daniel Diniz

Changes by Daniel Diniz :


--
priority:  -> normal
type: behavior -> feature request
versions: +Python 2.7, Python 3.2 -Python 2.5

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2981] confusing action of struct.pack and struct.unpack with fmt 'p'

2008-06-07 Thread Mark Hammond

Mark Hammond <[EMAIL PROTECTED]> added the comment:

What should struct.calcsize() do with a 'p' format string?

--
nosy: +mhammond

___
Python tracker <[EMAIL PROTECTED]>

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2981] confusing action of struct.pack and struct.unpack with fmt 'p'

2008-05-28 Thread John Stowers

John Stowers <[EMAIL PROTECTED]> added the comment:

I Agree. AIUI the benefit of pascal strings when transmitted is that the
decoder does not need to know its length ahead of time. The requirement
that the unpack pascal format string be given the length of the string
makes the implementation here useless

--
nosy: +nzjrs

___
Python tracker <[EMAIL PROTECTED]>

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com