On Wed, 2012-07-25 at 13:16 -0700, Toshio Kuratomi wrote:
Last week I stumbled upon the fact that the newest python-docutils
does not
pass its unittests if PyXML is installed. Looking into the issue brought me
to the conclusion that retiring PyXML may be the best thing to do as rrakus
(the current PyXML maintainer) was interested in doing in February:
[...snip
excellent summary...]
* Need to explore other options such as stopping the python stdlib
from
replacing its xml module with PyXML and patching our packages that can't
be ported to import pyxml explicitly?
I did some investigating of how to do
this.
The replacement of xml with PyXML in the stdlib happens in
xml/__init__.py (see e.g. /usr/lib64/python2.7/xml/__init__.py): as
"xml" is imported, it tries to import _xmlplus, and if
_xmlplus.version_info >= (0, 8, 4) it replaces sys.modules[__name__]
with _xmlplus, effectively replacing "xml" with "_xmlplus".
For example, with PyXML installed:
>> import xml
>> xml.__file__
'/usr/lib64/python2.7/site-packages/_xmlplus/__init__.pyc'
Note how "xml" now refers to PyXML.
However, we can subvert this, for example, by hacking up _xmlplus's
version_info so that the test mentioned above fails. In a fresh
process:
>>> import _xmlplus
>>> _xmlplus.version_info = (0, 0, 0)
>> import xml
>> xml.__file__
'/usr/lib64/python2.7/xml/__init__.pyc'
and so "xml" is now really using the stdlib's "xml", despite PyXML
being
installed.
So maybe if we have code that really needs to use xml, not PyXML (even
if the latter is installed), how about the following code fragment?
# Neutralize _xmlplus, if present; we want "xml" to be the
# stdlib's "xml"
try:
import _xmlplus
# Prevent _xmlplus from being used in place of "xml"
_xmlplus.version_info = (0, 0, 0)
except ImportError:
pass # _xmlplus aka PyXML not installed
With the above:
>> import xml
>> xml.__file__
'/usr/lib64/python2.7/xml/__init__.pyc'
and it's using the stdlib's "xml".
This does potentially break PyXML for the lifetime of the process, so
you could run into nasty situations where some modules you're using
really want "xml" to be "xml", and others want "xml" to be
"_xmlplus",
but hopefully we won't run into that...
Hope this is helpful
Dave