[New-bugs-announce] [issue2981] confusing action of struct.pack and struct.unpack with fmt 'p'

Yu LI report at bugs.python.org
Tue May 27 17:56:10 CEST 2008


New submission from Yu LI <liyu1981 at gmail.com>:

As documented, build in module struct has two format for string objects,
such as 's' 'p'. They suggest following actions

>>> struct.pack('5s', 'hello')
'hello'
>>> struct.pack('6p', 'hello')
'\x05hello'

However, the second action really confuses people. In the documentation:
the "p" format character encodes a "Pascal string", meaning a short
variable-length string stored in a fixed number of bytes. So people
naturally assumes following action

>>> struct.pack('p', 'hello')
'\x05hello'

which makes more sense. 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'? Also the the bigger
number (6) before 'p' makes people confuse. Why should it be string size
+ 1? If we know there is a padding character and the string size, why
not struct.unpack('x5s', abuf) instead?

So the suggestion is to modify the behavior of format string 'p' to be
the same as people's intuition. In detail, the actions should be

>>> s = struct.pack('p', 'hello')
'\x05hello'
>>> struct.unpack('p', s)
('hello',)

And also these actions should be consist with struct.pack_into and
struct.unpack_from

----------
components: Library (Lib)
messages: 67411
nosy: liyu
severity: normal
status: open
title: confusing action of struct.pack and struct.unpack with fmt 'p'
type: behavior
versions: Python 2.5

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2981>
__________________________________


More information about the New-bugs-announce mailing list