[Python-bugs-list] [ python-Bugs-461674 ] struct 'p' format doesn't work (maybe)

noreply@sourceforge.net noreply@sourceforge.net
Sat, 15 Sep 2001 08:23:59 -0700


Bugs item #461674, was opened at 2001-09-14 14:32
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=461674&group_id=5470

Category: Python Library
Group: Python 2.1.1
Status: Open
Resolution: None
Priority: 5
Submitted By: Gregory Smith (gregsmith)
>Assigned to: Tim Peters (tim_one)
Summary: struct 'p' format doesn't work (maybe)

Initial Comment:
Doc says: """
The "p" format character can be used to encode a Pascal string. The first byte is the length of the 
stored string, with the bytes of the string following. If count is given, it is
used as the total number of bytes used, including the length byte. If the string passed in to pack() 
is too long, the stored representation is truncated. If the string is too
short, padding is used to ensure that exactly enough bytes are used to satisfy the count. 
"""

The behaviour implied (to me, anyway) is
(1) pack('p','abc') -> '\x03abc'
(2) pack('3p','abc') ->'\x02ab'
(3) pack('5p','abc') ->'\x03abc\x00'
(4) pack('p','a') -> '\x01a'
(5) pack('p','') -> '\x00'
(6) pack('p','abcdefg')  -> '\x07abcdefg'

etc, etc. In fact, only (2) and (3) work like this; the rest return empty string.
and the corresponding unpack will return empty string.
"If count is given" This implies (arguably) that 'p' by itself is useful
and behaves as described in the first two sentences.
This variable-length string storage would be very useful and it
is too bad that it isn't supported. 
Of course, calcsize() would have to raise an exception if 'p' were
used without a count, or return the min size.



----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-09-15 08:23

Message:
Logged In: YES 
user_id=6380

It's a doc bug.  I didn't write the doc, but I did write the
code.


----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-09-14 19:15

Message:
Logged In: YES 
user_id=31435

Guido, is this just a doc bug?  A raw 'p' can't work the 
way Gregory hopes, as it would make calcsize() ill-defined 
(and so also break the way struct.pack is implemented).  
The way the code is written, the only result you can get 
out of a 'p' pack code without a count is a single '\x00' 
byte.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-09-14 16:48

Message:
Logged In: YES 
user_id=31435

Digging thru CVS, the code has always worked this way 
(contributed code introduced in structmodule.c rev 2.20).  
There are no tests of the 'p' code in the std test suite, 
so it's impossible to know for sure what the intent was.

I agree that the way it actually works appears useless.  
Assigned to me.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=461674&group_id=5470