[Python-Dev] Migration from Python 2.7 and bytes formatting

Neil Schemenauer nas at arctrix.com
Fri Jan 17 11:49:42 CET 2014


As I see it, there are two separate goals in adding formatting
methods to bytes.  One is to make it easier to write new programs
that manipulate byte data.  Another is to make it easier to upgrade
Python 2.x programs to Python 3.x.  Here is an idea to better
address these separate goals.

Introduce %-interpolation for bytes.  Support the following format
codes to aid in writing new code:

    %b: insert arbitrary bytes (via __bytes__ or Py_buffer)

    %[dox]: insert an integer, encoded as ASCII

    %[eEfFgG]: insert a float, encoded as ASCII

    %a: call ascii(), insert result

Add a command-line option, disabled by default, that enables the
following format codes:

    %s: if the object has __bytes__ or Py_buffer then insert it.
        Otherwise, call str() and encode with the 'ascii' codec

    %r: call repr(), encode with the 'ascii' codec

    %[iuX]: as per Python 2.x, for backwards compatibility

Introducing these extra codes and the command-line option will
provide a more gradual upgrade path.  The next step in porting could
be to examine each %s inside bytes literals and decide if they
should either be converted to %b or if the literal should be
converted to a unicode literal.  Any %r codes could likely be safely
changed to %a.



More information about the Python-Dev mailing list