[Python-ideas] format specifier for "not bytes"

Daniel Holth dholth at gmail.com
Sat Jul 6 01:01:13 CEST 2013


On Wed, Aug 29, 2012 at 6:30 PM, Mathias Panzenböck
<grosser.meister.morti at gmx.net> wrote:
> On 08/27/2012 04:34 PM, Barry Warsaw wrote:
>>
>> On Aug 25, 2012, at 09:16 AM, Nick Coghlan wrote:
>>
>>> A couple of people at PyCon Au mentioned running into this kind of issue
>>> with Python 3. It relates to the fact that:
>>> 1. String formatting is *coercive* by default
>>> 2. Absolutely everything, including bytes objects can be coerced to a
>>> string, due to the repr() fallback
>>>
>>> So it's relatively easy to miss a decode or encode operation, and end up
>>> interpolating an unwanted "b" prefix and some quotes.
>>>
>>> For existing versions, I think the easiest answer is to craft a regex
>>> that
>>> matches bytes object repr's and advise people to check that it *doesn’t*
>>> match their formatted strings in their unit tests.
>>>
>>> For 3.4+ a non-coercive string interpolation format code may be
>>> desirable.
>>
>>
>> Or maybe just one that calls __str__ without a __repr__ fallback?
>>
>
>>>> b'a'.__str__()
> "b'a'"
>
> __str__ still returns the bytes literal representation.
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas

This is now a patch at http://bugs.python.org/issue18373. The user can
call sys.getbyteswarning() and sys.setbyteswarning(integer) to control
whether str(bytes) warns in the current thread, but you also have to
adjust the warnings module for it to be useful.


More information about the Python-ideas mailing list