Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa9563c

Browse files
Issue #2175: Added tests for xml.sax.saxutils.prepare_input_source().
Made test XML files non-ASCII.
1 parent 2379d54 commit aa9563c

4 files changed

Lines changed: 82 additions & 10 deletions

File tree

Lib/test/test_minidom.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ def checkWholeText(self, node, s):
4949
t = node.wholeText
5050
self.confirm(t == s, "looking for %r, found %r" % (s, t))
5151

52-
def testParseFromFile(self):
53-
with open(tstfile) as file:
52+
def testParseFromBinaryFile(self):
53+
with open(tstfile, 'rb') as file:
54+
dom = parse(file)
55+
dom.unlink()
56+
self.confirm(isinstance(dom, Document))
57+
58+
def testParseFromTextFile(self):
59+
with open(tstfile, 'r', encoding='iso-8859-1') as file:
5460
dom = parse(file)
5561
dom.unlink()
5662
self.confirm(isinstance(dom, Document))

Lib/test/test_sax.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# don't try to test this module if we cannot create a parser
1111
raise unittest.SkipTest("no XML parsers available")
1212
from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
13-
XMLFilterBase
13+
XMLFilterBase, prepare_input_source
1414
from xml.sax.expatreader import create_parser
1515
from xml.sax.handler import feature_namespaces
1616
from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
@@ -172,6 +172,60 @@ def test_make_parser(self):
172172
p = make_parser(['xml.parsers.no_such_parser'])
173173

174174

175+
class PrepareInputSourceTest(unittest.TestCase):
176+
177+
def setUp(self):
178+
self.file = support.TESTFN
179+
with open(self.file, "w") as tmp:
180+
tmp.write("This was read from a file.")
181+
182+
def tearDown(self):
183+
support.unlink(self.file)
184+
185+
def make_byte_stream(self):
186+
return BytesIO(b"This is a byte stream.")
187+
188+
def checkContent(self, stream, content):
189+
self.assertIsNotNone(stream)
190+
self.assertEqual(stream.read(), content)
191+
stream.close()
192+
193+
194+
def test_byte_stream(self):
195+
# If the source is an InputSource that does not have a character
196+
# stream but does have a byte stream, use the byte stream.
197+
src = InputSource(self.file)
198+
src.setByteStream(self.make_byte_stream())
199+
prep = prepare_input_source(src)
200+
self.assertIsNone(prep.getCharacterStream())
201+
self.checkContent(prep.getByteStream(),
202+
b"This is a byte stream.")
203+
204+
def test_system_id(self):
205+
# If the source is an InputSource that has neither a character
206+
# stream nor a byte stream, open the system ID.
207+
src = InputSource(self.file)
208+
prep = prepare_input_source(src)
209+
self.assertIsNone(prep.getCharacterStream())
210+
self.checkContent(prep.getByteStream(),
211+
b"This was read from a file.")
212+
213+
def test_string(self):
214+
# If the source is a string, use it as a system ID and open it.
215+
prep = prepare_input_source(self.file)
216+
self.assertIsNone(prep.getCharacterStream())
217+
self.checkContent(prep.getByteStream(),
218+
b"This was read from a file.")
219+
220+
def test_binary_file(self):
221+
# If the source is a binary file-like object, use it as a byte
222+
# stream.
223+
prep = prepare_input_source(self.make_byte_stream())
224+
self.assertIsNone(prep.getCharacterStream())
225+
self.checkContent(prep.getByteStream(),
226+
b"This is a byte stream.")
227+
228+
175229
# ===== XMLGenerator
176230

177231
class XmlgenTest:
@@ -622,7 +676,7 @@ class ExpatReaderTest(XmlTestBase):
622676

623677
# ===== XMLReader support
624678

625-
def test_expat_file(self):
679+
def test_expat_binary_file(self):
626680
parser = create_parser()
627681
result = BytesIO()
628682
xmlgen = XMLGenerator(result)
@@ -633,8 +687,19 @@ def test_expat_file(self):
633687

634688
self.assertEqual(result.getvalue(), xml_test_out)
635689

690+
def test_expat_text_file(self):
691+
parser = create_parser()
692+
result = BytesIO()
693+
xmlgen = XMLGenerator(result)
694+
695+
parser.setContentHandler(xmlgen)
696+
with open(TEST_XMLFILE, 'rt', encoding='iso-8859-1') as f:
697+
parser.parse(f)
698+
699+
self.assertEqual(result.getvalue(), xml_test_out)
700+
636701
@requires_nonascii_filenames
637-
def test_expat_file_nonascii(self):
702+
def test_expat_binary_file_nonascii(self):
638703
fname = support.TESTFN_UNICODE
639704
shutil.copyfile(TEST_XMLFILE, fname)
640705
self.addCleanup(support.unlink, fname)
@@ -644,7 +709,7 @@ def test_expat_file_nonascii(self):
644709
xmlgen = XMLGenerator(result)
645710

646711
parser.setContentHandler(xmlgen)
647-
parser.parse(open(fname))
712+
parser.parse(open(fname, 'rb'))
648713

649714
self.assertEqual(result.getvalue(), xml_test_out)
650715

@@ -826,7 +891,7 @@ def test_expat_inpsource_sysid_nonascii(self):
826891

827892
self.assertEqual(result.getvalue(), xml_test_out)
828893

829-
def test_expat_inpsource_stream(self):
894+
def test_expat_inpsource_byte_stream(self):
830895
parser = create_parser()
831896
result = BytesIO()
832897
xmlgen = XMLGenerator(result)
@@ -1018,6 +1083,7 @@ def test_nsattrs_wattr(self):
10181083
def test_main():
10191084
run_unittest(MakeParserTest,
10201085
SaxutilsTest,
1086+
PrepareInputSourceTest,
10211087
StringXmlgenTest,
10221088
BytesXmlgenTest,
10231089
WriterXmlgenTest,

Lib/test/xmltestdata/test.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0"?>
1+
<?xml version="1.0" encoding="iso-8859-1"?>
22
<HTML xmlns:pp="http://www.isogen.com/paul/post-processor">
33
<TITLE>Introduction to XSL</TITLE>
44
<H1>Introduction to XSL</H1>
@@ -110,6 +110,6 @@
110110
</UL>
111111

112112

113-
113+
µ
114114

115115
</HTML>

Lib/test/xmltestdata/test.xml.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@
110110
</UL>
111111

112112

113-
113+
µ
114114

115115
</HTML>

0 commit comments

Comments
 (0)