[Python-Dev] RFC: PEP 460: Add bytes % args and bytes.format(args) to Python 3.5 (original) (raw)

Victor Stinner victor.stinner at gmail.com
Mon Jan 6 14:24:50 CET 2014


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):

HTML version of the PEP: http://www.python.org/dev/peps/pep-0460/

Inline copy:

PEP: 460 Title: Add bytes % args and bytes.format(args) to Python 3.5 Version: RevisionRevisionRevision Last-Modified: DateDateDate Author: Victor Stinner <victor.stinner at gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 6-Jan-2014 Python-Version: 3.5

Abstract

Add bytes % args operator and bytes.format(args) method to Python 3.5.

Rationale

bytes % args and bytes.format(args) have been removed in Python 2. This operator and this method are requested by Mercurial and Twisted developers to ease porting their project on Python 3.

Python 3 suggests to format text first and then encode to bytes. In some cases, it does not make sense because arguments are bytes strings. Typical usage is a network protocol which is binary, since data are send to and received from sockets. For example, SMTP, SIP, HTTP, IMAP, POP, FTP are ASCII commands interspersed with binary data.

Using multiple bytes + bytes instructions is inefficient because it requires temporary buffers and copies which are slow and waste memory. Python 3.3 optimizes str2 += str2 but not bytes2 += bytes1.

bytes % args and bytes.format(args) were asked since 2008, even before the first release of Python 3.0 (see issue #3982).

struct.pack() is incomplete. For example, a number cannot be formatted as decimal and it does not support padding bytes string.

Mercurial 2.8 still supports Python 2.4.

Needed and excluded features

Needed features

The feature set is minimal to keep the implementation as simple as possible to limit the cost of the implementation. str % args and str.format(args) are already complex and difficult to maintain, the code is heavily optimized.

Excluded features:

Unknown:

bytes % args

Formatters:

bytes.format(args)

Formatters:

Examples

Criticisms

References

Copyright

This document has been placed in the public domain.

.. Local Variables: mode: indented-text indent-tabs-mode: nil sentence-end-double-space: t fill-column: 70 coding: utf-8 End:



More information about the Python-Dev mailing list