diff -r c4e6e560e6f5 Lib/test/test_sax.py
--- a/Lib/test/test_sax.py Mon Feb 04 18:29:47 2013 +0200
+++ b/Lib/test/test_sax.py Mon Feb 04 21:34:11 2013 +0200
@@ -9,15 +9,15 @@
# don't try to test this module if we cannot create a parser
raise ImportError("no XML parsers available")
from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
- XMLFilterBase
+ XMLFilterBase, prepare_input_source
from xml.sax.expatreader import create_parser
from xml.sax.handler import feature_namespaces
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
-from io import StringIO
+from io import BytesIO, StringIO
import os.path
import shutil
from test import support
-from test.support import findfile, run_unittest
+from test.support import findfile, run_unittest, TESTFN
import unittest
TEST_XMLFILE = findfile("test.xml", subdir="xmltestdata")
@@ -171,6 +171,81 @@
p = make_parser(['xml.parsers.no_such_parser'])
+class PrepareInputSourceTest(unittest.TestCase):
+
+ # Fixture methods
+ def setUp(self):
+ self.file = TESTFN
+ with open(self.file, "w") as tmp:
+ tmp.write("This was read from a file.")
+
+ def tearDown(self):
+ support.unlink(self.file)
+
+ def make_byte_stream(self):
+ return BytesIO(b"This is a byte stream.")
+
+ def make_character_stream(self):
+ return StringIO("This is a character stream.")
+
+ def checkContent(self, stream, content):
+ self.assertIsNotNone(stream)
+ self.assertEqual(stream.read(), content)
+ stream.close()
+
+ # The tests
+ def test_character_stream(self):
+ '''If the source is an InputSource with a character stream, use it.'''
+ src = InputSource(self.file)
+ src.setCharacterStream(self.make_character_stream())
+ prep = prepare_input_source(src)
+ self.assertIsNone(prep.getByteStream())
+ self.checkContent(prep.getCharacterStream(),
+ "This is a character stream.")
+
+ def test_byte_stream(self):
+ '''If the source is an InputSource that does not have a character
+ stream but does have a byte stream, use the byte stream.'''
+ src = InputSource(self.file)
+ src.setByteStream(self.make_byte_stream())
+ prep = prepare_input_source(src)
+ self.assertIsNone(prep.getCharacterStream())
+ self.checkContent(prep.getByteStream(),
+ b"This is a byte stream.")
+
+ def test_system_id(self):
+ '''If the source is an InputSource that has neither a character
+ stream nor a byte stream, open the system ID.'''
+ src = InputSource(self.file)
+ prep = prepare_input_source(src)
+ self.assertIsNone(prep.getCharacterStream())
+ self.checkContent(prep.getByteStream(),
+ b"This was read from a file.")
+
+ def test_string(self):
+ '''If the source is a string, use it as a system ID and open it.'''
+ prep = prepare_input_source(self.file)
+ self.assertIsNone(prep.getCharacterStream())
+ self.checkContent(prep.getByteStream(),
+ b"This was read from a file.")
+
+ def test_binary_file(self):
+ '''If the source is a binary file-like object, use it as a byte
+ stream.'''
+ prep = prepare_input_source(self.make_byte_stream())
+ self.assertIsNone(prep.getCharacterStream())
+ self.checkContent(prep.getByteStream(),
+ b"This is a byte stream.")
+
+ def test_text_file(self):
+ '''If the source is a text file-like object, use it as a character
+ stream.'''
+ prep = prepare_input_source(self.make_character_stream())
+ self.assertIsNone(prep.getByteStream())
+ self.checkContent(prep.getCharacterStream(),
+ "This is a character stream.")
+
+
# ===== XMLGenerator
start = '\n'
@@ -478,26 +553,37 @@
#
# ===========================================================================
-with open(TEST_XMLFILE_OUT) as f:
+with open(TEST_XMLFILE_OUT, encoding='iso-8859-1') as f:
xml_test_out = f.read()
class ExpatReaderTest(XmlTestBase):
# ===== XMLReader support
- def test_expat_file(self):
+ def test_expat_binary_file(self):
parser = create_parser()
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
- with open(TEST_XMLFILE) as f:
+ with open(TEST_XMLFILE, 'rb') as f:
+ parser.parse(f)
+
+ self.assertEqual(result.getvalue(), xml_test_out)
+
+ def test_expat_text_file(self):
+ parser = create_parser()
+ result = StringIO()
+ xmlgen = XMLGenerator(result)
+
+ parser.setContentHandler(xmlgen)
+ with open(TEST_XMLFILE, 'rt', encoding='iso-8859-1') as f:
parser.parse(f)
self.assertEqual(result.getvalue(), xml_test_out)
@requires_nonascii_filenames
- def test_expat_file_nonascii(self):
+ def test_expat_binary_file_nonascii(self):
fname = support.TESTFN_UNICODE
shutil.copyfile(TEST_XMLFILE, fname)
self.addCleanup(support.unlink, fname)
@@ -507,7 +593,7 @@
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
- parser.parse(open(fname))
+ parser.parse(open(fname, 'rb'))
self.assertEqual(result.getvalue(), xml_test_out)
@@ -547,7 +633,7 @@
def resolveEntity(self, publicId, systemId):
inpsrc = InputSource()
- inpsrc.setByteStream(StringIO(""))
+ inpsrc.setByteStream(BytesIO(b""))
return inpsrc
def test_expat_entityresolver(self):
@@ -665,19 +751,32 @@
self.assertEqual(result.getvalue(), xml_test_out)
- def test_expat_inpsource_stream(self):
+ def test_expat_inpsource_byte_stream(self):
parser = create_parser()
result = StringIO()
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
inpsrc = InputSource()
- with open(TEST_XMLFILE) as f:
+ with open(TEST_XMLFILE, 'rb') as f:
inpsrc.setByteStream(f)
parser.parse(inpsrc)
self.assertEqual(result.getvalue(), xml_test_out)
+ def test_expat_inpsource_character_stream(self):
+ parser = create_parser()
+ result = StringIO()
+ xmlgen = XMLGenerator(result)
+
+ parser.setContentHandler(xmlgen)
+ inpsrc = InputSource()
+ with open(TEST_XMLFILE, 'rt', encoding='iso-8859-1') as f:
+ inpsrc.setCharacterStream(f)
+ parser.parse(inpsrc)
+
+ self.assertEqual(result.getvalue(), xml_test_out)
+
# ===== IncrementalParser support
def test_expat_incremental(self):
@@ -766,7 +865,7 @@
parser = create_parser()
parser.setContentHandler(ContentHandler()) # do nothing
source = InputSource()
- source.setByteStream(StringIO("")) #ill-formed
+ source.setByteStream(BytesIO(b"")) #ill-formed
name = "a file name"
source.setSystemId(name)
try:
@@ -857,6 +956,7 @@
def test_main():
run_unittest(MakeParserTest,
SaxutilsTest,
+ PrepareInputSourceTest,
XmlgenTest,
ExpatReaderTest,
ErrorReportingTest,
diff -r c4e6e560e6f5 Lib/test/xmltestdata/test.xml
--- a/Lib/test/xmltestdata/test.xml Mon Feb 04 18:29:47 2013 +0200
+++ b/Lib/test/xmltestdata/test.xml Mon Feb 04 21:34:11 2013 +0200
@@ -1,4 +1,4 @@
-
+
Introduction to XSL
Introduction to XSL
@@ -110,6 +110,6 @@
-
+µ
diff -r c4e6e560e6f5 Lib/test/xmltestdata/test.xml.out
--- a/Lib/test/xmltestdata/test.xml.out Mon Feb 04 18:29:47 2013 +0200
+++ b/Lib/test/xmltestdata/test.xml.out Mon Feb 04 21:34:11 2013 +0200
@@ -110,6 +110,6 @@
-
+µ