[Python-Dev] stupid package tricks

Phillip J. Eby pje at telecommunity.com
Mon Dec 12 22:29:36 CET 2005


At 10:03 PM 12/12/2005 +0100, Fredrik Lundh wrote:
>the xml/__init__.py file contains a cute little hack that overrides
>the *entire* xml subtree with stuff from PyXML, if available.
>
>the code basically does
>
>     import _xmlplus
>     sys.modules[__name__] = _xmlplus
>
>(exception handling and version checks not shown).
>
>however, this means that as things are right now, xml.etree will
>simply disappear if the user has PyXML on the machine.
>
>what's the best way to fix this?  the obvious fix is of course to do
>something like
>
>     import _xmlplus
>     import xml.etree
>     _xmlplus.etree = xml.etree
>     sys.modules[__name__] = _xmlplus
>
>but I have to admit that I'm no expert on package internals, so I
>might be missing something here.  will the above solution work in
>all cases?  is there some better way to do it?

I'd suggest:

     import _xmlplus
     _xmlplus.__path__.extend(__path__)
     sys.modules[__name__] = _xmlplus

This ensures that any modules or packages inside 'xml' that aren't 
explicitly overridden by _xmlplus will still be available.



More information about the Python-Dev mailing list