Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(83975)

Delta Between Two Patch Sets: Lib/test/test_pyexpat.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 11 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_pulldom.py ('k') | Lib/test/test_re.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # XXX TypeErrors on calling handlers, or on bad return values from a 1 # XXX TypeErrors on calling handlers, or on bad return values from a
2 # handler, are obscure and unhelpful. 2 # handler, are obscure and unhelpful.
3 3
4 from io import BytesIO 4 from io import BytesIO
5 import unittest 5 import unittest
6 6
7 from xml.parsers import expat 7 from xml.parsers import expat
8 from xml.parsers.expat import errors 8 from xml.parsers.expat import errors
9 9
10 from test.support import sortdict, run_unittest 10 from test.support import sortdict, run_unittest
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 %unparsed_entity; 45 %unparsed_entity;
46 ]> 46 ]>
47 47
48 <root attr1="value1" attr2="value2&#8000;"> 48 <root attr1="value1" attr2="value2&#8000;">
49 <myns:subelement xmlns:myns="http://www.python.org/namespace"> 49 <myns:subelement xmlns:myns="http://www.python.org/namespace">
50 Contents of subelements 50 Contents of subelements
51 </myns:subelement> 51 </myns:subelement>
52 <sub2><![CDATA[contents of CDATA section]]></sub2> 52 <sub2><![CDATA[contents of CDATA section]]></sub2>
53 &external_entity; 53 &external_entity;
54 &skipped_entity; 54 &skipped_entity;
55 \xb5
55 </root> 56 </root>
56 ''' 57 '''
57 58
58 59
59 # Produce UTF-8 output 60 # Produce UTF-8 output
60 class ParseTest(unittest.TestCase): 61 class ParseTest(unittest.TestCase):
61 class Outputter: 62 class Outputter:
62 def __init__(self): 63 def __init__(self):
63 self.out = [] 64 self.out = []
64 65
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 "Character data: 'Contents of subelements'", 189 "Character data: 'Contents of subelements'",
189 "End element: 'http://www.python.org/namespace!subelement'", 190 "End element: 'http://www.python.org/namespace!subelement'",
190 "End of NS decl: 'myns'", 191 "End of NS decl: 'myns'",
191 "Start element: 'sub2' {}", 192 "Start element: 'sub2' {}",
192 'Start of CDATA section', 193 'Start of CDATA section',
193 "Character data: 'contents of CDATA section'", 194 "Character data: 'contents of CDATA section'",
194 'End of CDATA section', 195 'End of CDATA section',
195 "End element: 'sub2'", 196 "End element: 'sub2'",
196 "External entity ref: (None, 'entity.file', None)", 197 "External entity ref: (None, 'entity.file', None)",
197 ('Skipped entity', ('skipped_entity', 0)), 198 ('Skipped entity', ('skipped_entity', 0)),
199 "Character data: '\xb5'",
198 "End element: 'root'", 200 "End element: 'root'",
199 ] 201 ]
200 for operation, expected_operation in zip(operations, expected_operations ): 202 for operation, expected_operation in zip(operations, expected_operations ):
201 self.assertEqual(operation, expected_operation) 203 self.assertEqual(operation, expected_operation)
202 204
203 def test_unicode(self): 205 def test_parse_bytes(self):
204 # Try the parse again, this time producing Unicode output
205 out = self.Outputter() 206 out = self.Outputter()
206 parser = expat.ParserCreate(namespace_separator='!') 207 parser = expat.ParserCreate(namespace_separator='!')
207 self._hookup_callbacks(parser, out) 208 self._hookup_callbacks(parser, out)
208 209
209 parser.Parse(data, 1) 210 parser.Parse(data, 1)
210 211
211 operations = out.out 212 operations = out.out
212 self._verify_parse_output(operations) 213 self._verify_parse_output(operations)
213 # Issue #6697. 214 # Issue #6697.
214 self.assertRaises(AttributeError, getattr, parser, '\uD800') 215 self.assertRaises(AttributeError, getattr, parser, '\uD800')
216
217 def test_parse_str(self):
218 out = self.Outputter()
219 parser = expat.ParserCreate(namespace_separator='!')
220 self._hookup_callbacks(parser, out)
221
222 parser.Parse(data.decode('iso-8859-1'), 1)
223
224 operations = out.out
225 self._verify_parse_output(operations)
215 226
216 def test_parse_file(self): 227 def test_parse_file(self):
217 # Try parsing a file 228 # Try parsing a file
218 out = self.Outputter() 229 out = self.Outputter()
219 parser = expat.ParserCreate(namespace_separator='!') 230 parser = expat.ParserCreate(namespace_separator='!')
220 self._hookup_callbacks(parser, out) 231 self._hookup_callbacks(parser, out)
221 file = BytesIO(data) 232 file = BytesIO(data)
222 233
223 parser.ParseFile(file) 234 parser.ParseFile(file)
224 235
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 273
263 class InterningTest(unittest.TestCase): 274 class InterningTest(unittest.TestCase):
264 def test(self): 275 def test(self):
265 # Test the interning machinery. 276 # Test the interning machinery.
266 p = expat.ParserCreate() 277 p = expat.ParserCreate()
267 L = [] 278 L = []
268 def collector(name, *args): 279 def collector(name, *args):
269 L.append(name) 280 L.append(name)
270 p.StartElementHandler = collector 281 p.StartElementHandler = collector
271 p.EndElementHandler = collector 282 p.EndElementHandler = collector
272 p.Parse("<e> <e/> <e></e> </e>", 1) 283 p.Parse(b"<e> <e/> <e></e> </e>", 1)
273 tag = L[0] 284 tag = L[0]
274 self.assertEqual(len(L), 6) 285 self.assertEqual(len(L), 6)
275 for entry in L: 286 for entry in L:
276 # L should have the same string repeated over and over. 287 # L should have the same string repeated over and over.
277 self.assertIs(tag, entry) 288 self.assertIs(tag, entry)
278 289
279 def test_issue9402(self): 290 def test_issue9402(self):
280 # create an ExternalEntityParserCreate with buffer text 291 # create an ExternalEntityParserCreate with buffer text
281 class ExternalOutputter: 292 class ExternalOutputter:
282 def __init__(self, parser): 293 def __init__(self, parser):
283 self.parser = parser 294 self.parser = parser
284 self.parser_result = None 295 self.parser_result = None
285 296
286 def ExternalEntityRefHandler(self, context, base, sysId, pubId): 297 def ExternalEntityRefHandler(self, context, base, sysId, pubId):
287 external_parser = self.parser.ExternalEntityParserCreate("") 298 external_parser = self.parser.ExternalEntityParserCreate("")
288 self.parser_result = external_parser.Parse("", 1) 299 self.parser_result = external_parser.Parse(b"", 1)
289 return 1 300 return 1
290 301
291 parser = expat.ParserCreate(namespace_separator='!') 302 parser = expat.ParserCreate(namespace_separator='!')
292 parser.buffer_text = 1 303 parser.buffer_text = 1
293 out = ExternalOutputter(parser) 304 out = ExternalOutputter(parser)
294 parser.ExternalEntityRefHandler = out.ExternalEntityRefHandler 305 parser.ExternalEntityRefHandler = out.ExternalEntityRefHandler
295 parser.Parse(data, 1) 306 parser.Parse(data, 1)
296 self.assertEqual(out.parser_result, 1) 307 self.assertEqual(out.parser_result, 1)
297 308
298 309
(...skipping 30 matching lines...) Expand all
329 for name in handlers: 340 for name in handlers:
330 setattr(self.parser, name, getattr(self, name)) 341 setattr(self.parser, name, getattr(self, name))
331 342
332 def test_default_to_disabled(self): 343 def test_default_to_disabled(self):
333 parser = expat.ParserCreate() 344 parser = expat.ParserCreate()
334 self.assertFalse(parser.buffer_text) 345 self.assertFalse(parser.buffer_text)
335 346
336 def test_buffering_enabled(self): 347 def test_buffering_enabled(self):
337 # Make sure buffering is turned on 348 # Make sure buffering is turned on
338 self.assertTrue(self.parser.buffer_text) 349 self.assertTrue(self.parser.buffer_text)
339 self.parser.Parse("<a>1<b/>2<c/>3</a>", 1) 350 self.parser.Parse(b"<a>1<b/>2<c/>3</a>", 1)
340 self.assertEqual(self.stuff, ['123'], 351 self.assertEqual(self.stuff, ['123'],
341 "buffered text not properly collapsed") 352 "buffered text not properly collapsed")
342 353
343 def test1(self): 354 def test1(self):
344 # XXX This test exposes more detail of Expat's text chunking than we 355 # XXX This test exposes more detail of Expat's text chunking than we
345 # XXX like, but it tests what we need to concisely. 356 # XXX like, but it tests what we need to concisely.
346 self.setHandlers(["StartElementHandler"]) 357 self.setHandlers(["StartElementHandler"])
347 self.parser.Parse("<a>1<b buffer-text='no'/>2\n3<c buffer-text='yes'/>4\ n5</a>", 1) 358 self.parser.Parse(b"<a>1<b buffer-text='no'/>2\n3<c buffer-text='yes'/>4 \n5</a>", 1)
348 self.assertEqual(self.stuff, 359 self.assertEqual(self.stuff,
349 ["<a>", "1", "<b>", "2", "\n", "3", "<c>", "4\n5"], 360 ["<a>", "1", "<b>", "2", "\n", "3", "<c>", "4\n5"],
350 "buffering control not reacting as expected") 361 "buffering control not reacting as expected")
351 362
352 def test2(self): 363 def test2(self):
353 self.parser.Parse("<a>1<b/>&lt;2&gt;<c/>&#32;\n&#x20;3</a>", 1) 364 self.parser.Parse(b"<a>1<b/>&lt;2&gt;<c/>&#32;\n&#x20;3</a>", 1)
354 self.assertEqual(self.stuff, ["1<2> \n 3"], 365 self.assertEqual(self.stuff, ["1<2> \n 3"],
355 "buffered text not properly collapsed") 366 "buffered text not properly collapsed")
356 367
357 def test3(self): 368 def test3(self):
358 self.setHandlers(["StartElementHandler"]) 369 self.setHandlers(["StartElementHandler"])
359 self.parser.Parse("<a>1<b/>2<c/>3</a>", 1) 370 self.parser.Parse(b"<a>1<b/>2<c/>3</a>", 1)
360 self.assertEqual(self.stuff, ["<a>", "1", "<b>", "2", "<c>", "3"], 371 self.assertEqual(self.stuff, ["<a>", "1", "<b>", "2", "<c>", "3"],
361 "buffered text not properly split") 372 "buffered text not properly split")
362 373
363 def test4(self): 374 def test4(self):
364 self.setHandlers(["StartElementHandler", "EndElementHandler"]) 375 self.setHandlers(["StartElementHandler", "EndElementHandler"])
365 self.parser.CharacterDataHandler = None 376 self.parser.CharacterDataHandler = None
366 self.parser.Parse("<a>1<b/>2<c/>3</a>", 1) 377 self.parser.Parse(b"<a>1<b/>2<c/>3</a>", 1)
367 self.assertEqual(self.stuff, 378 self.assertEqual(self.stuff,
368 ["<a>", "<b>", "</b>", "<c>", "</c>", "</a>"]) 379 ["<a>", "<b>", "</b>", "<c>", "</c>", "</a>"])
369 380
370 def test5(self): 381 def test5(self):
371 self.setHandlers(["StartElementHandler", "EndElementHandler"]) 382 self.setHandlers(["StartElementHandler", "EndElementHandler"])
372 self.parser.Parse("<a>1<b></b>2<c/>3</a>", 1) 383 self.parser.Parse(b"<a>1<b></b>2<c/>3</a>", 1)
373 self.assertEqual(self.stuff, 384 self.assertEqual(self.stuff,
374 ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", "</a>"]) 385 ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", "</a>"])
375 386
376 def test6(self): 387 def test6(self):
377 self.setHandlers(["CommentHandler", "EndElementHandler", 388 self.setHandlers(["CommentHandler", "EndElementHandler",
378 "StartElementHandler"]) 389 "StartElementHandler"])
379 self.parser.Parse("<a>1<b/>2<c></c>345</a> ", 1) 390 self.parser.Parse(b"<a>1<b/>2<c></c>345</a> ", 1)
380 self.assertEqual(self.stuff, 391 self.assertEqual(self.stuff,
381 ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "345", "</a>"], 392 ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "345", "</a>"],
382 "buffered text not properly split") 393 "buffered text not properly split")
383 394
384 def test7(self): 395 def test7(self):
385 self.setHandlers(["CommentHandler", "EndElementHandler", 396 self.setHandlers(["CommentHandler", "EndElementHandler",
386 "StartElementHandler"]) 397 "StartElementHandler"])
387 self.parser.Parse("<a>1<b/>2<c></c>3<!--abc-->4<!--def-->5</a> ", 1) 398 self.parser.Parse(b"<a>1<b/>2<c></c>3<!--abc-->4<!--def-->5</a> ", 1)
388 self.assertEqual(self.stuff, 399 self.assertEqual(self.stuff,
389 ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", 400 ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3",
390 "<!--abc-->", "4", "<!--def-->", "5", "</a>"], 401 "<!--abc-->", "4", "<!--def-->", "5", "</a>"],
391 "buffered text not properly split") 402 "buffered text not properly split")
392 403
393 404
394 # Test handling of exception from callback: 405 # Test handling of exception from callback:
395 class HandlerExceptionTest(unittest.TestCase): 406 class HandlerExceptionTest(unittest.TestCase):
396 def StartElementHandler(self, name, attrs): 407 def StartElementHandler(self, name, attrs):
397 raise RuntimeError(name) 408 raise RuntimeError(name)
398 409
399 def test(self): 410 def test(self):
400 parser = expat.ParserCreate() 411 parser = expat.ParserCreate()
401 parser.StartElementHandler = self.StartElementHandler 412 parser.StartElementHandler = self.StartElementHandler
402 try: 413 try:
403 parser.Parse("<a><b><c/></b></a>", 1) 414 parser.Parse(b"<a><b><c/></b></a>", 1)
404 self.fail() 415 self.fail()
405 except RuntimeError as e: 416 except RuntimeError as e:
406 self.assertEqual(e.args[0], 'a', 417 self.assertEqual(e.args[0], 'a',
407 "Expected RuntimeError for element 'a', but" + \ 418 "Expected RuntimeError for element 'a', but" + \
408 " found %r" % e.args[0]) 419 " found %r" % e.args[0])
409 420
410 421
411 # Test Current* members: 422 # Test Current* members:
412 class PositionTest(unittest.TestCase): 423 class PositionTest(unittest.TestCase):
413 def StartElementHandler(self, name, attrs): 424 def StartElementHandler(self, name, attrs):
(...skipping 15 matching lines...) Expand all
429 self.upto += 1 440 self.upto += 1
430 441
431 def test(self): 442 def test(self):
432 self.parser = expat.ParserCreate() 443 self.parser = expat.ParserCreate()
433 self.parser.StartElementHandler = self.StartElementHandler 444 self.parser.StartElementHandler = self.StartElementHandler
434 self.parser.EndElementHandler = self.EndElementHandler 445 self.parser.EndElementHandler = self.EndElementHandler
435 self.upto = 0 446 self.upto = 0
436 self.expected_list = [('s', 0, 1, 0), ('s', 5, 2, 1), ('s', 11, 3, 2), 447 self.expected_list = [('s', 0, 1, 0), ('s', 5, 2, 1), ('s', 11, 3, 2),
437 ('e', 15, 3, 6), ('e', 17, 4, 1), ('e', 22, 5, 0)] 448 ('e', 15, 3, 6), ('e', 17, 4, 1), ('e', 22, 5, 0)]
438 449
439 xml = '<a>\n <b>\n <c/>\n </b>\n</a>' 450 xml = b'<a>\n <b>\n <c/>\n </b>\n</a>'
440 self.parser.Parse(xml, 1) 451 self.parser.Parse(xml, 1)
441 452
442 453
443 class sf1296433Test(unittest.TestCase): 454 class sf1296433Test(unittest.TestCase):
444 def test_parse_only_xml_data(self): 455 def test_parse_only_xml_data(self):
445 # http://python.org/sf/1296433 456 # http://python.org/sf/1296433
446 # 457 #
447 xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * 1025) 458 xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * 1025)
448 # this one doesn't crash 459 # this one doesn't crash
449 #xml = "<?xml version='1.0'?><s>%s</s>" % ('a' * 10000) 460 #xml = "<?xml version='1.0'?><s>%s</s>" % ('a' * 10000)
450 461
451 class SpecificException(Exception): 462 class SpecificException(Exception):
452 pass 463 pass
453 464
454 def handler(text): 465 def handler(text):
455 raise SpecificException 466 raise SpecificException
456 467
457 parser = expat.ParserCreate() 468 parser = expat.ParserCreate()
458 parser.CharacterDataHandler = handler 469 parser.CharacterDataHandler = handler
459 470
460 self.assertRaises(Exception, parser.Parse, xml) 471 self.assertRaises(Exception, parser.Parse, xml.encode('iso8859'))
461 472
462 class ChardataBufferTest(unittest.TestCase): 473 class ChardataBufferTest(unittest.TestCase):
463 """ 474 """
464 test setting of chardata buffer size 475 test setting of chardata buffer size
465 """ 476 """
466 477
467 def test_1025_bytes(self): 478 def test_1025_bytes(self):
468 self.assertEqual(self.small_buffer_test(1025), 2) 479 self.assertEqual(self.small_buffer_test(1025), 2)
469 480
470 def test_1000_bytes(self): 481 def test_1000_bytes(self):
471 self.assertEqual(self.small_buffer_test(1000), 1) 482 self.assertEqual(self.small_buffer_test(1000), 1)
472 483
473 def test_wrong_size(self): 484 def test_wrong_size(self):
474 parser = expat.ParserCreate() 485 parser = expat.ParserCreate()
475 parser.buffer_text = 1 486 parser.buffer_text = 1
476 def f(size): 487 def f(size):
477 parser.buffer_size = size 488 parser.buffer_size = size
478 489
479 self.assertRaises(ValueError, f, -1) 490 self.assertRaises(ValueError, f, -1)
480 self.assertRaises(ValueError, f, 0) 491 self.assertRaises(ValueError, f, 0)
481 492
482 def test_unchanged_size(self): 493 def test_unchanged_size(self):
483 xml1 = ("<?xml version='1.0' encoding='iso8859'?><s>%s" % ('a' * 512)) 494 xml1 = b"<?xml version='1.0' encoding='iso8859'?><s>" + b'a' * 512
484 xml2 = 'a'*512 + '</s>' 495 xml2 = b'a'*512 + b'</s>'
485 parser = expat.ParserCreate() 496 parser = expat.ParserCreate()
486 parser.CharacterDataHandler = self.counting_handler 497 parser.CharacterDataHandler = self.counting_handler
487 parser.buffer_size = 512 498 parser.buffer_size = 512
488 parser.buffer_text = 1 499 parser.buffer_text = 1
489 500
490 # Feed 512 bytes of character data: the handler should be called 501 # Feed 512 bytes of character data: the handler should be called
491 # once. 502 # once.
492 self.n = 0 503 self.n = 0
493 parser.Parse(xml1) 504 parser.Parse(xml1)
494 self.assertEqual(self.n, 1) 505 self.assertEqual(self.n, 1)
495 506
496 # Reassign to buffer_size, but assign the same size. 507 # Reassign to buffer_size, but assign the same size.
497 parser.buffer_size = parser.buffer_size 508 parser.buffer_size = parser.buffer_size
498 self.assertEqual(self.n, 1) 509 self.assertEqual(self.n, 1)
499 510
500 # Try parsing rest of the document 511 # Try parsing rest of the document
501 parser.Parse(xml2) 512 parser.Parse(xml2)
502 self.assertEqual(self.n, 2) 513 self.assertEqual(self.n, 2)
503 514
504 515
505 def test_disabling_buffer(self): 516 def test_disabling_buffer(self):
506 xml1 = "<?xml version='1.0' encoding='iso8859'?><a>%s" % ('a' * 512) 517 xml1 = b"<?xml version='1.0' encoding='iso8859'?><a>" + b'a' * 512
507 xml2 = ('b' * 1024) 518 xml2 = b'b' * 1024
508 xml3 = "%s</a>" % ('c' * 1024) 519 xml3 = b'c' * 1024 + b'</a>';
509 parser = expat.ParserCreate() 520 parser = expat.ParserCreate()
510 parser.CharacterDataHandler = self.counting_handler 521 parser.CharacterDataHandler = self.counting_handler
511 parser.buffer_text = 1 522 parser.buffer_text = 1
512 parser.buffer_size = 1024 523 parser.buffer_size = 1024
513 self.assertEqual(parser.buffer_size, 1024) 524 self.assertEqual(parser.buffer_size, 1024)
514 525
515 # Parse one chunk of XML 526 # Parse one chunk of XML
516 self.n = 0 527 self.n = 0
517 parser.Parse(xml1, 0) 528 parser.Parse(xml1, 0)
518 self.assertEqual(parser.buffer_size, 1024) 529 self.assertEqual(parser.buffer_size, 1024)
519 self.assertEqual(self.n, 1) 530 self.assertEqual(self.n, 1)
520 531
521 # Turn off buffering and parse the next chunk. 532 # Turn off buffering and parse the next chunk.
522 parser.buffer_text = 0 533 parser.buffer_text = 0
523 self.assertFalse(parser.buffer_text) 534 self.assertFalse(parser.buffer_text)
524 self.assertEqual(parser.buffer_size, 1024) 535 self.assertEqual(parser.buffer_size, 1024)
525 for i in range(10): 536 for i in range(10):
526 parser.Parse(xml2, 0) 537 parser.Parse(xml2, 0)
527 self.assertEqual(self.n, 11) 538 self.assertEqual(self.n, 11)
528 539
529 parser.buffer_text = 1 540 parser.buffer_text = 1
530 self.assertTrue(parser.buffer_text) 541 self.assertTrue(parser.buffer_text)
531 self.assertEqual(parser.buffer_size, 1024) 542 self.assertEqual(parser.buffer_size, 1024)
532 parser.Parse(xml3, 1) 543 parser.Parse(xml3, 1)
533 self.assertEqual(self.n, 12) 544 self.assertEqual(self.n, 12)
534 545
535
536
537 def make_document(self, bytes):
538 return ("<?xml version='1.0'?><tag>" + bytes * 'a' + '</tag>')
539
540 def counting_handler(self, text): 546 def counting_handler(self, text):
541 self.n += 1 547 self.n += 1
542 548
543 def small_buffer_test(self, buffer_len): 549 def small_buffer_test(self, buffer_len):
544 xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * buffe r_len) 550 xml = b"<?xml version='1.0' encoding='iso8859'?><s>" + b'a' * buffer_len + b'</s>'
545 parser = expat.ParserCreate() 551 parser = expat.ParserCreate()
546 parser.CharacterDataHandler = self.counting_handler 552 parser.CharacterDataHandler = self.counting_handler
547 parser.buffer_size = 1024 553 parser.buffer_size = 1024
548 parser.buffer_text = 1 554 parser.buffer_text = 1
549 555
550 self.n = 0 556 self.n = 0
551 parser.Parse(xml) 557 parser.Parse(xml)
552 return self.n 558 return self.n
553 559
554 def test_change_size_1(self): 560 def test_change_size_1(self):
555 xml1 = "<?xml version='1.0' encoding='iso8859'?><a><s>%s" % ('a' * 1024) 561 xml1 = b"<?xml version='1.0' encoding='iso8859'?><a><s>" + b'a' * 1024
556 xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025) 562 xml2 = b'aaa</s><s>' + b'a' * 1025 + b'</s></a>'
557 parser = expat.ParserCreate() 563 parser = expat.ParserCreate()
558 parser.CharacterDataHandler = self.counting_handler 564 parser.CharacterDataHandler = self.counting_handler
559 parser.buffer_text = 1 565 parser.buffer_text = 1
560 parser.buffer_size = 1024 566 parser.buffer_size = 1024
561 self.assertEqual(parser.buffer_size, 1024) 567 self.assertEqual(parser.buffer_size, 1024)
562 568
563 self.n = 0 569 self.n = 0
564 parser.Parse(xml1, 0) 570 parser.Parse(xml1, 0)
565 parser.buffer_size *= 2 571 parser.buffer_size *= 2
566 self.assertEqual(parser.buffer_size, 2048) 572 self.assertEqual(parser.buffer_size, 2048)
567 parser.Parse(xml2, 1) 573 parser.Parse(xml2, 1)
568 self.assertEqual(self.n, 2) 574 self.assertEqual(self.n, 2)
569 575
570 def test_change_size_2(self): 576 def test_change_size_2(self):
571 xml1 = "<?xml version='1.0' encoding='iso8859'?><a>a<s>%s" % ('a' * 1023 ) 577 xml1 = b"<?xml version='1.0' encoding='iso8859'?><a>a<s>" + b'a' * 1023
572 xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025) 578 xml2 = b'aaa</s><s>' + b'a' * 1025 + b'</s></a>'
573 parser = expat.ParserCreate() 579 parser = expat.ParserCreate()
574 parser.CharacterDataHandler = self.counting_handler 580 parser.CharacterDataHandler = self.counting_handler
575 parser.buffer_text = 1 581 parser.buffer_text = 1
576 parser.buffer_size = 2048 582 parser.buffer_size = 2048
577 self.assertEqual(parser.buffer_size, 2048) 583 self.assertEqual(parser.buffer_size, 2048)
578 584
579 self.n=0 585 self.n=0
580 parser.Parse(xml1, 0) 586 parser.Parse(xml1, 0)
581 parser.buffer_size = parser.buffer_size // 2 587 parser.buffer_size = parser.buffer_size // 2
582 self.assertEqual(parser.buffer_size, 1024) 588 self.assertEqual(parser.buffer_size, 1024)
583 parser.Parse(xml2, 1) 589 parser.Parse(xml2, 1)
584 self.assertEqual(self.n, 4) 590 self.assertEqual(self.n, 4)
585 591
586 class MalformedInputTest(unittest.TestCase): 592 class MalformedInputTest(unittest.TestCase):
587 def test1(self): 593 def test1(self):
588 xml = "\0\r\n" 594 xml = b"\0\r\n"
589 parser = expat.ParserCreate() 595 parser = expat.ParserCreate()
590 try: 596 try:
591 parser.Parse(xml, True) 597 parser.Parse(xml, True)
592 self.fail() 598 self.fail()
593 except expat.ExpatError as e: 599 except expat.ExpatError as e:
594 self.assertEqual(str(e), 'unclosed token: line 2, column 0') 600 self.assertEqual(str(e), 'unclosed token: line 2, column 0')
595 601
596 def test2(self): 602 def test2(self):
597 xml = "<?xml version\xc2\x85='1.0'?>\r\n" 603 # \xc2\x85 is UTF-8 encoded U+0085 (NEXT LINE)
604 xml = b"<?xml version\xc2\x85='1.0'?>\r\n"
598 parser = expat.ParserCreate() 605 parser = expat.ParserCreate()
599 try: 606 try:
600 parser.Parse(xml, True) 607 parser.Parse(xml, True)
601 self.fail() 608 self.fail()
602 except expat.ExpatError as e: 609 except expat.ExpatError as e:
603 self.assertEqual(str(e), 'XML declaration not well-formed: line 1, c olumn 14') 610 self.assertEqual(str(e), 'XML declaration not well-formed: line 1, c olumn 14')
604 611
605 class ErrorMessageTest(unittest.TestCase): 612 class ErrorMessageTest(unittest.TestCase):
606 def test_codes(self): 613 def test_codes(self):
607 # verify mapping of errors.codes and errors.messages 614 # verify mapping of errors.codes and errors.messages
608 self.assertEqual(errors.XML_ERROR_SYNTAX, 615 self.assertEqual(errors.XML_ERROR_SYNTAX,
609 errors.messages[errors.codes[errors.XML_ERROR_SYNTAX]]) 616 errors.messages[errors.codes[errors.XML_ERROR_SYNTAX]])
610 617
611 def test_expaterror(self): 618 def test_expaterror(self):
612 xml = '<' 619 xml = b'<'
613 parser = expat.ParserCreate() 620 parser = expat.ParserCreate()
614 try: 621 try:
615 parser.Parse(xml, True) 622 parser.Parse(xml, True)
616 self.fail() 623 self.fail()
617 except expat.ExpatError as e: 624 except expat.ExpatError as e:
618 self.assertEqual(e.code, 625 self.assertEqual(e.code,
619 errors.codes[errors.XML_ERROR_UNCLOSED_TOKEN]) 626 errors.codes[errors.XML_ERROR_UNCLOSED_TOKEN])
620 627
621 628
622 class ForeignDTDTests(unittest.TestCase): 629 class ForeignDTDTests(unittest.TestCase):
623 """ 630 """
624 Tests for the UseForeignDTD method of expat parser objects. 631 Tests for the UseForeignDTD method of expat parser objects.
625 """ 632 """
626 def test_use_foreign_dtd(self): 633 def test_use_foreign_dtd(self):
627 """ 634 """
628 If UseForeignDTD is passed True and a document without an external 635 If UseForeignDTD is passed True and a document without an external
629 entity reference is parsed, ExternalEntityRefHandler is first called 636 entity reference is parsed, ExternalEntityRefHandler is first called
630 with None for the public and system ids. 637 with None for the public and system ids.
631 """ 638 """
632 handler_call_args = [] 639 handler_call_args = []
633 def resolve_entity(context, base, system_id, public_id): 640 def resolve_entity(context, base, system_id, public_id):
634 handler_call_args.append((public_id, system_id)) 641 handler_call_args.append((public_id, system_id))
635 return 1 642 return 1
636 643
637 parser = expat.ParserCreate() 644 parser = expat.ParserCreate()
638 parser.UseForeignDTD(True) 645 parser.UseForeignDTD(True)
639 parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS) 646 parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS)
640 parser.ExternalEntityRefHandler = resolve_entity 647 parser.ExternalEntityRefHandler = resolve_entity
641 parser.Parse("<?xml version='1.0'?><element/>") 648 parser.Parse(b"<?xml version='1.0'?><element/>")
642 self.assertEqual(handler_call_args, [(None, None)]) 649 self.assertEqual(handler_call_args, [(None, None)])
643 650
644 # test UseForeignDTD() is equal to UseForeignDTD(True) 651 # test UseForeignDTD() is equal to UseForeignDTD(True)
645 handler_call_args[:] = [] 652 handler_call_args[:] = []
646 653
647 parser = expat.ParserCreate() 654 parser = expat.ParserCreate()
648 parser.UseForeignDTD() 655 parser.UseForeignDTD()
649 parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS) 656 parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS)
650 parser.ExternalEntityRefHandler = resolve_entity 657 parser.ExternalEntityRefHandler = resolve_entity
651 parser.Parse("<?xml version='1.0'?><element/>") 658 parser.Parse(b"<?xml version='1.0'?><element/>")
652 self.assertEqual(handler_call_args, [(None, None)]) 659 self.assertEqual(handler_call_args, [(None, None)])
653 660
654 def test_ignore_use_foreign_dtd(self): 661 def test_ignore_use_foreign_dtd(self):
655 """ 662 """
656 If UseForeignDTD is passed True and a document with an external 663 If UseForeignDTD is passed True and a document with an external
657 entity reference is parsed, ExternalEntityRefHandler is called with 664 entity reference is parsed, ExternalEntityRefHandler is called with
658 the public and system ids from the document. 665 the public and system ids from the document.
659 """ 666 """
660 handler_call_args = [] 667 handler_call_args = []
661 def resolve_entity(context, base, system_id, public_id): 668 def resolve_entity(context, base, system_id, public_id):
662 handler_call_args.append((public_id, system_id)) 669 handler_call_args.append((public_id, system_id))
663 return 1 670 return 1
664 671
665 parser = expat.ParserCreate() 672 parser = expat.ParserCreate()
666 parser.UseForeignDTD(True) 673 parser.UseForeignDTD(True)
667 parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS) 674 parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS)
668 parser.ExternalEntityRefHandler = resolve_entity 675 parser.ExternalEntityRefHandler = resolve_entity
669 parser.Parse( 676 parser.Parse(
670 "<?xml version='1.0'?><!DOCTYPE foo PUBLIC 'bar' 'baz'><element/>") 677 b"<?xml version='1.0'?><!DOCTYPE foo PUBLIC 'bar' 'baz'><element/>")
671 self.assertEqual(handler_call_args, [("bar", "baz")]) 678 self.assertEqual(handler_call_args, [("bar", "baz")])
672 679
673 680
674 def test_main(): 681 def test_main():
675 run_unittest(SetAttributeTest, 682 run_unittest(SetAttributeTest,
676 ParseTest, 683 ParseTest,
677 NamespaceSeparatorTest, 684 NamespaceSeparatorTest,
678 InterningTest, 685 InterningTest,
679 BufferTextTest, 686 BufferTextTest,
680 HandlerExceptionTest, 687 HandlerExceptionTest,
681 PositionTest, 688 PositionTest,
682 sf1296433Test, 689 sf1296433Test,
683 ChardataBufferTest, 690 ChardataBufferTest,
684 MalformedInputTest, 691 MalformedInputTest,
685 ErrorMessageTest, 692 ErrorMessageTest,
686 ForeignDTDTests) 693 ForeignDTDTests)
687 694
688 if __name__ == "__main__": 695 if __name__ == "__main__":
689 test_main() 696 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+