summaryrefslogtreecommitdiff
path: root/Lib/xml/sax
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/xml/sax')
-rw-r--r--Lib/xml/sax/__init__.py8
-rw-r--r--Lib/xml/sax/expatreader.py11
-rw-r--r--Lib/xml/sax/saxutils.py7
-rw-r--r--Lib/xml/sax/xmlreader.py4
4 files changed, 21 insertions, 9 deletions
diff --git a/Lib/xml/sax/__init__.py b/Lib/xml/sax/__init__.py
index b161b1f07a..ef67ae67a6 100644
--- a/Lib/xml/sax/__init__.py
+++ b/Lib/xml/sax/__init__.py
@@ -33,8 +33,7 @@ def parse(source, handler, errorHandler=ErrorHandler()):
parser.parse(source)
def parseString(string, handler, errorHandler=ErrorHandler()):
- from io import BytesIO
-
+ import io
if errorHandler is None:
errorHandler = ErrorHandler()
parser = make_parser()
@@ -42,7 +41,10 @@ def parseString(string, handler, errorHandler=ErrorHandler()):
parser.setErrorHandler(errorHandler)
inpsrc = InputSource()
- inpsrc.setByteStream(BytesIO(string))
+ if isinstance(string, str):
+ inpsrc.setCharacterStream(io.StringIO(string))
+ else:
+ inpsrc.setByteStream(io.BytesIO(string))
parser.parse(inpsrc)
# this is the parser list used by the make_parser function if no
diff --git a/Lib/xml/sax/expatreader.py b/Lib/xml/sax/expatreader.py
index 3b63737457..98b5ca9539 100644
--- a/Lib/xml/sax/expatreader.py
+++ b/Lib/xml/sax/expatreader.py
@@ -232,9 +232,14 @@ class ExpatParser(xmlreader.IncrementalParser, xmlreader.Locator):
parser.ErrorColumnNumber = self._parser.ErrorColumnNumber
parser.ErrorLineNumber = self._parser.ErrorLineNumber
self._parser = parser
- bs = self._source.getByteStream()
- if bs is not None:
- bs.close()
+ try:
+ file = self._source.getCharacterStream()
+ if file is not None:
+ file.close()
+ finally:
+ file = self._source.getByteStream()
+ if file is not None:
+ file.close()
def _reset_cont_handler(self):
self._parser.ProcessingInstructionHandler = \
diff --git a/Lib/xml/sax/saxutils.py b/Lib/xml/sax/saxutils.py
index 1d3d0ecc5f..a69c7f7621 100644
--- a/Lib/xml/sax/saxutils.py
+++ b/Lib/xml/sax/saxutils.py
@@ -345,11 +345,14 @@ def prepare_input_source(source, base=""):
elif hasattr(source, "read"):
f = source
source = xmlreader.InputSource()
- source.setByteStream(f)
+ if isinstance(f.read(0), str):
+ source.setCharacterStream(f)
+ else:
+ source.setByteStream(f)
if hasattr(f, "name") and isinstance(f.name, str):
source.setSystemId(f.name)
- if source.getByteStream() is None:
+ if source.getCharacterStream() is None and source.getByteStream() is None:
sysid = source.getSystemId()
basehead = os.path.dirname(os.path.normpath(base))
sysidfilename = os.path.join(basehead, sysid)
diff --git a/Lib/xml/sax/xmlreader.py b/Lib/xml/sax/xmlreader.py
index 7ef497f94f..716f228404 100644
--- a/Lib/xml/sax/xmlreader.py
+++ b/Lib/xml/sax/xmlreader.py
@@ -117,7 +117,9 @@ class IncrementalParser(XMLReader):
source = saxutils.prepare_input_source(source)
self.prepareParser(source)
- file = source.getByteStream()
+ file = source.getCharacterStream()
+ if file is None:
+ file = source.getByteStream()
buffer = file.read(self._bufsize)
while buffer:
self.feed(buffer)