lxml.tests.test_errors

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
    30 -    def test_element_cyclic_gc_none(self):
    31          # test if cyclic reference can crash etree
    32          Element = self.etree.Element
    33          getrefcount = sys.getrefcount
    34
    35          # must disable tracing as it could change the refcounts
    36          trace_func = sys.gettrace()
    37          try:
    38              sys.settrace(None)
    39              gc.collect()
    40
    41              count = getrefcount(None)
    42
    43              l = [Element('name'), Element('name')]
    44              l.append(l)
    45
    46              del l
    47              gc.collect()
    48              count = getrefcount(None) - count
    49
    50              self.assertEqual(count, 0)
    51          finally:
    52              sys.settrace(trace_func)
    53
    54 -    def test_xmlsyntaxerror_has_info(self):
    55          broken_xml_name = 'test_broken.xml'
    56          broken_xml_path = os.path.join(this_dir, broken_xml_name)
    57          fail_msg = 'test_broken.xml should raise an etree.XMLSyntaxError'
    58          try:
    59              etree.parse(broken_xml_path)
    60          except etree.XMLSyntaxError as e:
    61              # invariant
    62              self.assertEqual(e.position, (e.lineno, e.offset + 1), 'position and lineno/offset out of sync')
    63              # SyntaxError info derived from file & contents
    64              self.assertTrue(e.filename.endswith(broken_xml_name), 'filename must be preserved')
    65              self.assertEqual(e.lineno, 1)
    66              self.assertEqual(e.offset, 10)
    67          except Exception as e:
    68              self.fail('{0}, not {1}'.format(fail_msg, type(e)))
    69          else:
    70              self.fail('test_broken.xml should raise an etree.XMLSyntaxError')
    71
    72
    73 -def test_suite():
    74      suite = unittest.TestSuite()
    75      suite.addTests([unittest.makeSuite(ErrorTestCase)])
    76      return suite
    77
    78  if __name__ == '__main__':
    79      print('to test use test.py %s' % __file__)
    80