Package lxml :: Package tests :: Module test_errors
[hide private]
[frames] | no frames]

Source Code for Module lxml.tests.test_errors

 1  # -*- coding: utf-8 -*- 
 2  import unittest, doctest 
 3   
 4  # These tests check that error handling in the Pyrex code is 
 5  # complete. 
 6  # It is likely that if there are errors, instead of failing the code 
 7  # will simply crash. 
 8   
 9  import sys, gc, os.path 
10  from lxml import etree 
11   
12  this_dir = os.path.dirname(__file__) 
13  if this_dir not in sys.path: 
14      sys.path.insert(0, this_dir) # needed for Py3 
15   
16  from common_imports import HelperTestCase 
17   
18   
19 -class ErrorTestCase(HelperTestCase):
20 etree = etree 21
22 - def test_bad_element(self):
23 # attrib argument of Element() should be a dictionary, so if 24 # we pass a string we should get an error. 25 self.assertRaises(TypeError, self.etree.Element, 'a', 'b')
26
27 - def test_empty_parse(self):
28 self.assertRaises(etree.XMLSyntaxError, etree.fromstring, '')
29
31 # test if cyclic reference can crash etree 32 Element = self.etree.Element 33 34 # must disable tracing as it could change the refcounts 35 trace_func = sys.gettrace() 36 try: 37 sys.settrace(None) 38 gc.collect() 39 40 count = sys.getrefcount(None) 41 42 l = [Element('name'), Element('name')] 43 l.append(l) 44 45 del l 46 gc.collect() 47 48 self.assertEqual(sys.getrefcount(None), count) 49 finally: 50 sys.settrace(trace_func)
51 52
53 -def test_suite():
54 suite = unittest.TestSuite() 55 suite.addTests([unittest.makeSuite(ErrorTestCase)]) 56 return suite
57 58 if __name__ == '__main__': 59 print('to test use test.py %s' % __file__) 60