[XML-SIG] import tricks (was: XML in Python 1.6 (PROPOSAL))

Greg Stein gstein@lyra.org
Fri, 30 Jun 2000 14:56:40 -0700


On Thu, Jun 29, 2000 at 02:31:24PM -0600, Uche Ogbuji wrote:
>...
> Somebody wrote this, but I didn't see the message and Uche didn't quote:
> >   Another possibility may be to have PyXML provide a package
> > "xmlextra" (or whatever), and xml.__init__ can check for it's presence
> > and "incorporate" it somehow:
> > 
> > ------------------------------------------------------------
> > try:
> >     import xmlextra
> > except ImportError:
> >     # not present
> >     pass
> > else:
> >     # add new subpackages
> >     __path__.append(os.path.dirname(xmlextra.__file__))
> >     del xmlextra
> > ------------------------------------------------------------
> > 
> >   There are two problems with this approach:
> > 
> >   1.  The packages/modules provided by xmlextra are importable by two
> >       names: xml.<foo> and xmlextra.<foo>.  I'm not entirely sure how
> >       much we should care about this one.
> 
> I wouldn't worry about it: we can either not publish the xmlextra package, or 
> we can explicitly forbid using it in the docs.

Agreed. I would also suggest renaming it _xmlextra to give that extra
indication of "don't get near this."

Also, reliance on __path__ might be a Bad Thing from a long-term standpoint.
It would be a lot better to simply do:

    try:
        import _xmlextra
    except ImportError:
        pass
    else:
        _xmlextra.setup_xml_package()

We would then do something like:

    def setup_xml_package():
        import xml.dom
	import xml.parser
        from _xmlextra.dom import super_dom, other-dom
	import _xmlextra.parser.other_parser
	xml.dom.super_dom = super_dom
	xml.dom.other_dom = other_dom
	xml.parser.other_parser = _xmlextra.parser.other_parser

This allows to "merge" the two trees. In the code posted above, _xmlextra
could not override "xml.dom" without completely replacing it. Also, by
locating setup_xml_package() in the _xmlextra package, the merging process
is definitely completely external (e.g. we can rev _xmlextra all the time
without changing the core).

xml.dom.super_dom.__file__ will point to _xmlextra, but TFB. People
shouldn't use __file__ (because it makes no sense when a module is yanked
from an archive).

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/