[Python-Dev] PEP 461 - Adding % and {} formatting to bytes (original) (raw)

Michael Urman murman at gmail.com
Thu Jan 16 18:49:52 CET 2014


On Thu, Jan 16, 2014 at 11:13 AM, Neil Schemenauer <nas at arctrix.com> wrote:

A TypeError exception is what we want if the object does not support bytes formatting. Some possible problems:

- It could be hard to provide a helpful exception message since it is generated inside the format method rather than inside the bytes.mod method (in the case of a missing ascii method). The most common error will be using a str object and so we could modify the format method of str to provide a nice hint (use encode()).

The various format functions could certainly intercept and wrap exceptions raised by format methods. Once the core types were modified to expect bytes in format_spec, however, this may not be critical; format methods which delegate would work as expected, str could certainly be clear about why it raised, and custom implementations would be handled per comments I'll make on your second point. Overall I suspect this is no worse than unhandled values in the format_spec are today.

- Is there some risk that an object will unwittingly implement a format method that unintentionally accepts a bytes argument? That requires some investigation.

Agreed. Some quick armchair calculations suggest to me that there are three likely outcomes:

I'm uncertain whether this reflects all the scenarios we would care about.



More information about the Python-Dev mailing list