























In this post by Dirk Makowski, he explains how he added preliminary Python 3.x support to the forthcoming Pelican 3.2 release.
When referring to Pelican's "unified" codebase, this means the code runs without changes (or requiring 2to3) on both Python 2.x and 3.x. Specifically, this unified codebase is tested on Python 2.7 and 3.2.
At the time of this writing all tests pass, whether manually invoked via python -m unittest discover within manually-created virtual environments or via tox, which automatically creates virtual environments for Python 2.7 and 3.2 and runs tests on both Python versions:
74 tests, 9 skips, no errors
During my first attempt to port Pelican to Python 3, I had used 2to3 and manually applied corrections to achieve syntactical correctness. But as the tests showed, more subtle bugs in the encoding of strings remained (e.g. outputting b'...' instead of '...'). Also, the code was suitable for Python 3 only and would not run properly on Python 2.
Building on the lessons learned with 2to3, the current in-development version of Pelican now provides a single codebase for Python 2 and 3. The following section lists some hints about what had to be changed in order to achieve this. These hints may also be valuable for anyone who wants to contribute new Pelican features or bug fixes, since all Pelican code submissions must now run on both Python versions.
Pelican core depends on feedgenerator. To run on Py3k, you will need feedgenerator 1.5, which also now has a unified codebase and is available on PyPI.
BeautifulSoup had to be upgraded to bs4 (only bs4 supports Py3k), which has some consequences:
On Python 2 you still can use all plugins and their dependencies as they are. But if you want to use Pelican plugins on Python 3, you must seek out and install compatible packages yourself.
For Typogrify and SmartyPants, on which Typogrify depends, I provide ready-made 2to3'd code:
For webassets, I also have 2to3'd code available:
But webassets still has issues. One is that the less-css compiler is not correctly invoked when I build the blog (e.g., with make html); I have to manually invoke the compiler afterwards:
lessc themes/pymblog/static/less/pymblog.less > output/theme/css/pymblog.css
Be aware that the 2to3'd code of aforementioned libraries runs on Python 3 only.
Testing on Python 2 is straightforward.
On Python 3, if you have installed the Python 3.x-compatible versions of the plugins, like shown in the previous chapter, manual testing with python -m unittest discover is also straightforward.
However, you must tell tox to use those Py3k libraries. If you forget this, tox will pull the regular packages from PyPI and the tests will fail.
Tell tox about the local packages thusly: enter the source directory of SmartyPants and run tox there. Do this again for Typogrify and webassets. SmartyPants and Typogrify do not have real tests, and webassets will fail noisily, but as a result we get these libraries neatly packaged in tox's distshare directory. And this is what we need to order to run tox for Pelican.
See also the comments in tox.ini for more detail.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。