ยปCore Development>Code coverage>Lib/test/test_pyexpat.py

Python code coverage for Lib/test/test_pyexpat.py

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