[Python-ideas] Binary f-strings
Guido van Rossum
guido at python.org
Fri Oct 2 18:00:56 CEST 2015
Bingo. IMO the exact same arguments that show why f'{x} {y}' is better than
'%s %s' % (x, y) applies to byte strings. It would be totally acceptable if
it only took bytes (and bytearray, and memoryview) and numbers (which we
can guarantee are rendered in ASCII only).
On Fri, Oct 2, 2015 at 8:43 AM, Sven R. Kunze <srkunze at mail.de> wrote:
> On 02.10.2015 16:26, Eric V. Smith wrote:
>
>> bf'{foo}'
>>
>> Might succeed or fail, depending on what foo returns for __format__. If
>> foo is 'bar', it succeeds. If it's '\u1234', it fails.
>>
>
> I know a lot of functions that fail when passing the wrong kind of
> arguments. What's so wrong with it?
>
> But some of the other arguments are making me think bf'' is a bad idea,
>> so now I'm leaning towards not implementing it.
>>
>
> I see. Do you think of an alternative solution?
>
>
> I was digging deeper into the matter of binary/byte strings formatting in
> order to sympathise why {} is not usable for binary protocols. Let's look
> at this practical example
> https://blog.tox.chat/2015/09/fuzzing-the-new-groupchats/ I hope the tox
> protocol fully qualifies as a wireframe protocol. What he's trying to do it
> is to fuzzing his new groupchat implementation by creating more-or-less
> random packages and feed them into tox core to see if it breaks.
>
> He conveniently uses this type of syntax to describe the structure of the
> first header:
>
> Header 1: [ Packet ID (1 b) | Chat ID hash (4 b) | Sender PK (32
> b) | nonce (24 b) ]
>
>
> Interested in writing a fuzzer, I would find the following really helpful
> as it mirrors the description within his blog post:
>
> header1 = bf'{packet_id}{chat_id}{sender_pk}{nonce}'
>
> # which should be the same as
>
> header1 = b'%b%b%b%b' % (packet_id, chat_id, sender_pk, nonce)
>
> I wouldn't mind specifying the encoding for all non-byte-string arguments.
> Why? Because I would be working with bytes anyway, so no formatting (as in
> format()) would be necessary in the first place. However, I like the syntax
> for specifying the structure of (byte) strings.
>
>
> Does this makes sense?
>
>
> Best,
> Sven
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
--
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20151002/33147b2e/attachment.html>
More information about the Python-ideas
mailing list