[XML-SIG] Extending the xml package

Fred L. Drake, Jr. fdrake@beopen.com
Thu, 27 Jul 2000 01:31:22 -0400 (EDT)


  As promised, I brought up the package extension issue at today's
PythonLabs meeting.  We decided that there are two interesting cases
for package importing involved here.
  The first is package extension -- allowing one package to extend
another.  We basically agreed that the Java model got this right, with
the issue of multiple __init__ modules being a serious problem for
Python (it's not clear what the right way to deal with multiple
__init__ modules; you want to execute all of them, and the current
implementation doesn't lend itself to this).  This is the approach
we've discussed here before.
  Another possibility is providing an extended replacement for the
standard package.  This doesn't sound like it makes sense given that
using the same name creates order dependencies for sys.path, and the
current setup would be wrong for overriding a standard package with a
package installed in site-packages or a user's or application's
private library.
  However, this actually appears to be the most reasonable if we want
to be able to include bug fixes in the "enhanced" version of the
package, and doesn't require weird hacking on distutils.  It does
require that the package that can be overridden in this way be written
to support this.
  Here's how to do it:
  Deploy the "xml" package in the standard library.  Create an
"_xmlplus" package (PyXML) which provides all of the facilities from
the standard library and any extensions.  The "_xmlplus" package can
be treated as any other package with distutils.
  In the __init__.py for the "xml" package, include the following
code:

------------------------------------------------------------
if __name__ == "xml":
    try:
        import _xmlplus
    except ImportError:
        pass
    else:
        import sys
        sys.modules[__name__] = _xmlplus
------------------------------------------------------------

  Yes, this works.
  The leading test for __name__ is useful to allow the same file to be
used for both the xml and _xmlplus packages.
  The PyXML package (providing _xmlplus) could continue to be the
leading-edge development package with all the bells and whistles, and
portions adopted into the standard library could be updated before a
Python release.
  Guido also suggested looking at the version-handling code from Pmw,
but I don't know how valuable that would be.
  Comments?


  -Fred

-- 
Fred L. Drake, Jr.  <fdrake at beopen.com>
BeOpen PythonLabs Team Member