Python 3

John Barstow jbowtie at amathaine.com
Tue Jun 22 23:32:56 BST 2010


On Wed, Jun 23, 2010 at 9:47 AM, Robert Collins
<robertc at robertcollins.net> wrote:
> I've done a couple of trivial patches moving in the direction of being
> able to play with Python 3 and bzr.
>

Hmmm. These all seem to ignore the porting guidelines
[http://docs.python.org/py3k/whatsnew/3.0.html#porting-to-python-3-0]
There's actually very little we would need to do if we followed those
guidelines.

> I'd like to document some changes to our coding style, to make
> eventual Python 3 migration easier - but they aren't all no-brainers
> so I thought I'd raise stuff here and we can discuss it.
>
> The no brainers (simple, not terribly ugly, not used a lot in our code):
> octal 0666 ->0o666
> print foo, bar -> print("%s %s" % (foo, bar))
> exec foo, locals() -> exec(foo, locals())
>

These are handled by the numliterals, print, and exec fixers. If we
change our coding style we will need to disable these fixers when
using 2to3.


> Somewhat ugly - we're going to see a lot of these:
> except Foo, e:
> ->
> except Foo:
>    e = sys.exc_info()[1]
>

Shouldn't that be "except Foo as e:"? Handled by the except fixer.

> bytestrings:
> This has the potential to slow load times slightly: in 3 to get a
> bytestring one says b'foo', but you can't do that in 2, so for
> single-source compatibility it gets written as:
>  _b('foo')
>
> which on 2 is a no-op, and on 3 reencodes using latin-1 (so everything
> works). Or we could split out separate files to import on 2 and 3, but
> I think the extra seek would make it a wash perf wise.

Well, Python 2.6 supports bytestring literals, so you really only need
to do this if you're committed to supporting 2.4/2.5 going forward.
Personally I think it would make sense to simply drop support for
anything older than 2.6 when we decide we want to start supporting
3.x, then you could just use bytestring literals.

>
> really ugly, but very few occurences:
>    raise type, val, tb
> ->
>    bzrlib.util.py._builtin._reraise(type, val, tb)
>

The raises fixer will rewrite this as "raise
type(val).with_traceback(tb)"; personally I find your proposal much
uglier.

So I guess that makes my counterproposal to adopt a minimum
requirement of Python 2.6 and use 2to3 to until such time as we drop
2.x support.



More information about the bazaar mailing list