summaryrefslogtreecommitdiff
path: root/Lib/test/test_sax.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-04-02 20:55:59 +0300
committerSerhiy Storchaka <storchaka@gmail.com>2015-04-02 20:55:59 +0300
commitaa9563c1b8b8187282c0cad2b0e3c0ed7f6a8dc9 (patch)
tree7582c9bfdb3b1931d249d8eccd35a852b14ff341 /Lib/test/test_sax.py
parent2379d541dd2a4944713999f0071d53fdddee3d47 (diff)
downloadcpython-git-aa9563c1b8b8187282c0cad2b0e3c0ed7f6a8dc9.tar.gz
Issue #2175: Added tests for xml.sax.saxutils.prepare_input_source().
Made test XML files non-ASCII.
Diffstat (limited to 'Lib/test/test_sax.py')
-rw-r--r--Lib/test/test_sax.py76
1 files changed, 71 insertions, 5 deletions
diff --git a/Lib/test/test_sax.py b/Lib/test/test_sax.py
index a6238b2786..c8d5b21c85 100644
--- a/Lib/test/test_sax.py
+++ b/Lib/test/test_sax.py
@@ -10,7 +10,7 @@ except SAXReaderNotAvailable:
# don't try to test this module if we cannot create a parser
raise unittest.SkipTest("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
@@ -172,6 +172,60 @@ class SaxutilsTest(unittest.TestCase):
p = make_parser(['xml.parsers.no_such_parser'])
+class PrepareInputSourceTest(unittest.TestCase):
+
+ def setUp(self):
+ self.file = support.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 checkContent(self, stream, content):
+ self.assertIsNotNone(stream)
+ self.assertEqual(stream.read(), content)
+ stream.close()
+
+
+ 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.")
+
+
# ===== XMLGenerator
class XmlgenTest:
@@ -622,7 +676,7 @@ class ExpatReaderTest(XmlTestBase):
# ===== XMLReader support
- def test_expat_file(self):
+ def test_expat_binary_file(self):
parser = create_parser()
result = BytesIO()
xmlgen = XMLGenerator(result)
@@ -633,8 +687,19 @@ class ExpatReaderTest(XmlTestBase):
self.assertEqual(result.getvalue(), xml_test_out)
+ def test_expat_text_file(self):
+ parser = create_parser()
+ result = BytesIO()
+ 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)
@@ -644,7 +709,7 @@ class ExpatReaderTest(XmlTestBase):
xmlgen = XMLGenerator(result)
parser.setContentHandler(xmlgen)
- parser.parse(open(fname))
+ parser.parse(open(fname, 'rb'))
self.assertEqual(result.getvalue(), xml_test_out)
@@ -826,7 +891,7 @@ class ExpatReaderTest(XmlTestBase):
self.assertEqual(result.getvalue(), xml_test_out)
- def test_expat_inpsource_stream(self):
+ def test_expat_inpsource_byte_stream(self):
parser = create_parser()
result = BytesIO()
xmlgen = XMLGenerator(result)
@@ -1018,6 +1083,7 @@ class XmlReaderTest(XmlTestBase):
def test_main():
run_unittest(MakeParserTest,
SaxutilsTest,
+ PrepareInputSourceTest,
StringXmlgenTest,
BytesXmlgenTest,
WriterXmlgenTest,