2to3
As a learning exercise, I tried converting the Universal Feed Parser to Python 3.0. I picked it because it is a relatively self contained code base that I am familiar with, one that is actively in use, and one that has seen the wear and tear of dealing with compatibility (and the need to monkeypatch the occasional bug) of a number of Python releases.
$ svn co http://svn.python.org/projects/sandbox/trunk/2to3/ $ cd 2to3 $ python refactor.py -w ../feedparser/feedparser.py > feedparser.2to3.diff root: Generating grammar tables from /home/rubys/svn/2to3/PatternGrammar.txt root: Writing grammar tables to /home/rubys/svn/2to3/PatternGrammar.pickle ../feedparser/feedparser.py: At line 3091: You should use a for loop here RefactoringTool: Files that were modified: RefactoringTool: ../feedparser/feedparser.py $ python refactor.py -w ../feedparser/feedparsertest.py > feedparsertest.2to3.diff RefactoringTool: Files that were modified: RefactoringTool: ../feedparser/feedparsertest.py
A few manual changes later, and 91% of the tests pass. I’m confident that with a little more work, I could quickly get that to 99%, perhaps even to 100%.
The Good
- Python 3.0 feels very natural and comfortable, at least to this Python programmer. The language feels clean and new again. No more “new-style” classes. Things that should have been iteraters all along now are. Python 3.0 is full of those kind of small changes.
- No bugs were found in the language, though some minor issues (noted below) were found in various parts of the runtime.
- The
2to3conversion, even at this first alpha sandbox state, was painless and efficient. It also didn’t reduce the readability or break the functionality of the code produced. - The places where manual attention is required did generally seem to be places where human attention is required.
The Bad
- The Unicode change (while *VERY* welcome) is going to hit people hard. While the UFP code has greater than its fair share of such code, the fact that people will no longer simply be able to
open(file).read()unless that file isutf-8is going to be a big shock. - Some of the python3 libraries don’t seem to have internalized the Unicode changes yet.
base64.decodestringcan only handlebytes, not characters (why?). More troublesome to me is that I couldn't getxml.sax.xmlreader.InputSourceto work with aio.BytesIO(aStringIOwork-alike forbytes), but instead only seemed to work with Characters — something that is at odds with proper handling of XML. However, I do realize that this is an alpha, and fully believe that these issues will be worked out. - The test code for UFP relies on
eval, and some portion of those strings — ones that can never be automatically handled by a2to3migration tool — rely on Python 2.x specific syntax. Ultimately it might be worth considering introducing anpython2module with functions likeevalthat can be used to ease migration. - The places that the
2to3migration tool can’t currently handle, and perhaps never will be able to handle, will often require somebody who has an understanding and a history with the code base. Such people aren’t always available. I’m not sure what can be done about that.
The Ugly
- 3.0 will only get better. I’m highly confident that the issues listed above will be either eliminated or mitigated. The one question that this exercise raised for me that I can’t seem to shake is this: what is the role of 2.6? If it introduces new stuff that makes the migration easier, I can’t see libraries like the feedparser making much use of them until it is 2.5 and 2.4 and even earlier versions of Pythons are pretty much a thing of the past. I’m sure I’m missing something here, but I just don’t see it.
Imperfection - Martin Jansen
Last Friday, Guido van Rossum released the first alpha version of Python 3.0 aka Python 3000 . Judging by the changelog , the Python developers got rid of a number of annoyances and inconsistencies in order to make the language more cleaner...Excerpt from Planet PHP at
How would one do a <code>open(file).read()</code> where file is a non-unicode file in 3000?
Posted by Henrik Lied at
I probably should mention that as ASCII is a proper subset of utf-8, open(file).read() will likely work for most of your test data and wait to fail until after you deploy, probably as a result of somebody copy/pasting so-called “smart quotes”.
Also, open(file,"rb").read() will return raw bytes.
Sam Ruby: 2to3
Sam Ruby: 2to3 . Sam’s report on an attempt to port the Universal Feed Parser to Python 3.0. The 2to3 tool does most of the work, but it seems the unicode changes can be pretty tricky....Excerpt from Simon Willison's Weblog at
Sam Ruby: 2to3
Sam Ruby: 2to3 . Sam’s report on an attempt to port the Universal Feed Parser to Python 3.0. The 2to3 tool does most of the work, but it seems the unicode changes can be pretty tricky....Excerpt from PythonLovers.com at
Sam Ruby's report on porting the Universal Feed Parser to Python 3.0 using the 2to3 conversion tool
[link] [more]...Excerpt from reddit.com: programming - newest submissions at
Sam Ruby: 2to3
Simon Willison : Sam Ruby: 2to3 - Sam Ruby: 2to3. Sam’s report on an attempt to port the Universal Feed Parser to Python 3.0. The 2to3 tool does most of the work, but it seems the unicode changes can be pretty tricky....Excerpt from HotLinks - Level 1 at
La alpha 1 di Python 3000
Scopro dal blog di Guido van Rossum che ieri è stata rilasciata la 3.0a1 di Python – famoso e amato linguaggio di programmazione multiparadigma*. Su python.org (sito ufficiale) trovate la pagina della release. E’ inoltre disponibile...Excerpt from Il Tao dei blog at
The one question that this exercise raised for me that I can’t seem to shake is this: what is the role of 2.6?
Sure, it’s about migration. The process description is such that all code shall be adapted in Python 2.6 with appropriate hints and none in Python 3.0. Together with these hints the conversion process shall become fully automized. I didn’t examine 2to3 yet and I’m not sure how the the user provides hints to the conversion tool. It might even be that Python 2.6 introduces new language syntax for this purpose only.
Posted by Kay Schluehr atThe process description is such that all code shall be adapted in Python 2.6 with appropriate hints and none in Python 3.0.
This works well for top level applications, but not so well for library developers for which the library is not part of Python itself. Specifically, it would require a number of libraries I depend upon (e.g., httplib2, feedparser, html5lib) to stop supporting Python 2.5.
Posted by Sam Ruby at4 Sep 2007
Sam Ruby tryied to port the Univesal Feed Parser to Py3k. 91% of the tests pass, what is quite nice. UFP is a great piece of code, and one of the first things I would miss. PyGTK people started talking about the migration - this will be very...Excerpt from Advogato blog for eopadoan at
Sam Ruby's report on porting the Universal Feed Parser to Python 3.0 using the 2to3 conversion tool
[link] [comments]...Excerpt from reddit.com: reddit.com: ネットデハッケン at
Sam Ruby on python 3
[link]...Excerpt from del.icio.us/tag/python at