[issue3982] support .format for bytes

Martin v. Löwis report at bugs.python.org
Sat Jul 11 17:52:17 CEST 2009


Martin v. Löwis <martin at v.loewis.de> added the comment:

> def chunk(block):
>     return format(len(block), 'x').encode('ascii') + b'\r\n' + block +
> b'\r\n'
> 
> You cannot convert to ascii at the end of the pipeline as there are
> bytes > 127 in the data blocks.

I wouldn't write it in such a complicated way. Instead, use

def chunk(block):
   return hex(len(block)).encode('ascii') + b'\r\n' + block + b'\r\n'

This doesn't need any format call, and describes adequatly how the
protocol works: send an ASCII-encoded hex length, send CRLF, send
the block, then send another CRLF. Of course, I would probably write
that into the socket right away, rather than copying it into a different
bytes object first.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3982>
_______________________________________


More information about the Python-bugs-list mailing list