1 | n/a | # XXX TypeErrors on calling handlers, or on bad return values from a |
---|
2 | n/a | # handler, are obscure and unhelpful. |
---|
3 | n/a | |
---|
4 | n/a | from io import BytesIO |
---|
5 | n/a | import os |
---|
6 | n/a | import sys |
---|
7 | n/a | import sysconfig |
---|
8 | n/a | import unittest |
---|
9 | n/a | import traceback |
---|
10 | n/a | |
---|
11 | n/a | from xml.parsers import expat |
---|
12 | n/a | from xml.parsers.expat import errors |
---|
13 | n/a | |
---|
14 | n/a | from test.support import sortdict |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | class SetAttributeTest(unittest.TestCase): |
---|
18 | n/a | def setUp(self): |
---|
19 | n/a | self.parser = expat.ParserCreate(namespace_separator='!') |
---|
20 | n/a | |
---|
21 | n/a | def test_buffer_text(self): |
---|
22 | n/a | self.assertIs(self.parser.buffer_text, False) |
---|
23 | n/a | for x in 0, 1, 2, 0: |
---|
24 | n/a | self.parser.buffer_text = x |
---|
25 | n/a | self.assertIs(self.parser.buffer_text, bool(x)) |
---|
26 | n/a | |
---|
27 | n/a | def test_namespace_prefixes(self): |
---|
28 | n/a | self.assertIs(self.parser.namespace_prefixes, False) |
---|
29 | n/a | for x in 0, 1, 2, 0: |
---|
30 | n/a | self.parser.namespace_prefixes = x |
---|
31 | n/a | self.assertIs(self.parser.namespace_prefixes, bool(x)) |
---|
32 | n/a | |
---|
33 | n/a | def test_ordered_attributes(self): |
---|
34 | n/a | self.assertIs(self.parser.ordered_attributes, False) |
---|
35 | n/a | for x in 0, 1, 2, 0: |
---|
36 | n/a | self.parser.ordered_attributes = x |
---|
37 | n/a | self.assertIs(self.parser.ordered_attributes, bool(x)) |
---|
38 | n/a | |
---|
39 | n/a | def test_specified_attributes(self): |
---|
40 | n/a | self.assertIs(self.parser.specified_attributes, False) |
---|
41 | n/a | for x in 0, 1, 2, 0: |
---|
42 | n/a | self.parser.specified_attributes = x |
---|
43 | n/a | self.assertIs(self.parser.specified_attributes, bool(x)) |
---|
44 | n/a | |
---|
45 | n/a | def test_invalid_attributes(self): |
---|
46 | n/a | with self.assertRaises(AttributeError): |
---|
47 | n/a | self.parser.returns_unicode = 1 |
---|
48 | n/a | with self.assertRaises(AttributeError): |
---|
49 | n/a | self.parser.returns_unicode |
---|
50 | n/a | |
---|
51 | n/a | # Issue #25019 |
---|
52 | n/a | self.assertRaises(TypeError, setattr, self.parser, range(0xF), 0) |
---|
53 | n/a | self.assertRaises(TypeError, self.parser.__setattr__, range(0xF), 0) |
---|
54 | n/a | self.assertRaises(TypeError, getattr, self.parser, range(0xF)) |
---|
55 | n/a | |
---|
56 | n/a | |
---|
57 | n/a | data = b'''\ |
---|
58 | n/a | <?xml version="1.0" encoding="iso-8859-1" standalone="no"?> |
---|
59 | n/a | <?xml-stylesheet href="stylesheet.css"?> |
---|
60 | n/a | <!-- comment data --> |
---|
61 | n/a | <!DOCTYPE quotations SYSTEM "quotations.dtd" [ |
---|
62 | n/a | <!ELEMENT root ANY> |
---|
63 | n/a | <!ATTLIST root attr1 CDATA #REQUIRED attr2 CDATA #IMPLIED> |
---|
64 | n/a | <!NOTATION notation SYSTEM "notation.jpeg"> |
---|
65 | n/a | <!ENTITY acirc "â"> |
---|
66 | n/a | <!ENTITY external_entity SYSTEM "entity.file"> |
---|
67 | n/a | <!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation> |
---|
68 | n/a | %unparsed_entity; |
---|
69 | n/a | ]> |
---|
70 | n/a | |
---|
71 | n/a | <root attr1="value1" attr2="value2ὀ"> |
---|
72 | n/a | <myns:subelement xmlns:myns="http://www.python.org/namespace"> |
---|
73 | n/a | Contents of subelements |
---|
74 | n/a | </myns:subelement> |
---|
75 | n/a | <sub2><![CDATA[contents of CDATA section]]></sub2> |
---|
76 | n/a | &external_entity; |
---|
77 | n/a | &skipped_entity; |
---|
78 | n/a | \xb5 |
---|
79 | n/a | </root> |
---|
80 | n/a | ''' |
---|
81 | n/a | |
---|
82 | n/a | |
---|
83 | n/a | # Produce UTF-8 output |
---|
84 | n/a | class ParseTest(unittest.TestCase): |
---|
85 | n/a | class Outputter: |
---|
86 | n/a | def __init__(self): |
---|
87 | n/a | self.out = [] |
---|
88 | n/a | |
---|
89 | n/a | def StartElementHandler(self, name, attrs): |
---|
90 | n/a | self.out.append('Start element: ' + repr(name) + ' ' + |
---|
91 | n/a | sortdict(attrs)) |
---|
92 | n/a | |
---|
93 | n/a | def EndElementHandler(self, name): |
---|
94 | n/a | self.out.append('End element: ' + repr(name)) |
---|
95 | n/a | |
---|
96 | n/a | def CharacterDataHandler(self, data): |
---|
97 | n/a | data = data.strip() |
---|
98 | n/a | if data: |
---|
99 | n/a | self.out.append('Character data: ' + repr(data)) |
---|
100 | n/a | |
---|
101 | n/a | def ProcessingInstructionHandler(self, target, data): |
---|
102 | n/a | self.out.append('PI: ' + repr(target) + ' ' + repr(data)) |
---|
103 | n/a | |
---|
104 | n/a | def StartNamespaceDeclHandler(self, prefix, uri): |
---|
105 | n/a | self.out.append('NS decl: ' + repr(prefix) + ' ' + repr(uri)) |
---|
106 | n/a | |
---|
107 | n/a | def EndNamespaceDeclHandler(self, prefix): |
---|
108 | n/a | self.out.append('End of NS decl: ' + repr(prefix)) |
---|
109 | n/a | |
---|
110 | n/a | def StartCdataSectionHandler(self): |
---|
111 | n/a | self.out.append('Start of CDATA section') |
---|
112 | n/a | |
---|
113 | n/a | def EndCdataSectionHandler(self): |
---|
114 | n/a | self.out.append('End of CDATA section') |
---|
115 | n/a | |
---|
116 | n/a | def CommentHandler(self, text): |
---|
117 | n/a | self.out.append('Comment: ' + repr(text)) |
---|
118 | n/a | |
---|
119 | n/a | def NotationDeclHandler(self, *args): |
---|
120 | n/a | name, base, sysid, pubid = args |
---|
121 | n/a | self.out.append('Notation declared: %s' %(args,)) |
---|
122 | n/a | |
---|
123 | n/a | def UnparsedEntityDeclHandler(self, *args): |
---|
124 | n/a | entityName, base, systemId, publicId, notationName = args |
---|
125 | n/a | self.out.append('Unparsed entity decl: %s' %(args,)) |
---|
126 | n/a | |
---|
127 | n/a | def NotStandaloneHandler(self): |
---|
128 | n/a | self.out.append('Not standalone') |
---|
129 | n/a | return 1 |
---|
130 | n/a | |
---|
131 | n/a | def ExternalEntityRefHandler(self, *args): |
---|
132 | n/a | context, base, sysId, pubId = args |
---|
133 | n/a | self.out.append('External entity ref: %s' %(args[1:],)) |
---|
134 | n/a | return 1 |
---|
135 | n/a | |
---|
136 | n/a | def StartDoctypeDeclHandler(self, *args): |
---|
137 | n/a | self.out.append(('Start doctype', args)) |
---|
138 | n/a | return 1 |
---|
139 | n/a | |
---|
140 | n/a | def EndDoctypeDeclHandler(self): |
---|
141 | n/a | self.out.append("End doctype") |
---|
142 | n/a | return 1 |
---|
143 | n/a | |
---|
144 | n/a | def EntityDeclHandler(self, *args): |
---|
145 | n/a | self.out.append(('Entity declaration', args)) |
---|
146 | n/a | return 1 |
---|
147 | n/a | |
---|
148 | n/a | def XmlDeclHandler(self, *args): |
---|
149 | n/a | self.out.append(('XML declaration', args)) |
---|
150 | n/a | return 1 |
---|
151 | n/a | |
---|
152 | n/a | def ElementDeclHandler(self, *args): |
---|
153 | n/a | self.out.append(('Element declaration', args)) |
---|
154 | n/a | return 1 |
---|
155 | n/a | |
---|
156 | n/a | def AttlistDeclHandler(self, *args): |
---|
157 | n/a | self.out.append(('Attribute list declaration', args)) |
---|
158 | n/a | return 1 |
---|
159 | n/a | |
---|
160 | n/a | def SkippedEntityHandler(self, *args): |
---|
161 | n/a | self.out.append(("Skipped entity", args)) |
---|
162 | n/a | return 1 |
---|
163 | n/a | |
---|
164 | n/a | def DefaultHandler(self, userData): |
---|
165 | n/a | pass |
---|
166 | n/a | |
---|
167 | n/a | def DefaultHandlerExpand(self, userData): |
---|
168 | n/a | pass |
---|
169 | n/a | |
---|
170 | n/a | handler_names = [ |
---|
171 | n/a | 'StartElementHandler', 'EndElementHandler', 'CharacterDataHandler', |
---|
172 | n/a | 'ProcessingInstructionHandler', 'UnparsedEntityDeclHandler', |
---|
173 | n/a | 'NotationDeclHandler', 'StartNamespaceDeclHandler', |
---|
174 | n/a | 'EndNamespaceDeclHandler', 'CommentHandler', |
---|
175 | n/a | 'StartCdataSectionHandler', 'EndCdataSectionHandler', 'DefaultHandler', |
---|
176 | n/a | 'DefaultHandlerExpand', 'NotStandaloneHandler', |
---|
177 | n/a | 'ExternalEntityRefHandler', 'StartDoctypeDeclHandler', |
---|
178 | n/a | 'EndDoctypeDeclHandler', 'EntityDeclHandler', 'XmlDeclHandler', |
---|
179 | n/a | 'ElementDeclHandler', 'AttlistDeclHandler', 'SkippedEntityHandler', |
---|
180 | n/a | ] |
---|
181 | n/a | |
---|
182 | n/a | def _hookup_callbacks(self, parser, handler): |
---|
183 | n/a | """ |
---|
184 | n/a | Set each of the callbacks defined on handler and named in |
---|
185 | n/a | self.handler_names on the given parser. |
---|
186 | n/a | """ |
---|
187 | n/a | for name in self.handler_names: |
---|
188 | n/a | setattr(parser, name, getattr(handler, name)) |
---|
189 | n/a | |
---|
190 | n/a | def _verify_parse_output(self, operations): |
---|
191 | n/a | expected_operations = [ |
---|
192 | n/a | ('XML declaration', ('1.0', 'iso-8859-1', 0)), |
---|
193 | n/a | 'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'', |
---|
194 | n/a | "Comment: ' comment data '", |
---|
195 | n/a | "Not standalone", |
---|
196 | n/a | ("Start doctype", ('quotations', 'quotations.dtd', None, 1)), |
---|
197 | n/a | ('Element declaration', ('root', (2, 0, None, ()))), |
---|
198 | n/a | ('Attribute list declaration', ('root', 'attr1', 'CDATA', None, |
---|
199 | n/a | 1)), |
---|
200 | n/a | ('Attribute list declaration', ('root', 'attr2', 'CDATA', None, |
---|
201 | n/a | 0)), |
---|
202 | n/a | "Notation declared: ('notation', None, 'notation.jpeg', None)", |
---|
203 | n/a | ('Entity declaration', ('acirc', 0, '\xe2', None, None, None, None)), |
---|
204 | n/a | ('Entity declaration', ('external_entity', 0, None, None, |
---|
205 | n/a | 'entity.file', None, None)), |
---|
206 | n/a | "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')", |
---|
207 | n/a | "Not standalone", |
---|
208 | n/a | "End doctype", |
---|
209 | n/a | "Start element: 'root' {'attr1': 'value1', 'attr2': 'value2\u1f40'}", |
---|
210 | n/a | "NS decl: 'myns' 'http://www.python.org/namespace'", |
---|
211 | n/a | "Start element: 'http://www.python.org/namespace!subelement' {}", |
---|
212 | n/a | "Character data: 'Contents of subelements'", |
---|
213 | n/a | "End element: 'http://www.python.org/namespace!subelement'", |
---|
214 | n/a | "End of NS decl: 'myns'", |
---|
215 | n/a | "Start element: 'sub2' {}", |
---|
216 | n/a | 'Start of CDATA section', |
---|
217 | n/a | "Character data: 'contents of CDATA section'", |
---|
218 | n/a | 'End of CDATA section', |
---|
219 | n/a | "End element: 'sub2'", |
---|
220 | n/a | "External entity ref: (None, 'entity.file', None)", |
---|
221 | n/a | ('Skipped entity', ('skipped_entity', 0)), |
---|
222 | n/a | "Character data: '\xb5'", |
---|
223 | n/a | "End element: 'root'", |
---|
224 | n/a | ] |
---|
225 | n/a | for operation, expected_operation in zip(operations, expected_operations): |
---|
226 | n/a | self.assertEqual(operation, expected_operation) |
---|
227 | n/a | |
---|
228 | n/a | def test_parse_bytes(self): |
---|
229 | n/a | out = self.Outputter() |
---|
230 | n/a | parser = expat.ParserCreate(namespace_separator='!') |
---|
231 | n/a | self._hookup_callbacks(parser, out) |
---|
232 | n/a | |
---|
233 | n/a | parser.Parse(data, 1) |
---|
234 | n/a | |
---|
235 | n/a | operations = out.out |
---|
236 | n/a | self._verify_parse_output(operations) |
---|
237 | n/a | # Issue #6697. |
---|
238 | n/a | self.assertRaises(AttributeError, getattr, parser, '\uD800') |
---|
239 | n/a | |
---|
240 | n/a | def test_parse_str(self): |
---|
241 | n/a | out = self.Outputter() |
---|
242 | n/a | parser = expat.ParserCreate(namespace_separator='!') |
---|
243 | n/a | self._hookup_callbacks(parser, out) |
---|
244 | n/a | |
---|
245 | n/a | parser.Parse(data.decode('iso-8859-1'), 1) |
---|
246 | n/a | |
---|
247 | n/a | operations = out.out |
---|
248 | n/a | self._verify_parse_output(operations) |
---|
249 | n/a | |
---|
250 | n/a | def test_parse_file(self): |
---|
251 | n/a | # Try parsing a file |
---|
252 | n/a | out = self.Outputter() |
---|
253 | n/a | parser = expat.ParserCreate(namespace_separator='!') |
---|
254 | n/a | self._hookup_callbacks(parser, out) |
---|
255 | n/a | file = BytesIO(data) |
---|
256 | n/a | |
---|
257 | n/a | parser.ParseFile(file) |
---|
258 | n/a | |
---|
259 | n/a | operations = out.out |
---|
260 | n/a | self._verify_parse_output(operations) |
---|
261 | n/a | |
---|
262 | n/a | def test_parse_again(self): |
---|
263 | n/a | parser = expat.ParserCreate() |
---|
264 | n/a | file = BytesIO(data) |
---|
265 | n/a | parser.ParseFile(file) |
---|
266 | n/a | # Issue 6676: ensure a meaningful exception is raised when attempting |
---|
267 | n/a | # to parse more than one XML document per xmlparser instance, |
---|
268 | n/a | # a limitation of the Expat library. |
---|
269 | n/a | with self.assertRaises(expat.error) as cm: |
---|
270 | n/a | parser.ParseFile(file) |
---|
271 | n/a | self.assertEqual(expat.ErrorString(cm.exception.code), |
---|
272 | n/a | expat.errors.XML_ERROR_FINISHED) |
---|
273 | n/a | |
---|
274 | n/a | class NamespaceSeparatorTest(unittest.TestCase): |
---|
275 | n/a | def test_legal(self): |
---|
276 | n/a | # Tests that make sure we get errors when the namespace_separator value |
---|
277 | n/a | # is illegal, and that we don't for good values: |
---|
278 | n/a | expat.ParserCreate() |
---|
279 | n/a | expat.ParserCreate(namespace_separator=None) |
---|
280 | n/a | expat.ParserCreate(namespace_separator=' ') |
---|
281 | n/a | |
---|
282 | n/a | def test_illegal(self): |
---|
283 | n/a | try: |
---|
284 | n/a | expat.ParserCreate(namespace_separator=42) |
---|
285 | n/a | self.fail() |
---|
286 | n/a | except TypeError as e: |
---|
287 | n/a | self.assertEqual(str(e), |
---|
288 | n/a | 'ParserCreate() argument 2 must be str or None, not int') |
---|
289 | n/a | |
---|
290 | n/a | try: |
---|
291 | n/a | expat.ParserCreate(namespace_separator='too long') |
---|
292 | n/a | self.fail() |
---|
293 | n/a | except ValueError as e: |
---|
294 | n/a | self.assertEqual(str(e), |
---|
295 | n/a | 'namespace_separator must be at most one character, omitted, or None') |
---|
296 | n/a | |
---|
297 | n/a | def test_zero_length(self): |
---|
298 | n/a | # ParserCreate() needs to accept a namespace_separator of zero length |
---|
299 | n/a | # to satisfy the requirements of RDF applications that are required |
---|
300 | n/a | # to simply glue together the namespace URI and the localname. Though |
---|
301 | n/a | # considered a wart of the RDF specifications, it needs to be supported. |
---|
302 | n/a | # |
---|
303 | n/a | # See XML-SIG mailing list thread starting with |
---|
304 | n/a | # http://mail.python.org/pipermail/xml-sig/2001-April/005202.html |
---|
305 | n/a | # |
---|
306 | n/a | expat.ParserCreate(namespace_separator='') # too short |
---|
307 | n/a | |
---|
308 | n/a | |
---|
309 | n/a | class InterningTest(unittest.TestCase): |
---|
310 | n/a | def test(self): |
---|
311 | n/a | # Test the interning machinery. |
---|
312 | n/a | p = expat.ParserCreate() |
---|
313 | n/a | L = [] |
---|
314 | n/a | def collector(name, *args): |
---|
315 | n/a | L.append(name) |
---|
316 | n/a | p.StartElementHandler = collector |
---|
317 | n/a | p.EndElementHandler = collector |
---|
318 | n/a | p.Parse(b"<e> <e/> <e></e> </e>", 1) |
---|
319 | n/a | tag = L[0] |
---|
320 | n/a | self.assertEqual(len(L), 6) |
---|
321 | n/a | for entry in L: |
---|
322 | n/a | # L should have the same string repeated over and over. |
---|
323 | n/a | self.assertTrue(tag is entry) |
---|
324 | n/a | |
---|
325 | n/a | def test_issue9402(self): |
---|
326 | n/a | # create an ExternalEntityParserCreate with buffer text |
---|
327 | n/a | class ExternalOutputter: |
---|
328 | n/a | def __init__(self, parser): |
---|
329 | n/a | self.parser = parser |
---|
330 | n/a | self.parser_result = None |
---|
331 | n/a | |
---|
332 | n/a | def ExternalEntityRefHandler(self, context, base, sysId, pubId): |
---|
333 | n/a | external_parser = self.parser.ExternalEntityParserCreate("") |
---|
334 | n/a | self.parser_result = external_parser.Parse(b"", 1) |
---|
335 | n/a | return 1 |
---|
336 | n/a | |
---|
337 | n/a | parser = expat.ParserCreate(namespace_separator='!') |
---|
338 | n/a | parser.buffer_text = 1 |
---|
339 | n/a | out = ExternalOutputter(parser) |
---|
340 | n/a | parser.ExternalEntityRefHandler = out.ExternalEntityRefHandler |
---|
341 | n/a | parser.Parse(data, 1) |
---|
342 | n/a | self.assertEqual(out.parser_result, 1) |
---|
343 | n/a | |
---|
344 | n/a | |
---|
345 | n/a | class BufferTextTest(unittest.TestCase): |
---|
346 | n/a | def setUp(self): |
---|
347 | n/a | self.stuff = [] |
---|
348 | n/a | self.parser = expat.ParserCreate() |
---|
349 | n/a | self.parser.buffer_text = 1 |
---|
350 | n/a | self.parser.CharacterDataHandler = self.CharacterDataHandler |
---|
351 | n/a | |
---|
352 | n/a | def check(self, expected, label): |
---|
353 | n/a | self.assertEqual(self.stuff, expected, |
---|
354 | n/a | "%s\nstuff = %r\nexpected = %r" |
---|
355 | n/a | % (label, self.stuff, map(str, expected))) |
---|
356 | n/a | |
---|
357 | n/a | def CharacterDataHandler(self, text): |
---|
358 | n/a | self.stuff.append(text) |
---|
359 | n/a | |
---|
360 | n/a | def StartElementHandler(self, name, attrs): |
---|
361 | n/a | self.stuff.append("<%s>" % name) |
---|
362 | n/a | bt = attrs.get("buffer-text") |
---|
363 | n/a | if bt == "yes": |
---|
364 | n/a | self.parser.buffer_text = 1 |
---|
365 | n/a | elif bt == "no": |
---|
366 | n/a | self.parser.buffer_text = 0 |
---|
367 | n/a | |
---|
368 | n/a | def EndElementHandler(self, name): |
---|
369 | n/a | self.stuff.append("</%s>" % name) |
---|
370 | n/a | |
---|
371 | n/a | def CommentHandler(self, data): |
---|
372 | n/a | self.stuff.append("<!--%s-->" % data) |
---|
373 | n/a | |
---|
374 | n/a | def setHandlers(self, handlers=[]): |
---|
375 | n/a | for name in handlers: |
---|
376 | n/a | setattr(self.parser, name, getattr(self, name)) |
---|
377 | n/a | |
---|
378 | n/a | def test_default_to_disabled(self): |
---|
379 | n/a | parser = expat.ParserCreate() |
---|
380 | n/a | self.assertFalse(parser.buffer_text) |
---|
381 | n/a | |
---|
382 | n/a | def test_buffering_enabled(self): |
---|
383 | n/a | # Make sure buffering is turned on |
---|
384 | n/a | self.assertTrue(self.parser.buffer_text) |
---|
385 | n/a | self.parser.Parse(b"<a>1<b/>2<c/>3</a>", 1) |
---|
386 | n/a | self.assertEqual(self.stuff, ['123'], |
---|
387 | n/a | "buffered text not properly collapsed") |
---|
388 | n/a | |
---|
389 | n/a | def test1(self): |
---|
390 | n/a | # XXX This test exposes more detail of Expat's text chunking than we |
---|
391 | n/a | # XXX like, but it tests what we need to concisely. |
---|
392 | n/a | self.setHandlers(["StartElementHandler"]) |
---|
393 | n/a | self.parser.Parse(b"<a>1<b buffer-text='no'/>2\n3<c buffer-text='yes'/>4\n5</a>", 1) |
---|
394 | n/a | self.assertEqual(self.stuff, |
---|
395 | n/a | ["<a>", "1", "<b>", "2", "\n", "3", "<c>", "4\n5"], |
---|
396 | n/a | "buffering control not reacting as expected") |
---|
397 | n/a | |
---|
398 | n/a | def test2(self): |
---|
399 | n/a | self.parser.Parse(b"<a>1<b/><2><c/> \n 3</a>", 1) |
---|
400 | n/a | self.assertEqual(self.stuff, ["1<2> \n 3"], |
---|
401 | n/a | "buffered text not properly collapsed") |
---|
402 | n/a | |
---|
403 | n/a | def test3(self): |
---|
404 | n/a | self.setHandlers(["StartElementHandler"]) |
---|
405 | n/a | self.parser.Parse(b"<a>1<b/>2<c/>3</a>", 1) |
---|
406 | n/a | self.assertEqual(self.stuff, ["<a>", "1", "<b>", "2", "<c>", "3"], |
---|
407 | n/a | "buffered text not properly split") |
---|
408 | n/a | |
---|
409 | n/a | def test4(self): |
---|
410 | n/a | self.setHandlers(["StartElementHandler", "EndElementHandler"]) |
---|
411 | n/a | self.parser.CharacterDataHandler = None |
---|
412 | n/a | self.parser.Parse(b"<a>1<b/>2<c/>3</a>", 1) |
---|
413 | n/a | self.assertEqual(self.stuff, |
---|
414 | n/a | ["<a>", "<b>", "</b>", "<c>", "</c>", "</a>"]) |
---|
415 | n/a | |
---|
416 | n/a | def test5(self): |
---|
417 | n/a | self.setHandlers(["StartElementHandler", "EndElementHandler"]) |
---|
418 | n/a | self.parser.Parse(b"<a>1<b></b>2<c/>3</a>", 1) |
---|
419 | n/a | self.assertEqual(self.stuff, |
---|
420 | n/a | ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", "</a>"]) |
---|
421 | n/a | |
---|
422 | n/a | def test6(self): |
---|
423 | n/a | self.setHandlers(["CommentHandler", "EndElementHandler", |
---|
424 | n/a | "StartElementHandler"]) |
---|
425 | n/a | self.parser.Parse(b"<a>1<b/>2<c></c>345</a> ", 1) |
---|
426 | n/a | self.assertEqual(self.stuff, |
---|
427 | n/a | ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "345", "</a>"], |
---|
428 | n/a | "buffered text not properly split") |
---|
429 | n/a | |
---|
430 | n/a | def test7(self): |
---|
431 | n/a | self.setHandlers(["CommentHandler", "EndElementHandler", |
---|
432 | n/a | "StartElementHandler"]) |
---|
433 | n/a | self.parser.Parse(b"<a>1<b/>2<c></c>3<!--abc-->4<!--def-->5</a> ", 1) |
---|
434 | n/a | self.assertEqual(self.stuff, |
---|
435 | n/a | ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", |
---|
436 | n/a | "<!--abc-->", "4", "<!--def-->", "5", "</a>"], |
---|
437 | n/a | "buffered text not properly split") |
---|
438 | n/a | |
---|
439 | n/a | |
---|
440 | n/a | # Test handling of exception from callback: |
---|
441 | n/a | class HandlerExceptionTest(unittest.TestCase): |
---|
442 | n/a | def StartElementHandler(self, name, attrs): |
---|
443 | n/a | raise RuntimeError(name) |
---|
444 | n/a | |
---|
445 | n/a | def check_traceback_entry(self, entry, filename, funcname): |
---|
446 | n/a | self.assertEqual(os.path.basename(entry[0]), filename) |
---|
447 | n/a | self.assertEqual(entry[2], funcname) |
---|
448 | n/a | |
---|
449 | n/a | def test_exception(self): |
---|
450 | n/a | parser = expat.ParserCreate() |
---|
451 | n/a | parser.StartElementHandler = self.StartElementHandler |
---|
452 | n/a | try: |
---|
453 | n/a | parser.Parse(b"<a><b><c/></b></a>", 1) |
---|
454 | n/a | self.fail() |
---|
455 | n/a | except RuntimeError as e: |
---|
456 | n/a | self.assertEqual(e.args[0], 'a', |
---|
457 | n/a | "Expected RuntimeError for element 'a', but" + \ |
---|
458 | n/a | " found %r" % e.args[0]) |
---|
459 | n/a | # Check that the traceback contains the relevant line in pyexpat.c |
---|
460 | n/a | entries = traceback.extract_tb(e.__traceback__) |
---|
461 | n/a | self.assertEqual(len(entries), 3) |
---|
462 | n/a | self.check_traceback_entry(entries[0], |
---|
463 | n/a | "test_pyexpat.py", "test_exception") |
---|
464 | n/a | self.check_traceback_entry(entries[1], |
---|
465 | n/a | "pyexpat.c", "StartElement") |
---|
466 | n/a | self.check_traceback_entry(entries[2], |
---|
467 | n/a | "test_pyexpat.py", "StartElementHandler") |
---|
468 | n/a | if sysconfig.is_python_build(): |
---|
469 | n/a | self.assertIn('call_with_frame("StartElement"', entries[1][3]) |
---|
470 | n/a | |
---|
471 | n/a | |
---|
472 | n/a | # Test Current* members: |
---|
473 | n/a | class PositionTest(unittest.TestCase): |
---|
474 | n/a | def StartElementHandler(self, name, attrs): |
---|
475 | n/a | self.check_pos('s') |
---|
476 | n/a | |
---|
477 | n/a | def EndElementHandler(self, name): |
---|
478 | n/a | self.check_pos('e') |
---|
479 | n/a | |
---|
480 | n/a | def check_pos(self, event): |
---|
481 | n/a | pos = (event, |
---|
482 | n/a | self.parser.CurrentByteIndex, |
---|
483 | n/a | self.parser.CurrentLineNumber, |
---|
484 | n/a | self.parser.CurrentColumnNumber) |
---|
485 | n/a | self.assertTrue(self.upto < len(self.expected_list), |
---|
486 | n/a | 'too many parser events') |
---|
487 | n/a | expected = self.expected_list[self.upto] |
---|
488 | n/a | self.assertEqual(pos, expected, |
---|
489 | n/a | 'Expected position %s, got position %s' %(pos, expected)) |
---|
490 | n/a | self.upto += 1 |
---|
491 | n/a | |
---|
492 | n/a | def test(self): |
---|
493 | n/a | self.parser = expat.ParserCreate() |
---|
494 | n/a | self.parser.StartElementHandler = self.StartElementHandler |
---|
495 | n/a | self.parser.EndElementHandler = self.EndElementHandler |
---|
496 | n/a | self.upto = 0 |
---|
497 | n/a | self.expected_list = [('s', 0, 1, 0), ('s', 5, 2, 1), ('s', 11, 3, 2), |
---|
498 | n/a | ('e', 15, 3, 6), ('e', 17, 4, 1), ('e', 22, 5, 0)] |
---|
499 | n/a | |
---|
500 | n/a | xml = b'<a>\n <b>\n <c/>\n </b>\n</a>' |
---|
501 | n/a | self.parser.Parse(xml, 1) |
---|
502 | n/a | |
---|
503 | n/a | |
---|
504 | n/a | class sf1296433Test(unittest.TestCase): |
---|
505 | n/a | def test_parse_only_xml_data(self): |
---|
506 | n/a | # http://python.org/sf/1296433 |
---|
507 | n/a | # |
---|
508 | n/a | xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * 1025) |
---|
509 | n/a | # this one doesn't crash |
---|
510 | n/a | #xml = "<?xml version='1.0'?><s>%s</s>" % ('a' * 10000) |
---|
511 | n/a | |
---|
512 | n/a | class SpecificException(Exception): |
---|
513 | n/a | pass |
---|
514 | n/a | |
---|
515 | n/a | def handler(text): |
---|
516 | n/a | raise SpecificException |
---|
517 | n/a | |
---|
518 | n/a | parser = expat.ParserCreate() |
---|
519 | n/a | parser.CharacterDataHandler = handler |
---|
520 | n/a | |
---|
521 | n/a | self.assertRaises(Exception, parser.Parse, xml.encode('iso8859')) |
---|
522 | n/a | |
---|
523 | n/a | class ChardataBufferTest(unittest.TestCase): |
---|
524 | n/a | """ |
---|
525 | n/a | test setting of chardata buffer size |
---|
526 | n/a | """ |
---|
527 | n/a | |
---|
528 | n/a | def test_1025_bytes(self): |
---|
529 | n/a | self.assertEqual(self.small_buffer_test(1025), 2) |
---|
530 | n/a | |
---|
531 | n/a | def test_1000_bytes(self): |
---|
532 | n/a | self.assertEqual(self.small_buffer_test(1000), 1) |
---|
533 | n/a | |
---|
534 | n/a | def test_wrong_size(self): |
---|
535 | n/a | parser = expat.ParserCreate() |
---|
536 | n/a | parser.buffer_text = 1 |
---|
537 | n/a | with self.assertRaises(ValueError): |
---|
538 | n/a | parser.buffer_size = -1 |
---|
539 | n/a | with self.assertRaises(ValueError): |
---|
540 | n/a | parser.buffer_size = 0 |
---|
541 | n/a | with self.assertRaises((ValueError, OverflowError)): |
---|
542 | n/a | parser.buffer_size = sys.maxsize + 1 |
---|
543 | n/a | with self.assertRaises(TypeError): |
---|
544 | n/a | parser.buffer_size = 512.0 |
---|
545 | n/a | |
---|
546 | n/a | def test_unchanged_size(self): |
---|
547 | n/a | xml1 = b"<?xml version='1.0' encoding='iso8859'?><s>" + b'a' * 512 |
---|
548 | n/a | xml2 = b'a'*512 + b'</s>' |
---|
549 | n/a | parser = expat.ParserCreate() |
---|
550 | n/a | parser.CharacterDataHandler = self.counting_handler |
---|
551 | n/a | parser.buffer_size = 512 |
---|
552 | n/a | parser.buffer_text = 1 |
---|
553 | n/a | |
---|
554 | n/a | # Feed 512 bytes of character data: the handler should be called |
---|
555 | n/a | # once. |
---|
556 | n/a | self.n = 0 |
---|
557 | n/a | parser.Parse(xml1) |
---|
558 | n/a | self.assertEqual(self.n, 1) |
---|
559 | n/a | |
---|
560 | n/a | # Reassign to buffer_size, but assign the same size. |
---|
561 | n/a | parser.buffer_size = parser.buffer_size |
---|
562 | n/a | self.assertEqual(self.n, 1) |
---|
563 | n/a | |
---|
564 | n/a | # Try parsing rest of the document |
---|
565 | n/a | parser.Parse(xml2) |
---|
566 | n/a | self.assertEqual(self.n, 2) |
---|
567 | n/a | |
---|
568 | n/a | |
---|
569 | n/a | def test_disabling_buffer(self): |
---|
570 | n/a | xml1 = b"<?xml version='1.0' encoding='iso8859'?><a>" + b'a' * 512 |
---|
571 | n/a | xml2 = b'b' * 1024 |
---|
572 | n/a | xml3 = b'c' * 1024 + b'</a>'; |
---|
573 | n/a | parser = expat.ParserCreate() |
---|
574 | n/a | parser.CharacterDataHandler = self.counting_handler |
---|
575 | n/a | parser.buffer_text = 1 |
---|
576 | n/a | parser.buffer_size = 1024 |
---|
577 | n/a | self.assertEqual(parser.buffer_size, 1024) |
---|
578 | n/a | |
---|
579 | n/a | # Parse one chunk of XML |
---|
580 | n/a | self.n = 0 |
---|
581 | n/a | parser.Parse(xml1, 0) |
---|
582 | n/a | self.assertEqual(parser.buffer_size, 1024) |
---|
583 | n/a | self.assertEqual(self.n, 1) |
---|
584 | n/a | |
---|
585 | n/a | # Turn off buffering and parse the next chunk. |
---|
586 | n/a | parser.buffer_text = 0 |
---|
587 | n/a | self.assertFalse(parser.buffer_text) |
---|
588 | n/a | self.assertEqual(parser.buffer_size, 1024) |
---|
589 | n/a | for i in range(10): |
---|
590 | n/a | parser.Parse(xml2, 0) |
---|
591 | n/a | self.assertEqual(self.n, 11) |
---|
592 | n/a | |
---|
593 | n/a | parser.buffer_text = 1 |
---|
594 | n/a | self.assertTrue(parser.buffer_text) |
---|
595 | n/a | self.assertEqual(parser.buffer_size, 1024) |
---|
596 | n/a | parser.Parse(xml3, 1) |
---|
597 | n/a | self.assertEqual(self.n, 12) |
---|
598 | n/a | |
---|
599 | n/a | def counting_handler(self, text): |
---|
600 | n/a | self.n += 1 |
---|
601 | n/a | |
---|
602 | n/a | def small_buffer_test(self, buffer_len): |
---|
603 | n/a | xml = b"<?xml version='1.0' encoding='iso8859'?><s>" + b'a' * buffer_len + b'</s>' |
---|
604 | n/a | parser = expat.ParserCreate() |
---|
605 | n/a | parser.CharacterDataHandler = self.counting_handler |
---|
606 | n/a | parser.buffer_size = 1024 |
---|
607 | n/a | parser.buffer_text = 1 |
---|
608 | n/a | |
---|
609 | n/a | self.n = 0 |
---|
610 | n/a | parser.Parse(xml) |
---|
611 | n/a | return self.n |
---|
612 | n/a | |
---|
613 | n/a | def test_change_size_1(self): |
---|
614 | n/a | xml1 = b"<?xml version='1.0' encoding='iso8859'?><a><s>" + b'a' * 1024 |
---|
615 | n/a | xml2 = b'aaa</s><s>' + b'a' * 1025 + b'</s></a>' |
---|
616 | n/a | parser = expat.ParserCreate() |
---|
617 | n/a | parser.CharacterDataHandler = self.counting_handler |
---|
618 | n/a | parser.buffer_text = 1 |
---|
619 | n/a | parser.buffer_size = 1024 |
---|
620 | n/a | self.assertEqual(parser.buffer_size, 1024) |
---|
621 | n/a | |
---|
622 | n/a | self.n = 0 |
---|
623 | n/a | parser.Parse(xml1, 0) |
---|
624 | n/a | parser.buffer_size *= 2 |
---|
625 | n/a | self.assertEqual(parser.buffer_size, 2048) |
---|
626 | n/a | parser.Parse(xml2, 1) |
---|
627 | n/a | self.assertEqual(self.n, 2) |
---|
628 | n/a | |
---|
629 | n/a | def test_change_size_2(self): |
---|
630 | n/a | xml1 = b"<?xml version='1.0' encoding='iso8859'?><a>a<s>" + b'a' * 1023 |
---|
631 | n/a | xml2 = b'aaa</s><s>' + b'a' * 1025 + b'</s></a>' |
---|
632 | n/a | parser = expat.ParserCreate() |
---|
633 | n/a | parser.CharacterDataHandler = self.counting_handler |
---|
634 | n/a | parser.buffer_text = 1 |
---|
635 | n/a | parser.buffer_size = 2048 |
---|
636 | n/a | self.assertEqual(parser.buffer_size, 2048) |
---|
637 | n/a | |
---|
638 | n/a | self.n=0 |
---|
639 | n/a | parser.Parse(xml1, 0) |
---|
640 | n/a | parser.buffer_size = parser.buffer_size // 2 |
---|
641 | n/a | self.assertEqual(parser.buffer_size, 1024) |
---|
642 | n/a | parser.Parse(xml2, 1) |
---|
643 | n/a | self.assertEqual(self.n, 4) |
---|
644 | n/a | |
---|
645 | n/a | class MalformedInputTest(unittest.TestCase): |
---|
646 | n/a | def test1(self): |
---|
647 | n/a | xml = b"\0\r\n" |
---|
648 | n/a | parser = expat.ParserCreate() |
---|
649 | n/a | try: |
---|
650 | n/a | parser.Parse(xml, True) |
---|
651 | n/a | self.fail() |
---|
652 | n/a | except expat.ExpatError as e: |
---|
653 | n/a | self.assertEqual(str(e), 'unclosed token: line 2, column 0') |
---|
654 | n/a | |
---|
655 | n/a | def test2(self): |
---|
656 | n/a | # \xc2\x85 is UTF-8 encoded U+0085 (NEXT LINE) |
---|
657 | n/a | xml = b"<?xml version\xc2\x85='1.0'?>\r\n" |
---|
658 | n/a | parser = expat.ParserCreate() |
---|
659 | n/a | err_pattern = r'XML declaration not well-formed: line 1, column \d+' |
---|
660 | n/a | with self.assertRaisesRegex(expat.ExpatError, err_pattern): |
---|
661 | n/a | parser.Parse(xml, True) |
---|
662 | n/a | |
---|
663 | n/a | class ErrorMessageTest(unittest.TestCase): |
---|
664 | n/a | def test_codes(self): |
---|
665 | n/a | # verify mapping of errors.codes and errors.messages |
---|
666 | n/a | self.assertEqual(errors.XML_ERROR_SYNTAX, |
---|
667 | n/a | errors.messages[errors.codes[errors.XML_ERROR_SYNTAX]]) |
---|
668 | n/a | |
---|
669 | n/a | def test_expaterror(self): |
---|
670 | n/a | xml = b'<' |
---|
671 | n/a | parser = expat.ParserCreate() |
---|
672 | n/a | try: |
---|
673 | n/a | parser.Parse(xml, True) |
---|
674 | n/a | self.fail() |
---|
675 | n/a | except expat.ExpatError as e: |
---|
676 | n/a | self.assertEqual(e.code, |
---|
677 | n/a | errors.codes[errors.XML_ERROR_UNCLOSED_TOKEN]) |
---|
678 | n/a | |
---|
679 | n/a | |
---|
680 | n/a | class ForeignDTDTests(unittest.TestCase): |
---|
681 | n/a | """ |
---|
682 | n/a | Tests for the UseForeignDTD method of expat parser objects. |
---|
683 | n/a | """ |
---|
684 | n/a | def test_use_foreign_dtd(self): |
---|
685 | n/a | """ |
---|
686 | n/a | If UseForeignDTD is passed True and a document without an external |
---|
687 | n/a | entity reference is parsed, ExternalEntityRefHandler is first called |
---|
688 | n/a | with None for the public and system ids. |
---|
689 | n/a | """ |
---|
690 | n/a | handler_call_args = [] |
---|
691 | n/a | def resolve_entity(context, base, system_id, public_id): |
---|
692 | n/a | handler_call_args.append((public_id, system_id)) |
---|
693 | n/a | return 1 |
---|
694 | n/a | |
---|
695 | n/a | parser = expat.ParserCreate() |
---|
696 | n/a | parser.UseForeignDTD(True) |
---|
697 | n/a | parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS) |
---|
698 | n/a | parser.ExternalEntityRefHandler = resolve_entity |
---|
699 | n/a | parser.Parse(b"<?xml version='1.0'?><element/>") |
---|
700 | n/a | self.assertEqual(handler_call_args, [(None, None)]) |
---|
701 | n/a | |
---|
702 | n/a | # test UseForeignDTD() is equal to UseForeignDTD(True) |
---|
703 | n/a | handler_call_args[:] = [] |
---|
704 | n/a | |
---|
705 | n/a | parser = expat.ParserCreate() |
---|
706 | n/a | parser.UseForeignDTD() |
---|
707 | n/a | parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS) |
---|
708 | n/a | parser.ExternalEntityRefHandler = resolve_entity |
---|
709 | n/a | parser.Parse(b"<?xml version='1.0'?><element/>") |
---|
710 | n/a | self.assertEqual(handler_call_args, [(None, None)]) |
---|
711 | n/a | |
---|
712 | n/a | def test_ignore_use_foreign_dtd(self): |
---|
713 | n/a | """ |
---|
714 | n/a | If UseForeignDTD is passed True and a document with an external |
---|
715 | n/a | entity reference is parsed, ExternalEntityRefHandler is called with |
---|
716 | n/a | the public and system ids from the document. |
---|
717 | n/a | """ |
---|
718 | n/a | handler_call_args = [] |
---|
719 | n/a | def resolve_entity(context, base, system_id, public_id): |
---|
720 | n/a | handler_call_args.append((public_id, system_id)) |
---|
721 | n/a | return 1 |
---|
722 | n/a | |
---|
723 | n/a | parser = expat.ParserCreate() |
---|
724 | n/a | parser.UseForeignDTD(True) |
---|
725 | n/a | parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS) |
---|
726 | n/a | parser.ExternalEntityRefHandler = resolve_entity |
---|
727 | n/a | parser.Parse( |
---|
728 | n/a | b"<?xml version='1.0'?><!DOCTYPE foo PUBLIC 'bar' 'baz'><element/>") |
---|
729 | n/a | self.assertEqual(handler_call_args, [("bar", "baz")]) |
---|
730 | n/a | |
---|
731 | n/a | |
---|
732 | n/a | if __name__ == "__main__": |
---|
733 | n/a | unittest.main() |
---|