[Python-Dev] Py3k bytes type in 2.x (Re: nonlocal keyword in 2.x?)

Nick Coghlan ncoghlan at gmail.com
Wed Nov 4 14:20:55 CET 2009

M.-A. Lemburg wrote:
> Nick Coghlan wrote:
>> Lennart Regebro wrote:
>>> I also would really like to see a real port of the bytes class to 2.6,
>>> but I have a vague memory that there was some reason that wouldn't
>>> work.
>> Not so much that it wouldn't work, but that the interfaces to support
>> using it effectively really aren't there - lots of areas in the standard
>> library needed to be tweaked to cope with bytes objects in 3.x.
>> Generally speaking, the "bytes = str" trick represents a reasonable
>> compromise as the APIs that you would pass a bytes object to in 3.x
>> expect an 8-bit str instance in 2.x.
> Could you please add such hints, tricks and tips to the wiki
> page:
> 	http://wiki.python.org/moin/PortingToPy3k

Done (although the task of figuring out how to get the wiki to display
code properly defeated me... the normal Python documentation syntax for
it seemed to give the wiki's ReST interpreter fits).

I also mentioned the trick someone mentioned about "from __future__
import unicode_literals" not changing the meaning of 'str' since it only
alters the parser but not the builtins.

In writing it up, it occurred to me that having that kind of thing in a
"py3_compat" compatibility module (to be used as, e.g., "from py3_compat
import str, bytes") would not only make it easier to use in multiple
modules, but also easier for 2to3 to remove when forward porting.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list