As part of our support for Python 3.0 we have the following code in
markdown/inlinepatterns.py:
if sys.version >= "3.0":
from html import entities as htmlentitydefs
else:
import htmlentitydefs
We had to do this because of a bug in the 2to3 tool which shipped with
Python 3.0 where is failed to fix the import. This bug was fixed with
Python 3.1 and its 2to3 tool makes the following changes:
import sys
if sys.version >= "3.0":
from html import entities as htmlentitydefs
else:
- import htmlentitydefs
+ import html.entities
def build_inlinepatterns(md_instance, **kwargs):
@@ -378,7 +378,7 @@
def codepoint2name(code):
"""Return entity definition by code, or the code if
not defined."""
- entity = htmlentitydefs.codepoint2name.get(code)
+ entity = html.entities.codepoint2name.get(code)
if entity:
return "%s%s;" % (util.AMP_SUBSTITUTE, entity)
else:
Of course, the problem is that the import line for pre-3.0 was
changed, but not the import within the version >= 3.0 check. And as
the code that actually uses the library we're importing is changed to
match the new lib name, we are now calling a lib that hasn't actually
been imported. Well actually, I suspect it never gets that far because
it fails with an import error when trying to import the old lib.
So my question is, how should we fix this?
A) Drop support for 3.0 (but keep supporting 3.1) as the Python
project itself has dropped support for 3.0 immediately upon release of
3.1.
B) Fix the imports so that in 3.1 it will import html.entities twice
(silly, but it works)
C) Fix the test to only specifically check for 3.0 (if sys.version == "3.0").
Interestingly, we got a bug report [1] on this when someone ran the
2to3 tool (from Python 3.1), got some errors, looked at the code and
thought it was strange. Only after I figured out this was the order of
events did their report make full sense. If we go with either option B
or C, while we will be avoiding the error, the code will still be just
as confusing to read - although option C seems better in this case.
Personally, I would prefer option A. I remember reading once prior to
the release of Python 3.1 that people should still use 3.1's beta 2to3
tool even when converting to run on Python 3.0. Option A is the only
sensible solution in that scenario. In fact, if someone really needs
markdown on Python 3.0 (perhaps a host won't install 3.1 for some
reason) we can just suggest that they use 3.1's 2to3 tool before
uploading to their host.
Any input?
[1]: http://www.freewisdom.org/projects/python-markdown/Tickets/000066
--
----
\X/ /-\ `/ |_ /-\ |\|
Waylan Limberg
|