blob: db69e5f2c5635fad2edee0fa2bd5449210e33dfc [file] [log] [blame]
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001# xml.etree test for cElementTree
Fredrik Lundh9235ea42005-12-15 18:41:22 +00002
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Victor Stinnerb3c9e072011-01-04 02:07:34 +00004from test.support import precisionbigmemtest, _2G
5import unittest
Fredrik Lundh9235ea42005-12-15 18:41:22 +00006
Florent Xiclunaf15351d2010-03-13 23:24:31 +00007cET = support.import_module('xml.etree.cElementTree')
Fredrik Lundh8911ca3d2005-12-16 22:07:17 +00008
Fredrik Lundh9235ea42005-12-15 18:41:22 +00009
Florent Xiclunaf15351d2010-03-13 23:24:31 +000010# cElementTree specific tests
Fredrik Lundh9235ea42005-12-15 18:41:22 +000011
12def sanity():
Alexander Belopolskye239d232010-12-08 23:31:48 +000013 r"""
Fredrik Lundh9235ea42005-12-15 18:41:22 +000014 Import sanity.
15
Thomas Wouters0e3f5912006-08-11 14:57:12 +000016 >>> from xml.etree import cElementTree
Alexander Belopolskye239d232010-12-08 23:31:48 +000017
18 Issue #6697.
19
20 >>> e = cElementTree.Element('a')
21 >>> getattr(e, '\uD800') # doctest: +ELLIPSIS
22 Traceback (most recent call last):
23 ...
24 UnicodeEncodeError: ...
25
26 >>> p = cElementTree.XMLParser()
27 >>> p.version.split()[0]
28 'Expat'
29 >>> getattr(p, '\uD800')
30 Traceback (most recent call last):
31 ...
32 AttributeError: 'XMLParser' object has no attribute '\ud800'
Fredrik Lundh9235ea42005-12-15 18:41:22 +000033 """
34
Thomas Wouters00ee7ba2006-08-21 19:07:27 +000035
Victor Stinnerb3c9e072011-01-04 02:07:34 +000036class MiscTests(unittest.TestCase):
37 # Issue #8651.
38 @support.precisionbigmemtest(size=support._2G + 100, memuse=1)
39 def test_length_overflow(self, size):
40 if size < support._2G + 100:
41 self.skipTest("not enough free memory, need at least 2 GB")
42 data = b'x' * size
43 parser = cET.XMLParser()
44 try:
45 self.assertRaises(OverflowError, parser.feed, data)
46 finally:
47 data = None
48
49
Fredrik Lundh9235ea42005-12-15 18:41:22 +000050def test_main():
Florent Xiclunaf15351d2010-03-13 23:24:31 +000051 from test import test_xml_etree, test_xml_etree_c
52
53 # Run the tests specific to the C implementation
Benjamin Petersonee8712c2008-05-20 21:35:26 +000054 support.run_doctest(test_xml_etree_c, verbosity=True)
Fredrik Lundh9235ea42005-12-15 18:41:22 +000055
Victor Stinnerb3c9e072011-01-04 02:07:34 +000056 support.run_unittest(MiscTests)
57
Florent Xiclunaf15351d2010-03-13 23:24:31 +000058 # Assign the C implementation before running the doctests
59 # Patch the __name__, to prevent confusion with the pure Python test
60 pyET = test_xml_etree.ET
61 py__name__ = test_xml_etree.__name__
62 test_xml_etree.ET = cET
63 if __name__ != '__main__':
64 test_xml_etree.__name__ = __name__
65 try:
66 # Run the same test suite as xml.etree.ElementTree
67 test_xml_etree.test_main(module_name='xml.etree.cElementTree')
68 finally:
69 test_xml_etree.ET = pyET
70 test_xml_etree.__name__ = py__name__
71
Fredrik Lundh9235ea42005-12-15 18:41:22 +000072if __name__ == '__main__':
73 test_main()