blob: c4b211f6bf9a76ca123e52a1c833827bcb83cb69 [file] [log] [blame]
Martin v. Löwisa729daf2002-08-04 17:28:33 +00001# regression test for SAX 2.0 -*- coding: iso-8859-1 -*-
Lars Gustäbel96753b32000-09-24 12:24:24 +00002# $Id$
3
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004from xml.sax import make_parser, ContentHandler, \
5 SAXException, SAXReaderNotAvailable, SAXParseException
Martin v. Löwis962c9e72000-10-06 17:41:52 +00006try:
7 make_parser()
Martin v. Löwis80670bc2000-10-06 21:13:23 +00008except SAXReaderNotAvailable:
Martin v. Löwis962c9e72000-10-06 17:41:52 +00009 # don't try to test this module if we cannot create a parser
10 raise ImportError("no XML parsers available")
Thomas Wouters0e3f5912006-08-11 14:57:12 +000011from xml.sax.saxutils import XMLGenerator, escape, unescape, quoteattr, \
12 XMLFilterBase
13from xml.sax.expatreader import create_parser
Antoine Pitrou6b03ee62010-10-27 18:33:30 +000014from xml.sax.handler import feature_namespaces
Thomas Wouters0e3f5912006-08-11 14:57:12 +000015from xml.sax.xmlreader import InputSource, AttributesImpl, AttributesNSImpl
Guido van Rossum34d19282007-08-09 01:03:29 +000016from io import StringIO
Benjamin Petersonee8712c2008-05-20 21:35:26 +000017from test.support import findfile, run_unittest
Guido van Rossumd8faa362007-04-27 19:54:29 +000018import unittest
Florent Xiclunaf15351d2010-03-13 23:24:31 +000019
20TEST_XMLFILE = findfile("test.xml", subdir="xmltestdata")
21TEST_XMLFILE_OUT = findfile("test.xml.out", subdir="xmltestdata")
Victor Stinner6c6f8512010-08-07 10:09:35 +000022try:
23 TEST_XMLFILE.encode("utf8")
24 TEST_XMLFILE_OUT.encode("utf8")
25except UnicodeEncodeError:
26 raise unittest.SkipTest("filename is not encodable to utf8")
Lars Gustäbel96753b32000-09-24 12:24:24 +000027
Guido van Rossumd8faa362007-04-27 19:54:29 +000028ns_uri = "http://www.python.org/xml-ns/saxtest/"
Lars Gustäbel96753b32000-09-24 12:24:24 +000029
Guido van Rossumd8faa362007-04-27 19:54:29 +000030class XmlTestBase(unittest.TestCase):
31 def verify_empty_attrs(self, attrs):
32 self.assertRaises(KeyError, attrs.getValue, "attr")
33 self.assertRaises(KeyError, attrs.getValueByQName, "attr")
34 self.assertRaises(KeyError, attrs.getNameByQName, "attr")
35 self.assertRaises(KeyError, attrs.getQNameByName, "attr")
36 self.assertRaises(KeyError, attrs.__getitem__, "attr")
37 self.assertEquals(attrs.getLength(), 0)
38 self.assertEquals(attrs.getNames(), [])
39 self.assertEquals(attrs.getQNames(), [])
40 self.assertEquals(len(attrs), 0)
Ezio Melottib58e0bd2010-01-23 15:40:09 +000041 self.assertNotIn("attr", attrs)
Guido van Rossumd8faa362007-04-27 19:54:29 +000042 self.assertEquals(list(attrs.keys()), [])
43 self.assertEquals(attrs.get("attrs"), None)
44 self.assertEquals(attrs.get("attrs", 25), 25)
45 self.assertEquals(list(attrs.items()), [])
46 self.assertEquals(list(attrs.values()), [])
Lars Gustäbel96753b32000-09-24 12:24:24 +000047
Guido van Rossumd8faa362007-04-27 19:54:29 +000048 def verify_empty_nsattrs(self, attrs):
49 self.assertRaises(KeyError, attrs.getValue, (ns_uri, "attr"))
50 self.assertRaises(KeyError, attrs.getValueByQName, "ns:attr")
51 self.assertRaises(KeyError, attrs.getNameByQName, "ns:attr")
52 self.assertRaises(KeyError, attrs.getQNameByName, (ns_uri, "attr"))
53 self.assertRaises(KeyError, attrs.__getitem__, (ns_uri, "attr"))
54 self.assertEquals(attrs.getLength(), 0)
55 self.assertEquals(attrs.getNames(), [])
56 self.assertEquals(attrs.getQNames(), [])
57 self.assertEquals(len(attrs), 0)
Ezio Melottib58e0bd2010-01-23 15:40:09 +000058 self.assertNotIn((ns_uri, "attr"), attrs)
Guido van Rossumd8faa362007-04-27 19:54:29 +000059 self.assertEquals(list(attrs.keys()), [])
60 self.assertEquals(attrs.get((ns_uri, "attr")), None)
61 self.assertEquals(attrs.get((ns_uri, "attr"), 25), 25)
62 self.assertEquals(list(attrs.items()), [])
63 self.assertEquals(list(attrs.values()), [])
Lars Gustäbel96753b32000-09-24 12:24:24 +000064
Guido van Rossumd8faa362007-04-27 19:54:29 +000065 def verify_attrs_wattr(self, attrs):
66 self.assertEquals(attrs.getLength(), 1)
67 self.assertEquals(attrs.getNames(), ["attr"])
68 self.assertEquals(attrs.getQNames(), ["attr"])
69 self.assertEquals(len(attrs), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +000070 self.assertIn("attr", attrs)
Guido van Rossumd8faa362007-04-27 19:54:29 +000071 self.assertEquals(list(attrs.keys()), ["attr"])
72 self.assertEquals(attrs.get("attr"), "val")
73 self.assertEquals(attrs.get("attr", 25), "val")
74 self.assertEquals(list(attrs.items()), [("attr", "val")])
75 self.assertEquals(list(attrs.values()), ["val"])
76 self.assertEquals(attrs.getValue("attr"), "val")
77 self.assertEquals(attrs.getValueByQName("attr"), "val")
78 self.assertEquals(attrs.getNameByQName("attr"), "attr")
79 self.assertEquals(attrs["attr"], "val")
80 self.assertEquals(attrs.getQNameByName("attr"), "attr")
Lars Gustäbel96753b32000-09-24 12:24:24 +000081
Guido van Rossumd8faa362007-04-27 19:54:29 +000082class MakeParserTest(unittest.TestCase):
83 def test_make_parser2(self):
Lars Gustäbel2fc52942000-10-24 15:35:07 +000084 # Creating parsers several times in a row should succeed.
85 # Testing this because there have been failures of this kind
86 # before.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000087 from xml.sax import make_parser
Lars Gustäbel2fc52942000-10-24 15:35:07 +000088 p = make_parser()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000089 from xml.sax import make_parser
Lars Gustäbel2fc52942000-10-24 15:35:07 +000090 p = make_parser()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000091 from xml.sax import make_parser
Lars Gustäbel2fc52942000-10-24 15:35:07 +000092 p = make_parser()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000093 from xml.sax import make_parser
Lars Gustäbel2fc52942000-10-24 15:35:07 +000094 p = make_parser()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000095 from xml.sax import make_parser
Lars Gustäbel2fc52942000-10-24 15:35:07 +000096 p = make_parser()
Thomas Wouters0e3f5912006-08-11 14:57:12 +000097 from xml.sax import make_parser
Lars Gustäbel2fc52942000-10-24 15:35:07 +000098 p = make_parser()
Tim Petersd2bf3b72001-01-18 02:22:22 +000099
100
Lars Gustäbel96753b32000-09-24 12:24:24 +0000101# ===========================================================================
102#
103# saxutils tests
104#
105# ===========================================================================
106
Guido van Rossumd8faa362007-04-27 19:54:29 +0000107class SaxutilsTest(unittest.TestCase):
108 # ===== escape
109 def test_escape_basic(self):
110 self.assertEquals(escape("Donald Duck & Co"), "Donald Duck & Co")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000111
Guido van Rossumd8faa362007-04-27 19:54:29 +0000112 def test_escape_all(self):
113 self.assertEquals(escape("<Donald Duck & Co>"),
114 "&lt;Donald Duck &amp; Co&gt;")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000115
Guido van Rossumd8faa362007-04-27 19:54:29 +0000116 def test_escape_extra(self):
117 self.assertEquals(escape("Hei på deg", {"å" : "&aring;"}),
118 "Hei p&aring; deg")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000119
Guido van Rossumd8faa362007-04-27 19:54:29 +0000120 # ===== unescape
121 def test_unescape_basic(self):
122 self.assertEquals(unescape("Donald Duck &amp; Co"), "Donald Duck & Co")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000123
Guido van Rossumd8faa362007-04-27 19:54:29 +0000124 def test_unescape_all(self):
125 self.assertEquals(unescape("&lt;Donald Duck &amp; Co&gt;"),
126 "<Donald Duck & Co>")
Martin v. Löwis74b51ac2002-10-26 14:50:45 +0000127
Guido van Rossumd8faa362007-04-27 19:54:29 +0000128 def test_unescape_extra(self):
129 self.assertEquals(unescape("Hei på deg", {"å" : "&aring;"}),
130 "Hei p&aring; deg")
Martin v. Löwis74b51ac2002-10-26 14:50:45 +0000131
Guido van Rossumd8faa362007-04-27 19:54:29 +0000132 def test_unescape_amp_extra(self):
133 self.assertEquals(unescape("&amp;foo;", {"&foo;": "splat"}), "&foo;")
Martin v. Löwis74b51ac2002-10-26 14:50:45 +0000134
Guido van Rossumd8faa362007-04-27 19:54:29 +0000135 # ===== quoteattr
136 def test_quoteattr_basic(self):
137 self.assertEquals(quoteattr("Donald Duck & Co"),
138 '"Donald Duck &amp; Co"')
Martin v. Löwis74b51ac2002-10-26 14:50:45 +0000139
Guido van Rossumd8faa362007-04-27 19:54:29 +0000140 def test_single_quoteattr(self):
141 self.assertEquals(quoteattr('Includes "double" quotes'),
142 '\'Includes "double" quotes\'')
Fred Drake32f3add2002-10-28 17:58:48 +0000143
Guido van Rossumd8faa362007-04-27 19:54:29 +0000144 def test_double_quoteattr(self):
145 self.assertEquals(quoteattr("Includes 'single' quotes"),
146 "\"Includes 'single' quotes\"")
Fred Drakeacd32d32001-07-19 16:10:15 +0000147
Guido van Rossumd8faa362007-04-27 19:54:29 +0000148 def test_single_double_quoteattr(self):
149 self.assertEquals(quoteattr("Includes 'single' and \"double\" quotes"),
150 "\"Includes 'single' and &quot;double&quot; quotes\"")
Fred Drakeacd32d32001-07-19 16:10:15 +0000151
Guido van Rossumd8faa362007-04-27 19:54:29 +0000152 # ===== make_parser
153 def test_make_parser(self):
Martin v. Löwis962c9e72000-10-06 17:41:52 +0000154 # Creating a parser should succeed - it should fall back
155 # to the expatreader
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000156 p = make_parser(['xml.parsers.no_such_parser'])
Martin v. Löwis962c9e72000-10-06 17:41:52 +0000157
158
Lars Gustäbel96753b32000-09-24 12:24:24 +0000159# ===== XMLGenerator
160
161start = '<?xml version="1.0" encoding="iso-8859-1"?>\n'
162
Guido van Rossumd8faa362007-04-27 19:54:29 +0000163class XmlgenTest(unittest.TestCase):
164 def test_xmlgen_basic(self):
165 result = StringIO()
166 gen = XMLGenerator(result)
167 gen.startDocument()
168 gen.startElement("doc", {})
169 gen.endElement("doc")
170 gen.endDocument()
Lars Gustäbel96753b32000-09-24 12:24:24 +0000171
Guido van Rossumd8faa362007-04-27 19:54:29 +0000172 self.assertEquals(result.getvalue(), start + "<doc></doc>")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000173
R. David Murraya90032a2010-10-17 22:46:45 +0000174 def test_xmlgen_basic_empty(self):
175 result = StringIO()
176 gen = XMLGenerator(result, short_empty_elements=True)
177 gen.startDocument()
178 gen.startElement("doc", {})
179 gen.endElement("doc")
180 gen.endDocument()
181
182 self.assertEquals(result.getvalue(), start + "<doc/>")
183
Guido van Rossumd8faa362007-04-27 19:54:29 +0000184 def test_xmlgen_content(self):
185 result = StringIO()
186 gen = XMLGenerator(result)
Fred Drake004d5e62000-10-23 17:22:08 +0000187
Guido van Rossumd8faa362007-04-27 19:54:29 +0000188 gen.startDocument()
189 gen.startElement("doc", {})
190 gen.characters("huhei")
191 gen.endElement("doc")
192 gen.endDocument()
Lars Gustäbel96753b32000-09-24 12:24:24 +0000193
Guido van Rossumd8faa362007-04-27 19:54:29 +0000194 self.assertEquals(result.getvalue(), start + "<doc>huhei</doc>")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000195
R. David Murraya90032a2010-10-17 22:46:45 +0000196 def test_xmlgen_content_empty(self):
197 result = StringIO()
198 gen = XMLGenerator(result, short_empty_elements=True)
199
200 gen.startDocument()
201 gen.startElement("doc", {})
202 gen.characters("huhei")
203 gen.endElement("doc")
204 gen.endDocument()
205
206 self.assertEquals(result.getvalue(), start + "<doc>huhei</doc>")
207
Guido van Rossumd8faa362007-04-27 19:54:29 +0000208 def test_xmlgen_pi(self):
209 result = StringIO()
210 gen = XMLGenerator(result)
Fred Drake004d5e62000-10-23 17:22:08 +0000211
Guido van Rossumd8faa362007-04-27 19:54:29 +0000212 gen.startDocument()
213 gen.processingInstruction("test", "data")
214 gen.startElement("doc", {})
215 gen.endElement("doc")
216 gen.endDocument()
Lars Gustäbel96753b32000-09-24 12:24:24 +0000217
Guido van Rossumd8faa362007-04-27 19:54:29 +0000218 self.assertEquals(result.getvalue(), start + "<?test data?><doc></doc>")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000219
Guido van Rossumd8faa362007-04-27 19:54:29 +0000220 def test_xmlgen_content_escape(self):
221 result = StringIO()
222 gen = XMLGenerator(result)
Fred Drake004d5e62000-10-23 17:22:08 +0000223
Guido van Rossumd8faa362007-04-27 19:54:29 +0000224 gen.startDocument()
225 gen.startElement("doc", {})
226 gen.characters("<huhei&")
227 gen.endElement("doc")
228 gen.endDocument()
Lars Gustäbel96753b32000-09-24 12:24:24 +0000229
Guido van Rossumd8faa362007-04-27 19:54:29 +0000230 self.assertEquals(result.getvalue(),
231 start + "<doc>&lt;huhei&amp;</doc>")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000232
Guido van Rossumd8faa362007-04-27 19:54:29 +0000233 def test_xmlgen_attr_escape(self):
234 result = StringIO()
235 gen = XMLGenerator(result)
Fred Drakec9fadf92001-08-07 19:17:06 +0000236
Guido van Rossumd8faa362007-04-27 19:54:29 +0000237 gen.startDocument()
238 gen.startElement("doc", {"a": '"'})
239 gen.startElement("e", {"a": "'"})
240 gen.endElement("e")
241 gen.startElement("e", {"a": "'\""})
242 gen.endElement("e")
243 gen.startElement("e", {"a": "\n\r\t"})
244 gen.endElement("e")
245 gen.endElement("doc")
246 gen.endDocument()
Fred Drakec9fadf92001-08-07 19:17:06 +0000247
Guido van Rossumd8faa362007-04-27 19:54:29 +0000248 self.assertEquals(result.getvalue(), start +
249 ("<doc a='\"'><e a=\"'\"></e>"
250 "<e a=\"'&quot;\"></e>"
251 "<e a=\"&#10;&#13;&#9;\"></e></doc>"))
Fred Drakec9fadf92001-08-07 19:17:06 +0000252
Guido van Rossumd8faa362007-04-27 19:54:29 +0000253 def test_xmlgen_ignorable(self):
254 result = StringIO()
255 gen = XMLGenerator(result)
Fred Drake004d5e62000-10-23 17:22:08 +0000256
Guido van Rossumd8faa362007-04-27 19:54:29 +0000257 gen.startDocument()
258 gen.startElement("doc", {})
259 gen.ignorableWhitespace(" ")
260 gen.endElement("doc")
261 gen.endDocument()
Lars Gustäbel96753b32000-09-24 12:24:24 +0000262
Guido van Rossumd8faa362007-04-27 19:54:29 +0000263 self.assertEquals(result.getvalue(), start + "<doc> </doc>")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000264
R. David Murraya90032a2010-10-17 22:46:45 +0000265 def test_xmlgen_ignorable_empty(self):
266 result = StringIO()
267 gen = XMLGenerator(result, short_empty_elements=True)
268
269 gen.startDocument()
270 gen.startElement("doc", {})
271 gen.ignorableWhitespace(" ")
272 gen.endElement("doc")
273 gen.endDocument()
274
275 self.assertEquals(result.getvalue(), start + "<doc> </doc>")
276
Guido van Rossumd8faa362007-04-27 19:54:29 +0000277 def test_xmlgen_ns(self):
278 result = StringIO()
279 gen = XMLGenerator(result)
Lars Gustäbel96753b32000-09-24 12:24:24 +0000280
Guido van Rossumd8faa362007-04-27 19:54:29 +0000281 gen.startDocument()
282 gen.startPrefixMapping("ns1", ns_uri)
283 gen.startElementNS((ns_uri, "doc"), "ns1:doc", {})
284 # add an unqualified name
285 gen.startElementNS((None, "udoc"), None, {})
286 gen.endElementNS((None, "udoc"), None)
287 gen.endElementNS((ns_uri, "doc"), "ns1:doc")
288 gen.endPrefixMapping("ns1")
289 gen.endDocument()
Fred Drake004d5e62000-10-23 17:22:08 +0000290
Guido van Rossumd8faa362007-04-27 19:54:29 +0000291 self.assertEquals(result.getvalue(), start + \
Martin v. Löwiscf0a1cc2000-10-03 22:35:29 +0000292 ('<ns1:doc xmlns:ns1="%s"><udoc></udoc></ns1:doc>' %
Guido van Rossumd8faa362007-04-27 19:54:29 +0000293 ns_uri))
Lars Gustäbel96753b32000-09-24 12:24:24 +0000294
R. David Murraya90032a2010-10-17 22:46:45 +0000295 def test_xmlgen_ns_empty(self):
296 result = StringIO()
297 gen = XMLGenerator(result, short_empty_elements=True)
298
299 gen.startDocument()
300 gen.startPrefixMapping("ns1", ns_uri)
301 gen.startElementNS((ns_uri, "doc"), "ns1:doc", {})
302 # add an unqualified name
303 gen.startElementNS((None, "udoc"), None, {})
304 gen.endElementNS((None, "udoc"), None)
305 gen.endElementNS((ns_uri, "doc"), "ns1:doc")
306 gen.endPrefixMapping("ns1")
307 gen.endDocument()
308
309 self.assertEquals(result.getvalue(), start + \
310 ('<ns1:doc xmlns:ns1="%s"><udoc/></ns1:doc>' %
311 ns_uri))
312
Guido van Rossumd8faa362007-04-27 19:54:29 +0000313 def test_1463026_1(self):
314 result = StringIO()
315 gen = XMLGenerator(result)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000316
Guido van Rossumd8faa362007-04-27 19:54:29 +0000317 gen.startDocument()
318 gen.startElementNS((None, 'a'), 'a', {(None, 'b'):'c'})
319 gen.endElementNS((None, 'a'), 'a')
320 gen.endDocument()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000321
Guido van Rossumd8faa362007-04-27 19:54:29 +0000322 self.assertEquals(result.getvalue(), start+'<a b="c"></a>')
Thomas Wouterscf297e42007-02-23 15:07:44 +0000323
R. David Murraya90032a2010-10-17 22:46:45 +0000324 def test_1463026_1_empty(self):
325 result = StringIO()
326 gen = XMLGenerator(result, short_empty_elements=True)
327
328 gen.startDocument()
329 gen.startElementNS((None, 'a'), 'a', {(None, 'b'):'c'})
330 gen.endElementNS((None, 'a'), 'a')
331 gen.endDocument()
332
333 self.assertEquals(result.getvalue(), start+'<a b="c"/>')
334
Guido van Rossumd8faa362007-04-27 19:54:29 +0000335 def test_1463026_2(self):
336 result = StringIO()
337 gen = XMLGenerator(result)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000338
Guido van Rossumd8faa362007-04-27 19:54:29 +0000339 gen.startDocument()
340 gen.startPrefixMapping(None, 'qux')
341 gen.startElementNS(('qux', 'a'), 'a', {})
342 gen.endElementNS(('qux', 'a'), 'a')
343 gen.endPrefixMapping(None)
344 gen.endDocument()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000345
Guido van Rossumd8faa362007-04-27 19:54:29 +0000346 self.assertEquals(result.getvalue(), start+'<a xmlns="qux"></a>')
Thomas Wouterscf297e42007-02-23 15:07:44 +0000347
R. David Murraya90032a2010-10-17 22:46:45 +0000348 def test_1463026_2_empty(self):
349 result = StringIO()
350 gen = XMLGenerator(result, short_empty_elements=True)
351
352 gen.startDocument()
353 gen.startPrefixMapping(None, 'qux')
354 gen.startElementNS(('qux', 'a'), 'a', {})
355 gen.endElementNS(('qux', 'a'), 'a')
356 gen.endPrefixMapping(None)
357 gen.endDocument()
358
359 self.assertEquals(result.getvalue(), start+'<a xmlns="qux"/>')
360
Guido van Rossumd8faa362007-04-27 19:54:29 +0000361 def test_1463026_3(self):
362 result = StringIO()
363 gen = XMLGenerator(result)
Thomas Wouterscf297e42007-02-23 15:07:44 +0000364
Guido van Rossumd8faa362007-04-27 19:54:29 +0000365 gen.startDocument()
366 gen.startPrefixMapping('my', 'qux')
367 gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'})
368 gen.endElementNS(('qux', 'a'), 'a')
369 gen.endPrefixMapping('my')
370 gen.endDocument()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000371
Guido van Rossumd8faa362007-04-27 19:54:29 +0000372 self.assertEquals(result.getvalue(),
373 start+'<my:a xmlns:my="qux" b="c"></my:a>')
Lars Gustäbel96753b32000-09-24 12:24:24 +0000374
R. David Murraya90032a2010-10-17 22:46:45 +0000375 def test_1463026_3_empty(self):
376 result = StringIO()
377 gen = XMLGenerator(result, short_empty_elements=True)
378
379 gen.startDocument()
380 gen.startPrefixMapping('my', 'qux')
381 gen.startElementNS(('qux', 'a'), 'a', {(None, 'b'):'c'})
382 gen.endElementNS(('qux', 'a'), 'a')
383 gen.endPrefixMapping('my')
384 gen.endDocument()
385
386 self.assertEquals(result.getvalue(),
387 start+'<my:a xmlns:my="qux" b="c"/>')
388
Antoine Pitrou6b03ee62010-10-27 18:33:30 +0000389 def test_5027_1(self):
390 # The xml prefix (as in xml:lang below) is reserved and bound by
391 # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had
392 # a bug whereby a KeyError is thrown because this namespace is missing
393 # from a dictionary.
394 #
395 # This test demonstrates the bug by parsing a document.
396 test_xml = StringIO(
397 '<?xml version="1.0"?>'
398 '<a:g1 xmlns:a="http://example.com/ns">'
399 '<a:g2 xml:lang="en">Hello</a:g2>'
400 '</a:g1>')
401
402 parser = make_parser()
403 parser.setFeature(feature_namespaces, True)
404 result = StringIO()
405 gen = XMLGenerator(result)
406 parser.setContentHandler(gen)
407 parser.parse(test_xml)
408
409 self.assertEquals(result.getvalue(),
410 start + (
411 '<a:g1 xmlns:a="http://example.com/ns">'
412 '<a:g2 xml:lang="en">Hello</a:g2>'
413 '</a:g1>'))
414
415 def test_5027_2(self):
416 # The xml prefix (as in xml:lang below) is reserved and bound by
417 # definition to http://www.w3.org/XML/1998/namespace. XMLGenerator had
418 # a bug whereby a KeyError is thrown because this namespace is missing
419 # from a dictionary.
420 #
421 # This test demonstrates the bug by direct manipulation of the
422 # XMLGenerator.
423 result = StringIO()
424 gen = XMLGenerator(result)
425
426 gen.startDocument()
427 gen.startPrefixMapping('a', 'http://example.com/ns')
428 gen.startElementNS(('http://example.com/ns', 'g1'), 'g1', {})
429 lang_attr = {('http://www.w3.org/XML/1998/namespace', 'lang'): 'en'}
430 gen.startElementNS(('http://example.com/ns', 'g2'), 'g2', lang_attr)
431 gen.characters('Hello')
432 gen.endElementNS(('http://example.com/ns', 'g2'), 'g2')
433 gen.endElementNS(('http://example.com/ns', 'g1'), 'g1')
434 gen.endPrefixMapping('a')
435 gen.endDocument()
436
437 self.assertEquals(result.getvalue(),
438 start + (
439 '<a:g1 xmlns:a="http://example.com/ns">'
440 '<a:g2 xml:lang="en">Hello</a:g2>'
441 '</a:g1>'))
442
Fred Drake004d5e62000-10-23 17:22:08 +0000443
Guido van Rossumd8faa362007-04-27 19:54:29 +0000444class XMLFilterBaseTest(unittest.TestCase):
445 def test_filter_basic(self):
446 result = StringIO()
447 gen = XMLGenerator(result)
448 filter = XMLFilterBase()
449 filter.setContentHandler(gen)
Lars Gustäbel96753b32000-09-24 12:24:24 +0000450
Guido van Rossumd8faa362007-04-27 19:54:29 +0000451 filter.startDocument()
452 filter.startElement("doc", {})
453 filter.characters("content")
454 filter.ignorableWhitespace(" ")
455 filter.endElement("doc")
456 filter.endDocument()
457
458 self.assertEquals(result.getvalue(), start + "<doc>content </doc>")
Lars Gustäbel96753b32000-09-24 12:24:24 +0000459
460# ===========================================================================
461#
462# expatreader tests
463#
464# ===========================================================================
465
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000466xml_test_out = open(TEST_XMLFILE_OUT).read()
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000467
Guido van Rossumd8faa362007-04-27 19:54:29 +0000468class ExpatReaderTest(XmlTestBase):
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000469
Guido van Rossumd8faa362007-04-27 19:54:29 +0000470 # ===== XMLReader support
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000471
Guido van Rossumd8faa362007-04-27 19:54:29 +0000472 def test_expat_file(self):
473 parser = create_parser()
474 result = StringIO()
475 xmlgen = XMLGenerator(result)
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000476
Guido van Rossumd8faa362007-04-27 19:54:29 +0000477 parser.setContentHandler(xmlgen)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000478 parser.parse(open(TEST_XMLFILE))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000479
Guido van Rossumd8faa362007-04-27 19:54:29 +0000480 self.assertEquals(result.getvalue(), xml_test_out)
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000481
Guido van Rossumd8faa362007-04-27 19:54:29 +0000482 # ===== DTDHandler support
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000483
Guido van Rossumd8faa362007-04-27 19:54:29 +0000484 class TestDTDHandler:
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000485
Guido van Rossumd8faa362007-04-27 19:54:29 +0000486 def __init__(self):
487 self._notations = []
488 self._entities = []
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000489
Guido van Rossumd8faa362007-04-27 19:54:29 +0000490 def notationDecl(self, name, publicId, systemId):
491 self._notations.append((name, publicId, systemId))
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000492
Guido van Rossumd8faa362007-04-27 19:54:29 +0000493 def unparsedEntityDecl(self, name, publicId, systemId, ndata):
494 self._entities.append((name, publicId, systemId, ndata))
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000495
Guido van Rossumd8faa362007-04-27 19:54:29 +0000496 def test_expat_dtdhandler(self):
497 parser = create_parser()
498 handler = self.TestDTDHandler()
499 parser.setDTDHandler(handler)
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000500
Guido van Rossumd8faa362007-04-27 19:54:29 +0000501 parser.feed('<!DOCTYPE doc [\n')
502 parser.feed(' <!ENTITY img SYSTEM "expat.gif" NDATA GIF>\n')
503 parser.feed(' <!NOTATION GIF PUBLIC "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN">\n')
504 parser.feed(']>\n')
505 parser.feed('<doc></doc>')
506 parser.close()
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000507
Guido van Rossumd8faa362007-04-27 19:54:29 +0000508 self.assertEquals(handler._notations,
509 [("GIF", "-//CompuServe//NOTATION Graphics Interchange Format 89a//EN", None)])
510 self.assertEquals(handler._entities, [("img", None, "expat.gif", "GIF")])
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000511
Guido van Rossumd8faa362007-04-27 19:54:29 +0000512 # ===== EntityResolver support
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000513
Guido van Rossumd8faa362007-04-27 19:54:29 +0000514 class TestEntityResolver:
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000515
Guido van Rossumd8faa362007-04-27 19:54:29 +0000516 def resolveEntity(self, publicId, systemId):
517 inpsrc = InputSource()
518 inpsrc.setByteStream(StringIO("<entity/>"))
519 return inpsrc
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000520
Guido van Rossumd8faa362007-04-27 19:54:29 +0000521 def test_expat_entityresolver(self):
522 parser = create_parser()
523 parser.setEntityResolver(self.TestEntityResolver())
524 result = StringIO()
525 parser.setContentHandler(XMLGenerator(result))
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000526
Guido van Rossumd8faa362007-04-27 19:54:29 +0000527 parser.feed('<!DOCTYPE doc [\n')
528 parser.feed(' <!ENTITY test SYSTEM "whatever">\n')
529 parser.feed(']>\n')
530 parser.feed('<doc>&test;</doc>')
531 parser.close()
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000532
Guido van Rossumd8faa362007-04-27 19:54:29 +0000533 self.assertEquals(result.getvalue(), start +
534 "<doc><entity></entity></doc>")
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000535
Guido van Rossumd8faa362007-04-27 19:54:29 +0000536 # ===== Attributes support
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000537
Guido van Rossumd8faa362007-04-27 19:54:29 +0000538 class AttrGatherer(ContentHandler):
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000539
Guido van Rossumd8faa362007-04-27 19:54:29 +0000540 def startElement(self, name, attrs):
541 self._attrs = attrs
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000542
Guido van Rossumd8faa362007-04-27 19:54:29 +0000543 def startElementNS(self, name, qname, attrs):
544 self._attrs = attrs
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000545
Guido van Rossumd8faa362007-04-27 19:54:29 +0000546 def test_expat_attrs_empty(self):
547 parser = create_parser()
548 gather = self.AttrGatherer()
549 parser.setContentHandler(gather)
550
551 parser.feed("<doc/>")
552 parser.close()
553
554 self.verify_empty_attrs(gather._attrs)
555
556 def test_expat_attrs_wattr(self):
557 parser = create_parser()
558 gather = self.AttrGatherer()
559 parser.setContentHandler(gather)
560
561 parser.feed("<doc attr='val'/>")
562 parser.close()
563
564 self.verify_attrs_wattr(gather._attrs)
565
566 def test_expat_nsattrs_empty(self):
567 parser = create_parser(1)
568 gather = self.AttrGatherer()
569 parser.setContentHandler(gather)
570
571 parser.feed("<doc/>")
572 parser.close()
573
574 self.verify_empty_nsattrs(gather._attrs)
575
576 def test_expat_nsattrs_wattr(self):
577 parser = create_parser(1)
578 gather = self.AttrGatherer()
579 parser.setContentHandler(gather)
580
581 parser.feed("<doc xmlns:ns='%s' ns:attr='val'/>" % ns_uri)
582 parser.close()
583
584 attrs = gather._attrs
585
586 self.assertEquals(attrs.getLength(), 1)
587 self.assertEquals(attrs.getNames(), [(ns_uri, "attr")])
588 self.assertTrue((attrs.getQNames() == [] or
589 attrs.getQNames() == ["ns:attr"]))
590 self.assertEquals(len(attrs), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000591 self.assertIn((ns_uri, "attr"), attrs)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000592 self.assertEquals(attrs.get((ns_uri, "attr")), "val")
593 self.assertEquals(attrs.get((ns_uri, "attr"), 25), "val")
594 self.assertEquals(list(attrs.items()), [((ns_uri, "attr"), "val")])
595 self.assertEquals(list(attrs.values()), ["val"])
596 self.assertEquals(attrs.getValue((ns_uri, "attr")), "val")
597 self.assertEquals(attrs[(ns_uri, "attr")], "val")
598
599 # ===== InputSource support
600
Benjamin Petersona7f4f5a2008-09-04 02:22:52 +0000601 def test_expat_inpsource_filename(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000602 parser = create_parser()
603 result = StringIO()
604 xmlgen = XMLGenerator(result)
605
606 parser.setContentHandler(xmlgen)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000607 parser.parse(TEST_XMLFILE)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000608
609 self.assertEquals(result.getvalue(), xml_test_out)
610
Benjamin Petersona7f4f5a2008-09-04 02:22:52 +0000611 def test_expat_inpsource_sysid(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000612 parser = create_parser()
613 result = StringIO()
614 xmlgen = XMLGenerator(result)
615
616 parser.setContentHandler(xmlgen)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000617 parser.parse(InputSource(TEST_XMLFILE))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000618
619 self.assertEquals(result.getvalue(), xml_test_out)
620
621 def test_expat_inpsource_stream(self):
622 parser = create_parser()
623 result = StringIO()
624 xmlgen = XMLGenerator(result)
625
626 parser.setContentHandler(xmlgen)
627 inpsrc = InputSource()
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000628 inpsrc.setByteStream(open(TEST_XMLFILE))
Guido van Rossumd8faa362007-04-27 19:54:29 +0000629 parser.parse(inpsrc)
630
631 self.assertEquals(result.getvalue(), xml_test_out)
632
633 # ===== IncrementalParser support
634
635 def test_expat_incremental(self):
636 result = StringIO()
637 xmlgen = XMLGenerator(result)
638 parser = create_parser()
639 parser.setContentHandler(xmlgen)
640
641 parser.feed("<doc>")
642 parser.feed("</doc>")
643 parser.close()
644
645 self.assertEquals(result.getvalue(), start + "<doc></doc>")
646
647 def test_expat_incremental_reset(self):
648 result = StringIO()
649 xmlgen = XMLGenerator(result)
650 parser = create_parser()
651 parser.setContentHandler(xmlgen)
652
653 parser.feed("<doc>")
654 parser.feed("text")
655
656 result = StringIO()
657 xmlgen = XMLGenerator(result)
658 parser.setContentHandler(xmlgen)
659 parser.reset()
660
661 parser.feed("<doc>")
662 parser.feed("text")
663 parser.feed("</doc>")
664 parser.close()
665
666 self.assertEquals(result.getvalue(), start + "<doc>text</doc>")
667
668 # ===== Locator support
669
670 def test_expat_locator_noinfo(self):
671 result = StringIO()
672 xmlgen = XMLGenerator(result)
673 parser = create_parser()
674 parser.setContentHandler(xmlgen)
675
676 parser.feed("<doc>")
677 parser.feed("</doc>")
678 parser.close()
679
680 self.assertEquals(parser.getSystemId(), None)
681 self.assertEquals(parser.getPublicId(), None)
682 self.assertEquals(parser.getLineNumber(), 1)
683
Benjamin Petersona7f4f5a2008-09-04 02:22:52 +0000684 def test_expat_locator_withinfo(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000685 result = StringIO()
686 xmlgen = XMLGenerator(result)
687 parser = create_parser()
688 parser.setContentHandler(xmlgen)
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000689 parser.parse(TEST_XMLFILE)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000690
Florent Xiclunaf15351d2010-03-13 23:24:31 +0000691 self.assertEquals(parser.getSystemId(), TEST_XMLFILE)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000692 self.assertEquals(parser.getPublicId(), None)
Lars Gustäbel2fc52942000-10-24 15:35:07 +0000693
Martin v. Löwis80670bc2000-10-06 21:13:23 +0000694
695# ===========================================================================
696#
697# error reporting
698#
699# ===========================================================================
700
Guido van Rossumd8faa362007-04-27 19:54:29 +0000701class ErrorReportingTest(unittest.TestCase):
702 def test_expat_inpsource_location(self):
703 parser = create_parser()
704 parser.setContentHandler(ContentHandler()) # do nothing
705 source = InputSource()
706 source.setByteStream(StringIO("<foo bar foobar>")) #ill-formed
707 name = "a file name"
708 source.setSystemId(name)
709 try:
710 parser.parse(source)
711 self.fail()
712 except SAXException as e:
713 self.assertEquals(e.getSystemId(), name)
Martin v. Löwis80670bc2000-10-06 21:13:23 +0000714
Guido van Rossumd8faa362007-04-27 19:54:29 +0000715 def test_expat_incomplete(self):
716 parser = create_parser()
717 parser.setContentHandler(ContentHandler()) # do nothing
718 self.assertRaises(SAXParseException, parser.parse, StringIO("<foo>"))
Martin v. Löwis80670bc2000-10-06 21:13:23 +0000719
Guido van Rossumd8faa362007-04-27 19:54:29 +0000720 def test_sax_parse_exception_str(self):
721 # pass various values from a locator to the SAXParseException to
722 # make sure that the __str__() doesn't fall apart when None is
723 # passed instead of an integer line and column number
724 #
725 # use "normal" values for the locator:
726 str(SAXParseException("message", None,
727 self.DummyLocator(1, 1)))
728 # use None for the line number:
729 str(SAXParseException("message", None,
730 self.DummyLocator(None, 1)))
731 # use None for the column number:
732 str(SAXParseException("message", None,
733 self.DummyLocator(1, None)))
734 # use None for both:
735 str(SAXParseException("message", None,
736 self.DummyLocator(None, None)))
Fred Drake6fd0b0d2004-03-20 08:15:30 +0000737
Guido van Rossumd8faa362007-04-27 19:54:29 +0000738 class DummyLocator:
739 def __init__(self, lineno, colno):
740 self._lineno = lineno
741 self._colno = colno
Fred Drake6fd0b0d2004-03-20 08:15:30 +0000742
Guido van Rossumd8faa362007-04-27 19:54:29 +0000743 def getPublicId(self):
744 return "pubid"
Fred Drake6fd0b0d2004-03-20 08:15:30 +0000745
Guido van Rossumd8faa362007-04-27 19:54:29 +0000746 def getSystemId(self):
747 return "sysid"
Fred Drake6fd0b0d2004-03-20 08:15:30 +0000748
Guido van Rossumd8faa362007-04-27 19:54:29 +0000749 def getLineNumber(self):
750 return self._lineno
Fred Drake6fd0b0d2004-03-20 08:15:30 +0000751
Guido van Rossumd8faa362007-04-27 19:54:29 +0000752 def getColumnNumber(self):
753 return self._colno
Martin v. Löwis80670bc2000-10-06 21:13:23 +0000754
Lars Gustäbelab647872000-09-24 18:40:52 +0000755# ===========================================================================
756#
757# xmlreader tests
758#
759# ===========================================================================
760
Guido van Rossumd8faa362007-04-27 19:54:29 +0000761class XmlReaderTest(XmlTestBase):
Lars Gustäbelab647872000-09-24 18:40:52 +0000762
Guido van Rossumd8faa362007-04-27 19:54:29 +0000763 # ===== AttributesImpl
764 def test_attrs_empty(self):
765 self.verify_empty_attrs(AttributesImpl({}))
Lars Gustäbelab647872000-09-24 18:40:52 +0000766
Guido van Rossumd8faa362007-04-27 19:54:29 +0000767 def test_attrs_wattr(self):
768 self.verify_attrs_wattr(AttributesImpl({"attr" : "val"}))
Lars Gustäbelab647872000-09-24 18:40:52 +0000769
Guido van Rossumd8faa362007-04-27 19:54:29 +0000770 def test_nsattrs_empty(self):
771 self.verify_empty_nsattrs(AttributesNSImpl({}, {}))
Lars Gustäbelab647872000-09-24 18:40:52 +0000772
Guido van Rossumd8faa362007-04-27 19:54:29 +0000773 def test_nsattrs_wattr(self):
774 attrs = AttributesNSImpl({(ns_uri, "attr") : "val"},
775 {(ns_uri, "attr") : "ns:attr"})
Fred Drake004d5e62000-10-23 17:22:08 +0000776
Guido van Rossumd8faa362007-04-27 19:54:29 +0000777 self.assertEquals(attrs.getLength(), 1)
778 self.assertEquals(attrs.getNames(), [(ns_uri, "attr")])
779 self.assertEquals(attrs.getQNames(), ["ns:attr"])
780 self.assertEquals(len(attrs), 1)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000781 self.assertIn((ns_uri, "attr"), attrs)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000782 self.assertEquals(list(attrs.keys()), [(ns_uri, "attr")])
783 self.assertEquals(attrs.get((ns_uri, "attr")), "val")
784 self.assertEquals(attrs.get((ns_uri, "attr"), 25), "val")
785 self.assertEquals(list(attrs.items()), [((ns_uri, "attr"), "val")])
786 self.assertEquals(list(attrs.values()), ["val"])
787 self.assertEquals(attrs.getValue((ns_uri, "attr")), "val")
788 self.assertEquals(attrs.getValueByQName("ns:attr"), "val")
789 self.assertEquals(attrs.getNameByQName("ns:attr"), (ns_uri, "attr"))
790 self.assertEquals(attrs[(ns_uri, "attr")], "val")
791 self.assertEquals(attrs.getQNameByName((ns_uri, "attr")), "ns:attr")
Fred Drake004d5e62000-10-23 17:22:08 +0000792
Lars Gustäbelab647872000-09-24 18:40:52 +0000793
Guido van Rossumd8faa362007-04-27 19:54:29 +0000794 # During the development of Python 2.5, an attempt to move the "xml"
795 # package implementation to a new package ("xmlcore") proved painful.
796 # The goal of this change was to allow applications to be able to
797 # obtain and rely on behavior in the standard library implementation
798 # of the XML support without needing to be concerned about the
799 # availability of the PyXML implementation.
800 #
801 # While the existing import hackery in Lib/xml/__init__.py can cause
802 # PyXML's _xmlpus package to supplant the "xml" package, that only
803 # works because either implementation uses the "xml" package name for
804 # imports.
805 #
806 # The move resulted in a number of problems related to the fact that
807 # the import machinery's "package context" is based on the name that's
808 # being imported rather than the __name__ of the actual package
809 # containment; it wasn't possible for the "xml" package to be replaced
810 # by a simple module that indirected imports to the "xmlcore" package.
811 #
812 # The following two tests exercised bugs that were introduced in that
813 # attempt. Keeping these tests around will help detect problems with
814 # other attempts to provide reliable access to the standard library's
815 # implementation of the XML support.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000816
Guido van Rossumd8faa362007-04-27 19:54:29 +0000817 def test_sf_1511497(self):
818 # Bug report: http://www.python.org/sf/1511497
819 import sys
820 old_modules = sys.modules.copy()
821 for modname in list(sys.modules.keys()):
822 if modname.startswith("xml."):
823 del sys.modules[modname]
824 try:
825 import xml.sax.expatreader
826 module = xml.sax.expatreader
827 self.assertEquals(module.__name__, "xml.sax.expatreader")
828 finally:
829 sys.modules.update(old_modules)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000830
Guido van Rossumd8faa362007-04-27 19:54:29 +0000831 def test_sf_1513611(self):
832 # Bug report: http://www.python.org/sf/1513611
833 sio = StringIO("invalid")
834 parser = make_parser()
835 from xml.sax import SAXParseException
836 self.assertRaises(SAXParseException, parser.parse, sio)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000837
Lars Gustäbel96753b32000-09-24 12:24:24 +0000838
Christian Heimesbbe741d2008-03-28 10:53:29 +0000839def test_main():
Guido van Rossumd8faa362007-04-27 19:54:29 +0000840 run_unittest(MakeParserTest,
841 SaxutilsTest,
842 XmlgenTest,
843 ExpatReaderTest,
844 ErrorReportingTest,
845 XmlReaderTest)
Lars Gustäbelb7536d52000-09-24 18:53:56 +0000846
Guido van Rossumd8faa362007-04-27 19:54:29 +0000847if __name__ == "__main__":
Christian Heimesbbe741d2008-03-28 10:53:29 +0000848 test_main()