|
9 | 9 | from io import BytesIO |
10 | 10 | from test import support |
11 | 11 | from test.support import os_helper |
12 | | - |
| 12 | +from test.support import sortdict |
| 13 | +from unittest import mock |
13 | 14 | from xml.parsers import expat |
14 | 15 | from xml.parsers.expat import errors |
15 | 16 |
|
16 | | -from test.support import sortdict |
17 | | - |
18 | 17 |
|
19 | 18 | class SetAttributeTest(unittest.TestCase): |
20 | 19 | def setUp(self): |
@@ -436,6 +435,19 @@ def test7(self): |
436 | 435 | "<!--abc-->", "4", "<!--def-->", "5", "</a>"], |
437 | 436 | "buffered text not properly split") |
438 | 437 |
|
| 438 | + def test_change_character_data_handler_in_callback(self): |
| 439 | + # Test that xmlparse_handler_setter() properly handles |
| 440 | + # the special case "parser.CharacterDataHandler = None". |
| 441 | + def handler(*args): |
| 442 | + parser.CharacterDataHandler = None |
| 443 | + |
| 444 | + handler_wrapper = mock.Mock(wraps=handler) |
| 445 | + parser = expat.ParserCreate() |
| 446 | + parser.CharacterDataHandler = handler_wrapper |
| 447 | + parser.Parse(b"<a>1<b/>2<c></c>3<!--abc-->4<!--def-->5</a> ", True) |
| 448 | + handler_wrapper.assert_called_once() |
| 449 | + self.assertIsNone(parser.CharacterDataHandler) |
| 450 | + |
439 | 451 |
|
440 | 452 | # Test handling of exception from callback: |
441 | 453 | class HandlerExceptionTest(unittest.TestCase): |
@@ -595,7 +607,7 @@ def test_unchanged_size(self): |
595 | 607 | def test_disabling_buffer(self): |
596 | 608 | xml1 = b"<?xml version='1.0' encoding='iso8859'?><a>" + b'a' * 512 |
597 | 609 | xml2 = b'b' * 1024 |
598 | | - xml3 = b'c' * 1024 + b'</a>'; |
| 610 | + xml3 = b'c' * 1024 + b'</a>' |
599 | 611 | parser = expat.ParserCreate() |
600 | 612 | parser.CharacterDataHandler = self.counting_handler |
601 | 613 | parser.buffer_text = 1 |
|
0 commit comments