Those two controversial 2nd & 3rd paragraphs of my ch 1

Steve Holden steve at holdenweb.com
Wed Jan 13 21:04:03 CET 2010


Daniel Fetchinson wrote:
>>> Also, I would replace
>>>
>>> "in practice it’s very hard to create programs"
>>>
>>> with
>>>
>>> "in practice it’s very hard to create complex programs"
>>>
>>> because for small programs it's very possible to write code that will
>>> work with both python 2 and 3. The question is of course what program
>>> is simple/small and what program is large/complex, but without
>>> qualifications your sentence is misleading, I think.
>> We had the example of Cheetah over in the other thread. Would you say that
>> Cheetah doesn't qualify as "complex" program?
> 
> One code base of cheetah works under python 2 and 3? I doubt it, but I
> could be wrong. What I can easily imagine is that somebody ported
> cheetah to python 3. In this case there are two code bases, one for
> python 2 and another for python 3. So it's not the same program that
> runs under python 2 and 3.
> 
> What the sentence in Alf's book is about is having the same code base
> working for both python 2 and 3.
> 
>> There are also other
>> examples, like Django.
> 
> Again, django has been ported to python 3, that's fine, everybody
> acknowledges that, but it's not the case that one code base works with
> both python versions.
> 
>> Often enough it's just a couple of cleanups and tiny
>> try-except blocks in the program header that enables running the program in
>> both Py2 and Py3.
> 
> Yes, this is true. But I'd say it's fair to say that with complex
> programs this is usually not the case, but I definitely not want to
> enter into a discussion into whether any given program is complex or
> simple. It's a matter of judgement and gut feeling it's pointless to
> argue about this too much.
> 
>> If it's more, it can usually be done using 2to3.
> 
> Again, 2to3 helps with porting, but does not help with having a single
> code base that will run unmodified on python 2 and 3, which is what
> Alf was writing about.
> 
>> So I
>> would change the above statement into something more like "for some
>> programs, especially large existing code bases, it can be hard to get the
>> code to work in both Python 2 and Python 3".
> 
> I actually agree with this sentence :)
> 
>> Nevertheless, it has been  done, more than once.
> 
> Example? Just to be clear I'm looking for an example where one given
> code runs on python 2 and 3 unmodified. I think django and cheetah
> doesn't count because they simply take their python 2 code, run it
> through 2to3 which gives them a python 3 code (I could be wrong
> though). Two codes for the two python versions.
> 
>> Personally, I don't see much value in deliberately trying to keep people
>> from porting their code to Py3 by producing underqualified statements like
>> the above.
> 
> Nobody is deliberately trying to keep people from porting! I think you
> misunderstand what is being said, these two statements are very
> different: (1) single code base working on both python versions (2)
> creating a second code from a code so that the second code works with
> python 3 and the first one with python 2. Statement (2) is about
> porting, statement (1) is about something else.
> 
> Having said all that I actually seriously doubt (probably in agreement
> with you) that Alf is able to write good and helpful material on the
> relationship between python 2 and 3, porting, migrating, etc, based on
> his emails :)
> 
[sigh]

OK, let's review the approved strategy for porting to 3.x from 2.x.

1. Ensure you have an acceptable test suite that verifies all major
functionality.

2. Port your code to the most recent version of 2.x (retaining backward
compatibility with older versions where necessary and practical)

3. Enable Python 3 warnings with the -3 argument and fix all
incompatibilities

4. Run 2to3 on your 2.x code and observe the errors obtained when
running the result under 3.x

5. Fix by paraphrasing your 2.x code to remove translation errors and
return to step 4, repeating until there are no more errors.

In other words, you are not recommended to aim for source compatibility,
you instead aim for a 2.x source which produces a correct 3.x source
when run through the 2to3 translator.

In this way you can continue to produce both 2.x and 3.x versions of
your projects form a single source tree until you no longer care about
2.x, at which point you simply use the output of the final 2to3
conversion as your onward source.

Of course this only works for pure Python code, but that's the majority
of Python code produced.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/




More information about the Python-list mailing list