Author liyu
Recipients liyu
Date 2008-05-27.15:56:07
SpamBayes Score 0.0404066
Marked as misclassified No
Message-id <1211903770.4.0.523120419892.issue2981@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2008-05-27 15:56:11liyusetspambayes_score: 0.0404066 -> 0.0404066
recipients: + liyu
2008-05-27 15:56:10liyusetspambayes_score: 0.0404066 -> 0.0404066
messageid: <1211903770.4.0.523120419892.issue2981@psf.upfronthosting.co.za>
2008-05-27 15:56:09liyulinkissue2981 messages
2008-05-27 15:56:07liyucreate