On 2014-01-06 13:24, Victor Stinner wrote:
Hi,
bytes % args and bytes.format(args) are requested by Mercurial and Twisted projects. The issue #3982 was stuck because nobody proposed a complete definition of the "new" features. Here is a try as a PEP.
The PEP is a draft with open questions. First, I'm not sure that both bytes%args and bytes.format(args) are needed. The implementation of .format() is more complex, so why not only adding bytes%args? Then, the following points must be decided to define the complete list of supported features (formatters):
* Format integer to hexadecimal? ``%x`` and ``%X`` * Format integer to octal? ``%o`` * Format integer to binary? ``{!b}`` * Alignment? * Truncating? Truncate or raise an error? * format keywords? ``b'{arg}'.format(arg=5)`` * ``str % dict`` ? ``b'%(arg)s' % {'arg': 5)`` * Floating point number? * ``%i``, ``%u`` and ``%d`` formats for integer numbers? * Signed number? ``%+i`` and ``%-i``
I'm thinking that the "i" format could be used for signed integers and the "u" for unsigned integers. The width would be the number of bytes. You would also need to have a way of specifying the endianness. For example:
b'{:<2i}'.format(256) b'\x01\x00' b'{:>2i}'.format(256) b'\x00\x01'
Perhaps the width should default to 1 in the cases of "i" and "u":
b'{:i}'.format(-1) b'\xFF' b'{:u}'.format(255) b'\xFF' b'{:i}'.format(255) ValueError: ...
Interestingly, I've just been checking what exception is raised for some format types, and I got this:
'{:c}'.format(-1) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: %c arg not in range(0x110000)
Should the exception be OverflowError (probably yes), and should the message say "%c"?