Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 1 | # XXX TypeErrors on calling handlers, or on bad return values from a |
| 2 | # handler, are obscure and unhelpful. |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 3 | |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 4 | from io import BytesIO |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 5 | import sys |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | |
Jeremy Hylton | 3c19ec4 | 2001-07-30 21:47:25 +0000 | [diff] [blame] | 8 | import pyexpat |
Fred Drake | 7fbc85c | 2000-09-23 04:47:56 +0000 | [diff] [blame] | 9 | from xml.parsers import expat |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 10 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 11 | from test.support import sortdict, run_unittest |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 12 | |
| 13 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 14 | class SetAttributeTest(unittest.TestCase): |
| 15 | def setUp(self): |
| 16 | self.parser = expat.ParserCreate(namespace_separator='!') |
| 17 | self.set_get_pairs = [ |
| 18 | [0, 0], |
| 19 | [1, 1], |
| 20 | [2, 1], |
| 21 | [0, 0], |
| 22 | ] |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 24 | def test_ordered_attributes(self): |
| 25 | for x, y in self.set_get_pairs: |
| 26 | self.parser.ordered_attributes = x |
| 27 | self.assertEquals(self.parser.ordered_attributes, y) |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 29 | def test_specified_attributes(self): |
| 30 | for x, y in self.set_get_pairs: |
| 31 | self.parser.specified_attributes = x |
| 32 | self.assertEquals(self.parser.specified_attributes, y) |
Fred Drake | 8f42e2b | 2001-04-25 16:03:54 +0000 | [diff] [blame] | 33 | |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 35 | data = b'''\ |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 36 | <?xml version="1.0" encoding="iso-8859-1" standalone="no"?> |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 37 | <?xml-stylesheet href="stylesheet.css"?> |
| 38 | <!-- comment data --> |
| 39 | <!DOCTYPE quotations SYSTEM "quotations.dtd" [ |
| 40 | <!ELEMENT root ANY> |
| 41 | <!NOTATION notation SYSTEM "notation.jpeg"> |
| 42 | <!ENTITY acirc "â"> |
| 43 | <!ENTITY external_entity SYSTEM "entity.file"> |
| 44 | <!ENTITY unparsed_entity SYSTEM "entity.file" NDATA notation> |
| 45 | %unparsed_entity; |
| 46 | ]> |
| 47 | |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 48 | <root attr1="value1" attr2="value2ὀ"> |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 49 | <myns:subelement xmlns:myns="http://www.python.org/namespace"> |
| 50 | Contents of subelements |
| 51 | </myns:subelement> |
| 52 | <sub2><![CDATA[contents of CDATA section]]></sub2> |
| 53 | &external_entity; |
| 54 | </root> |
Fred Drake | 265a804 | 2000-09-21 20:32:13 +0000 | [diff] [blame] | 55 | ''' |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 57 | |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 58 | # Produce UTF-8 output |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 59 | class ParseTest(unittest.TestCase): |
| 60 | class Outputter: |
| 61 | def __init__(self): |
| 62 | self.out = [] |
Andrew M. Kuchling | b17664d | 2000-03-31 15:44:52 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 64 | def StartElementHandler(self, name, attrs): |
| 65 | self.out.append('Start element: ' + repr(name) + ' ' + |
| 66 | sortdict(attrs)) |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 68 | def EndElementHandler(self, name): |
| 69 | self.out.append('End element: ' + repr(name)) |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 71 | def CharacterDataHandler(self, data): |
| 72 | data = data.strip() |
| 73 | if data: |
| 74 | self.out.append('Character data: ' + repr(data)) |
Andrew M. Kuchling | 7fd7e36 | 2000-06-27 00:37:25 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 76 | def ProcessingInstructionHandler(self, target, data): |
| 77 | self.out.append('PI: ' + repr(target) + ' ' + repr(data)) |
| 78 | |
| 79 | def StartNamespaceDeclHandler(self, prefix, uri): |
| 80 | self.out.append('NS decl: ' + repr(prefix) + ' ' + repr(uri)) |
| 81 | |
| 82 | def EndNamespaceDeclHandler(self, prefix): |
| 83 | self.out.append('End of NS decl: ' + repr(prefix)) |
| 84 | |
| 85 | def StartCdataSectionHandler(self): |
| 86 | self.out.append('Start of CDATA section') |
| 87 | |
| 88 | def EndCdataSectionHandler(self): |
| 89 | self.out.append('End of CDATA section') |
| 90 | |
| 91 | def CommentHandler(self, text): |
| 92 | self.out.append('Comment: ' + repr(text)) |
| 93 | |
| 94 | def NotationDeclHandler(self, *args): |
| 95 | name, base, sysid, pubid = args |
| 96 | self.out.append('Notation declared: %s' %(args,)) |
| 97 | |
| 98 | def UnparsedEntityDeclHandler(self, *args): |
| 99 | entityName, base, systemId, publicId, notationName = args |
| 100 | self.out.append('Unparsed entity decl: %s' %(args,)) |
| 101 | |
| 102 | def NotStandaloneHandler(self, userData): |
| 103 | self.out.append('Not standalone') |
| 104 | return 1 |
| 105 | |
| 106 | def ExternalEntityRefHandler(self, *args): |
| 107 | context, base, sysId, pubId = args |
| 108 | self.out.append('External entity ref: %s' %(args[1:],)) |
| 109 | return 1 |
| 110 | |
| 111 | def DefaultHandler(self, userData): |
| 112 | pass |
| 113 | |
| 114 | def DefaultHandlerExpand(self, userData): |
| 115 | pass |
| 116 | |
| 117 | handler_names = [ |
| 118 | 'StartElementHandler', 'EndElementHandler', |
| 119 | 'CharacterDataHandler', 'ProcessingInstructionHandler', |
| 120 | 'UnparsedEntityDeclHandler', 'NotationDeclHandler', |
| 121 | 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler', |
| 122 | 'CommentHandler', 'StartCdataSectionHandler', |
| 123 | 'EndCdataSectionHandler', |
| 124 | 'DefaultHandler', 'DefaultHandlerExpand', |
| 125 | #'NotStandaloneHandler', |
| 126 | 'ExternalEntityRefHandler' |
| 127 | ] |
| 128 | |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 129 | def _verify_parse_output(self, op): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 130 | self.assertEquals(op[0], 'PI: \'xml-stylesheet\' \'href="stylesheet.css"\'') |
| 131 | self.assertEquals(op[1], "Comment: ' comment data '") |
| 132 | self.assertEquals(op[2], "Notation declared: ('notation', None, 'notation.jpeg', None)") |
| 133 | self.assertEquals(op[3], "Unparsed entity decl: ('unparsed_entity', None, 'entity.file', None, 'notation')") |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 134 | self.assertEquals(op[4], "Start element: 'root' {'attr1': 'value1', 'attr2': 'value2\\u1f40'}") |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 135 | self.assertEquals(op[5], "NS decl: 'myns' 'http://www.python.org/namespace'") |
| 136 | self.assertEquals(op[6], "Start element: 'http://www.python.org/namespace!subelement' {}") |
| 137 | self.assertEquals(op[7], "Character data: 'Contents of subelements'") |
| 138 | self.assertEquals(op[8], "End element: 'http://www.python.org/namespace!subelement'") |
| 139 | self.assertEquals(op[9], "End of NS decl: 'myns'") |
| 140 | self.assertEquals(op[10], "Start element: 'sub2' {}") |
| 141 | self.assertEquals(op[11], 'Start of CDATA section') |
| 142 | self.assertEquals(op[12], "Character data: 'contents of CDATA section'") |
| 143 | self.assertEquals(op[13], 'End of CDATA section') |
| 144 | self.assertEquals(op[14], "End element: 'sub2'") |
| 145 | self.assertEquals(op[15], "External entity ref: (None, 'entity.file', None)") |
| 146 | self.assertEquals(op[16], "End element: 'root'") |
| 147 | |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 148 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 149 | def test_unicode(self): |
| 150 | # Try the parse again, this time producing Unicode output |
| 151 | out = self.Outputter() |
| 152 | parser = expat.ParserCreate(namespace_separator='!') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 153 | for name in self.handler_names: |
| 154 | setattr(parser, name, getattr(out, name)) |
| 155 | |
| 156 | parser.Parse(data, 1) |
| 157 | |
| 158 | op = out.out |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 159 | self._verify_parse_output(op) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 160 | |
| 161 | def test_parse_file(self): |
| 162 | # Try parsing a file |
| 163 | out = self.Outputter() |
| 164 | parser = expat.ParserCreate(namespace_separator='!') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 165 | for name in self.handler_names: |
| 166 | setattr(parser, name, getattr(out, name)) |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 167 | file = BytesIO(data) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 168 | |
| 169 | parser.ParseFile(file) |
| 170 | |
| 171 | op = out.out |
Guido van Rossum | 4ca9471 | 2007-07-23 17:42:32 +0000 | [diff] [blame] | 172 | self._verify_parse_output(op) |
Fred Drake | 1e0611b | 2000-12-23 22:12:07 +0000 | [diff] [blame] | 173 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 174 | class NamespaceSeparatorTest(unittest.TestCase): |
| 175 | def test_legal(self): |
| 176 | # Tests that make sure we get errors when the namespace_separator value |
| 177 | # is illegal, and that we don't for good values: |
| 178 | expat.ParserCreate() |
| 179 | expat.ParserCreate(namespace_separator=None) |
| 180 | expat.ParserCreate(namespace_separator=' ') |
Fred Drake | 8f42e2b | 2001-04-25 16:03:54 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 182 | def test_illegal(self): |
| 183 | try: |
| 184 | expat.ParserCreate(namespace_separator=42) |
| 185 | self.fail() |
| 186 | except TypeError as e: |
| 187 | self.assertEquals(str(e), |
| 188 | 'ParserCreate() argument 2 must be string or None, not int') |
Fred Drake | 8f42e2b | 2001-04-25 16:03:54 +0000 | [diff] [blame] | 189 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 190 | try: |
| 191 | expat.ParserCreate(namespace_separator='too long') |
| 192 | self.fail() |
| 193 | except ValueError as e: |
| 194 | self.assertEquals(str(e), |
| 195 | 'namespace_separator must be at most one character, omitted, or None') |
Fred Drake | 1add023 | 2002-06-27 19:41:51 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 197 | def test_zero_length(self): |
| 198 | # ParserCreate() needs to accept a namespace_separator of zero length |
| 199 | # to satisfy the requirements of RDF applications that are required |
| 200 | # to simply glue together the namespace URI and the localname. Though |
| 201 | # considered a wart of the RDF specifications, it needs to be supported. |
| 202 | # |
| 203 | # See XML-SIG mailing list thread starting with |
| 204 | # http://mail.python.org/pipermail/xml-sig/2001-April/005202.html |
| 205 | # |
| 206 | expat.ParserCreate(namespace_separator='') # too short |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 207 | |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 209 | class InterningTest(unittest.TestCase): |
| 210 | def test(self): |
| 211 | # Test the interning machinery. |
| 212 | p = expat.ParserCreate() |
| 213 | L = [] |
| 214 | def collector(name, *args): |
| 215 | L.append(name) |
| 216 | p.StartElementHandler = collector |
| 217 | p.EndElementHandler = collector |
| 218 | p.Parse("<e> <e/> <e></e> </e>", 1) |
| 219 | tag = L[0] |
| 220 | self.assertEquals(len(L), 6) |
| 221 | for entry in L: |
| 222 | # L should have the same string repeated over and over. |
| 223 | self.assertTrue(tag is entry) |
| 224 | |
| 225 | |
| 226 | class BufferTextTest(unittest.TestCase): |
| 227 | def setUp(self): |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 228 | self.stuff = [] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 229 | self.parser = expat.ParserCreate() |
| 230 | self.parser.buffer_text = 1 |
| 231 | self.parser.CharacterDataHandler = self.CharacterDataHandler |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 232 | |
| 233 | def check(self, expected, label): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 234 | self.assertEquals(self.stuff, expected, |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 235 | "%s\nstuff = %r\nexpected = %r" |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 236 | % (label, self.stuff, map(str, expected))) |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 237 | |
| 238 | def CharacterDataHandler(self, text): |
| 239 | self.stuff.append(text) |
| 240 | |
| 241 | def StartElementHandler(self, name, attrs): |
| 242 | self.stuff.append("<%s>" % name) |
| 243 | bt = attrs.get("buffer-text") |
| 244 | if bt == "yes": |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 245 | self.parser.buffer_text = 1 |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 246 | elif bt == "no": |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 247 | self.parser.buffer_text = 0 |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 248 | |
| 249 | def EndElementHandler(self, name): |
| 250 | self.stuff.append("</%s>" % name) |
| 251 | |
| 252 | def CommentHandler(self, data): |
| 253 | self.stuff.append("<!--%s-->" % data) |
| 254 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 255 | def setHandlers(self, handlers=[]): |
| 256 | for name in handlers: |
| 257 | setattr(self.parser, name, getattr(self, name)) |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 259 | def test_default_to_disabled(self): |
| 260 | parser = expat.ParserCreate() |
| 261 | self.assertFalse(parser.buffer_text) |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 262 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 263 | def test_buffering_enabled(self): |
| 264 | # Make sure buffering is turned on |
| 265 | self.assertTrue(self.parser.buffer_text) |
| 266 | self.parser.Parse("<a>1<b/>2<c/>3</a>", 1) |
| 267 | self.assertEquals(self.stuff, ['123'], |
| 268 | "buffered text not properly collapsed") |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 269 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 270 | def test1(self): |
| 271 | # XXX This test exposes more detail of Expat's text chunking than we |
| 272 | # XXX like, but it tests what we need to concisely. |
| 273 | self.setHandlers(["StartElementHandler"]) |
| 274 | self.parser.Parse("<a>1<b buffer-text='no'/>2\n3<c buffer-text='yes'/>4\n5</a>", 1) |
| 275 | self.assertEquals(self.stuff, |
| 276 | ["<a>", "1", "<b>", "2", "\n", "3", "<c>", "4\n5"], |
| 277 | "buffering control not reacting as expected") |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 279 | def test2(self): |
| 280 | self.parser.Parse("<a>1<b/><2><c/> \n 3</a>", 1) |
| 281 | self.assertEquals(self.stuff, ["1<2> \n 3"], |
| 282 | "buffered text not properly collapsed") |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 284 | def test3(self): |
| 285 | self.setHandlers(["StartElementHandler"]) |
| 286 | self.parser.Parse("<a>1<b/>2<c/>3</a>", 1) |
| 287 | self.assertEquals(self.stuff, ["<a>", "1", "<b>", "2", "<c>", "3"], |
| 288 | "buffered text not properly split") |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 289 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 290 | def test4(self): |
| 291 | self.setHandlers(["StartElementHandler", "EndElementHandler"]) |
| 292 | self.parser.CharacterDataHandler = None |
| 293 | self.parser.Parse("<a>1<b/>2<c/>3</a>", 1) |
| 294 | self.assertEquals(self.stuff, |
| 295 | ["<a>", "<b>", "</b>", "<c>", "</c>", "</a>"]) |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 297 | def test5(self): |
| 298 | self.setHandlers(["StartElementHandler", "EndElementHandler"]) |
| 299 | self.parser.Parse("<a>1<b></b>2<c/>3</a>", 1) |
| 300 | self.assertEquals(self.stuff, |
| 301 | ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", "</a>"]) |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 303 | def test6(self): |
| 304 | self.setHandlers(["CommentHandler", "EndElementHandler", |
| 305 | "StartElementHandler"]) |
| 306 | self.parser.Parse("<a>1<b/>2<c></c>345</a> ", 1) |
| 307 | self.assertEquals(self.stuff, |
| 308 | ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "345", "</a>"], |
| 309 | "buffered text not properly split") |
Fred Drake | 2a3d7db | 2002-06-28 22:56:48 +0000 | [diff] [blame] | 310 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 311 | def test7(self): |
| 312 | self.setHandlers(["CommentHandler", "EndElementHandler", |
| 313 | "StartElementHandler"]) |
| 314 | self.parser.Parse("<a>1<b/>2<c></c>3<!--abc-->4<!--def-->5</a> ", 1) |
| 315 | self.assertEquals(self.stuff, |
| 316 | ["<a>", "1", "<b>", "</b>", "2", "<c>", "</c>", "3", |
| 317 | "<!--abc-->", "4", "<!--def-->", "5", "</a>"], |
| 318 | "buffered text not properly split") |
| 319 | |
Fred Drake | d7ea55b | 2004-08-13 03:09:07 +0000 | [diff] [blame] | 320 | |
| 321 | # Test handling of exception from callback: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 322 | class HandlerExceptionTest(unittest.TestCase): |
| 323 | def StartElementHandler(self, name, attrs): |
| 324 | raise RuntimeError(name) |
Fred Drake | d7ea55b | 2004-08-13 03:09:07 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 326 | def test(self): |
| 327 | parser = expat.ParserCreate() |
| 328 | parser.StartElementHandler = self.StartElementHandler |
| 329 | try: |
| 330 | parser.Parse("<a><b><c/></b></a>", 1) |
| 331 | self.fail() |
| 332 | except RuntimeError as e: |
| 333 | self.assertEquals(e.args[0], 'a', |
| 334 | "Expected RuntimeError for element 'a', but" + \ |
| 335 | " found %r" % e.args[0]) |
Fred Drake | d7ea55b | 2004-08-13 03:09:07 +0000 | [diff] [blame] | 336 | |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 337 | |
| 338 | # Test Current* members: |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 339 | class PositionTest(unittest.TestCase): |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 340 | def StartElementHandler(self, name, attrs): |
| 341 | self.check_pos('s') |
| 342 | |
| 343 | def EndElementHandler(self, name): |
| 344 | self.check_pos('e') |
| 345 | |
| 346 | def check_pos(self, event): |
| 347 | pos = (event, |
| 348 | self.parser.CurrentByteIndex, |
| 349 | self.parser.CurrentLineNumber, |
| 350 | self.parser.CurrentColumnNumber) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 351 | self.assertTrue(self.upto < len(self.expected_list), |
| 352 | 'too many parser events') |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 353 | expected = self.expected_list[self.upto] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 354 | self.assertEquals(pos, expected, |
| 355 | 'Expected position %s, got position %s' %(pos, expected)) |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 356 | self.upto += 1 |
| 357 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 358 | def test(self): |
| 359 | self.parser = expat.ParserCreate() |
| 360 | self.parser.StartElementHandler = self.StartElementHandler |
| 361 | self.parser.EndElementHandler = self.EndElementHandler |
| 362 | self.upto = 0 |
| 363 | self.expected_list = [('s', 0, 1, 0), ('s', 5, 2, 1), ('s', 11, 3, 2), |
| 364 | ('e', 15, 3, 6), ('e', 17, 4, 1), ('e', 22, 5, 0)] |
Dave Cole | 3203efb | 2004-08-26 00:37:31 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 366 | xml = '<a>\n <b>\n <c/>\n </b>\n</a>' |
| 367 | self.parser.Parse(xml, 1) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 368 | |
| 369 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 370 | class sf1296433Test(unittest.TestCase): |
| 371 | def test_parse_only_xml_data(self): |
| 372 | # http://python.org/sf/1296433 |
| 373 | # |
| 374 | xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * 1025) |
| 375 | # this one doesn't crash |
| 376 | #xml = "<?xml version='1.0'?><s>%s</s>" % ('a' * 10000) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 378 | class SpecificException(Exception): |
| 379 | pass |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 380 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 381 | def handler(text): |
| 382 | raise SpecificException |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 383 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 384 | parser = expat.ParserCreate() |
| 385 | parser.CharacterDataHandler = handler |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 386 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 387 | self.assertRaises(Exception, parser.Parse, xml) |
| 388 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 389 | class ChardataBufferTest(unittest.TestCase): |
| 390 | """ |
| 391 | test setting of chardata buffer size |
| 392 | """ |
| 393 | |
| 394 | def test_1025_bytes(self): |
| 395 | self.assertEquals(self.small_buffer_test(1025), 2) |
| 396 | |
| 397 | def test_1000_bytes(self): |
| 398 | self.assertEquals(self.small_buffer_test(1000), 1) |
| 399 | |
| 400 | def test_wrong_size(self): |
| 401 | parser = expat.ParserCreate() |
| 402 | parser.buffer_text = 1 |
| 403 | def f(size): |
| 404 | parser.buffer_size = size |
| 405 | |
| 406 | self.assertRaises(ValueError, f, -1) |
| 407 | self.assertRaises(ValueError, f, 0) |
| 408 | |
| 409 | def test_unchanged_size(self): |
| 410 | xml1 = ("<?xml version='1.0' encoding='iso8859'?><s>%s" % ('a' * 512)) |
| 411 | xml2 = 'a'*512 + '</s>' |
| 412 | parser = expat.ParserCreate() |
| 413 | parser.CharacterDataHandler = self.counting_handler |
| 414 | parser.buffer_size = 512 |
| 415 | parser.buffer_text = 1 |
| 416 | |
| 417 | # Feed 512 bytes of character data: the handler should be called |
| 418 | # once. |
| 419 | self.n = 0 |
| 420 | parser.Parse(xml1) |
| 421 | self.assertEquals(self.n, 1) |
| 422 | |
| 423 | # Reassign to buffer_size, but assign the same size. |
| 424 | parser.buffer_size = parser.buffer_size |
| 425 | self.assertEquals(self.n, 1) |
| 426 | |
| 427 | # Try parsing rest of the document |
| 428 | parser.Parse(xml2) |
| 429 | self.assertEquals(self.n, 2) |
| 430 | |
| 431 | |
| 432 | def test_disabling_buffer(self): |
| 433 | xml1 = "<?xml version='1.0' encoding='iso8859'?><a>%s" % ('a' * 512) |
| 434 | xml2 = ('b' * 1024) |
| 435 | xml3 = "%s</a>" % ('c' * 1024) |
| 436 | parser = expat.ParserCreate() |
| 437 | parser.CharacterDataHandler = self.counting_handler |
| 438 | parser.buffer_text = 1 |
| 439 | parser.buffer_size = 1024 |
| 440 | self.assertEquals(parser.buffer_size, 1024) |
| 441 | |
| 442 | # Parse one chunk of XML |
| 443 | self.n = 0 |
| 444 | parser.Parse(xml1, 0) |
| 445 | self.assertEquals(parser.buffer_size, 1024) |
| 446 | self.assertEquals(self.n, 1) |
| 447 | |
| 448 | # Turn off buffering and parse the next chunk. |
| 449 | parser.buffer_text = 0 |
| 450 | self.assertFalse(parser.buffer_text) |
| 451 | self.assertEquals(parser.buffer_size, 1024) |
| 452 | for i in range(10): |
| 453 | parser.Parse(xml2, 0) |
| 454 | self.assertEquals(self.n, 11) |
| 455 | |
| 456 | parser.buffer_text = 1 |
| 457 | self.assertTrue(parser.buffer_text) |
| 458 | self.assertEquals(parser.buffer_size, 1024) |
| 459 | parser.Parse(xml3, 1) |
| 460 | self.assertEquals(self.n, 12) |
| 461 | |
| 462 | |
| 463 | |
| 464 | def make_document(self, bytes): |
| 465 | return ("<?xml version='1.0'?><tag>" + bytes * 'a' + '</tag>') |
| 466 | |
| 467 | def counting_handler(self, text): |
| 468 | self.n += 1 |
| 469 | |
| 470 | def small_buffer_test(self, buffer_len): |
| 471 | xml = "<?xml version='1.0' encoding='iso8859'?><s>%s</s>" % ('a' * buffer_len) |
| 472 | parser = expat.ParserCreate() |
| 473 | parser.CharacterDataHandler = self.counting_handler |
| 474 | parser.buffer_size = 1024 |
| 475 | parser.buffer_text = 1 |
| 476 | |
| 477 | self.n = 0 |
| 478 | parser.Parse(xml) |
| 479 | return self.n |
| 480 | |
| 481 | def test_change_size_1(self): |
| 482 | xml1 = "<?xml version='1.0' encoding='iso8859'?><a><s>%s" % ('a' * 1024) |
| 483 | xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025) |
| 484 | parser = expat.ParserCreate() |
| 485 | parser.CharacterDataHandler = self.counting_handler |
| 486 | parser.buffer_text = 1 |
| 487 | parser.buffer_size = 1024 |
| 488 | self.assertEquals(parser.buffer_size, 1024) |
| 489 | |
| 490 | self.n = 0 |
| 491 | parser.Parse(xml1, 0) |
| 492 | parser.buffer_size *= 2 |
| 493 | self.assertEquals(parser.buffer_size, 2048) |
| 494 | parser.Parse(xml2, 1) |
| 495 | self.assertEquals(self.n, 2) |
| 496 | |
| 497 | def test_change_size_2(self): |
| 498 | xml1 = "<?xml version='1.0' encoding='iso8859'?><a>a<s>%s" % ('a' * 1023) |
| 499 | xml2 = "aaa</s><s>%s</s></a>" % ('a' * 1025) |
| 500 | parser = expat.ParserCreate() |
| 501 | parser.CharacterDataHandler = self.counting_handler |
| 502 | parser.buffer_text = 1 |
| 503 | parser.buffer_size = 2048 |
| 504 | self.assertEquals(parser.buffer_size, 2048) |
| 505 | |
| 506 | self.n=0 |
| 507 | parser.Parse(xml1, 0) |
| 508 | parser.buffer_size = parser.buffer_size // 2 |
| 509 | self.assertEquals(parser.buffer_size, 1024) |
| 510 | parser.Parse(xml2, 1) |
| 511 | self.assertEquals(self.n, 4) |
| 512 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 513 | |
| 514 | def test_main(): |
| 515 | run_unittest(SetAttributeTest, |
| 516 | ParseTest, |
| 517 | NamespaceSeparatorTest, |
| 518 | InterningTest, |
| 519 | BufferTextTest, |
| 520 | HandlerExceptionTest, |
| 521 | PositionTest, |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 522 | sf1296433Test, |
| 523 | ChardataBufferTest) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 524 | |
| 525 | if __name__ == "__main__": |
| 526 | test_main() |