]> granicus.if.org Git - python/commitdiff
[Patch 101634]
authorMartin v. Löwis <martin@v.loewis.de>
Sun, 24 Sep 2000 21:31:06 +0000 (21:31 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Sun, 24 Sep 2000 21:31:06 +0000 (21:31 +0000)
xml.sax: Fix parse and parseString not to rely on ExpatParser
         Greatly simplify import logic by using __import__
saxutils: Support Unicode strings and files as parameters to
          prepare_input_source

Lib/xml/sax/__init__.py
Lib/xml/sax/saxutils.py

index 7c62ea1cd7e001c3b3e611f41e17866c7421c18d..1f1f58eba6b03a7cc68ab8417317ad3a3338fa28 100644 (file)
@@ -26,7 +26,7 @@ from _exceptions import SAXException, SAXNotRecognizedException, \
 
 
 def parse(source, handler, errorHandler=ErrorHandler()):
-    parser = ExpatParser()
+    parser = make_parser()
     parser.setContentHandler(handler)
     parser.setErrorHandler(errorHandler)
     parser.parse(source)
@@ -39,7 +39,7 @@ def parseString(string, handler, errorHandler=ErrorHandler()):
         
     if errorHandler is None:
         errorHandler = ErrorHandler()
-    parser = ExpatParser()
+    parser = make_parser()
     parser.setContentHandler(handler)
     parser.setErrorHandler(errorHandler)
 
@@ -87,29 +87,8 @@ if sys.platform[ : 4] == "java":
         return drv_module.create_parser()
 
 else:
-    import imp as _imp
-
-    def _rec_find_module(module):
-        "Improvement over imp.find_module which finds submodules."
-        path=""
-        for mod in string.split(module,"."):
-            if path == "":
-                info = (mod,) + _imp.find_module(mod)
-            else:
-                info = (mod,) + _imp.find_module(mod, [path])
-                
-            lastmod = _imp.load_module(*info)
-
-            try:
-                path = lastmod.__path__[0]
-            except AttributeError, e:
-                pass
-
-        return info
-
     def _create_parser(parser_name):
-        info = _rec_find_module(parser_name)
-        drv_module = _imp.load_module(*info)
+        drv_module = __import__(parser_name,{},{},['create_parser'])
         return drv_module.create_parser()
 
 del sys
index 8f8f42e811a60b67a22165c1f18ad49959b33022..3f130f3af5909fa3e6fee983ffd481d198c1bf2d 100644 (file)
@@ -3,10 +3,12 @@ A library of useful helper classes to the SAX classes, for the
 convenience of application and driver writers.
 """
 
-import os, urlparse, urllib
+import os, urlparse, urllib, types
 import handler
 import xmlreader
 
+_StringTypes = [types.StringType, types.UnicodeType]
+
 def escape(data, entities={}):
     """Escape &, <, and > in a string of data.
 
@@ -189,8 +191,12 @@ def prepare_input_source(source, base = ""):
     """This function takes an InputSource and an optional base URL and
     returns a fully resolved InputSource object ready for reading."""
     
-    if type(source) == type(""):
+    if type(source) in _StringTypes:
+        source = xmlreader.InputSource(source)
+    elif hasattr(source, "read"):
+        f = source
         source = xmlreader.InputSource(source)
+        source.setByteStream(f)
 
     if source.getByteStream() == None:
         sysid = source.getSystemId()