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

# Source Code for Module lxml.tests.common_imports

      1  import unittest
      2  import os.path
      3  import re, gc, sys
      4
      5  from lxml import etree
      6
      7 -def make_version_tuple(version_string):
      8      l = []
      9      for part in re.findall('([0-9]+|[^0-9.]+)', version_string):
     10          try:
     11              l.append(int(part))
     12          except ValueError:
     13              l.append(part)
     14      return tuple(l)
     15
     16  try:
     17      from elementtree import ElementTree # standard ET
     18  except ImportError:
     19      try:
     20          from xml.etree import ElementTree # Python 2.5+
     21      except ImportError:
     22          ElementTree = None
     23
     24  if hasattr(ElementTree, 'VERSION'):
     25      ET_VERSION = make_version_tuple(ElementTree.VERSION)
     26  else:
     27      ET_VERSION = (0,0,0)
     28
     29  try:
     30      import cElementTree # standard ET
     31  except ImportError:
     32      try:
     33          from xml.etree import cElementTree # Python 2.5+
     34      except ImportError:
     35          cElementTree = None
     36
     37  if hasattr(cElementTree, 'VERSION'):
     38      CET_VERSION = make_version_tuple(cElementTree.VERSION)
     39  else:
     40      CET_VERSION = (0,0,0)
     41
     42 -def filter_by_version(test_class, version_dict, current_version):
     43      """Remove test methods that do not work with the current lib version.
     44      """
     45      find_required_version = version_dict.get
     46      def dummy_test_method(self):
     47          pass
     48      for name in dir(test_class):
     49          expected_version = find_required_version(name, (0,0,0))
     50          if expected_version > current_version:
     51              setattr(test_class, name, dummy_test_method)
     52
     53  try:
     54      import doctest
     55      # check if the system version has everything we need
     56      doctest.DocFileSuite
     57      doctest.DocTestParser
     58      doctest.NORMALIZE_WHITESPACE
     59      doctest.ELLIPSIS
     60  except (ImportError, AttributeError):
     61      # we need our own version to make it work (Python 2.3?)
     62      import local_doctest as doctest
     63
     64  try:
     65      sorted
     66  except NameError:
     67 -    def sorted(seq, **kwargs):
     68          seq = list(seq)
     69          seq.sort(**kwargs)
     70          return seq
     71  else:
     72      locals()['sorted'] = sorted
     73
     74 -def _get_caller_relative_path(filename, frame_depth=2):
     75      module = sys.modules[sys._getframe(frame_depth).f_globals['__name__']]
     76      return os.path.normpath(os.path.join(
     77              os.path.dirname(getattr(module, '__file__', '')), filename))
     78
     79  if sys.version_info[0] >= 3:
     80      # Python 3
     81      from builtins import str as unicode
     82 -    def _str(s, encoding="UTF-8"):
     83          return s
     84 -    def _bytes(s, encoding="UTF-8"):
     85          return s.encode(encoding)
     86      from io import StringIO, BytesIO as _BytesIO
     87 -    def BytesIO(*args):
     88          if args and isinstance(args[0], str):
     89              args = (args[0].encode("UTF-8"),)
     90          return _BytesIO(*args)
     91
     92      doctest_parser = doctest.DocTestParser()
     93      _fix_unicode = re.compile(r'(\s+)u(["\'])').sub
     94      _fix_exceptions = re.compile(r'(.*except [^(]*),\s*(.*:)').sub
     95 -    def make_doctest(filename):
     96          filename = _get_caller_relative_path(filename)
     97          doctests = read_file(filename)
     98          doctests = _fix_unicode(r'\1\2', doctests)
     99          doctests = _fix_exceptions(r'\1 as \2', doctests)
    100          return doctest.DocTestCase(
    101              doctest_parser.get_doctest(
    102                  doctests, {}, os.path.basename(filename), filename, 0))
    103  else:
    104      # Python 2
    105      from __builtin__ import unicode
    106 -    def _str(s, encoding="UTF-8"):
    107          return unicode(s, encoding=encoding)
    108 -    def _bytes(s, encoding="UTF-8"):
    109          return s
    110      from StringIO import StringIO
    111      BytesIO = StringIO
    112
    113      doctest_parser = doctest.DocTestParser()
    114      _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
    115      _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub
    116      _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
    117 -    def make_doctest(filename):
    118          filename = _get_caller_relative_path(filename)
    119          doctests = read_file(filename)
    120          doctests = _fix_traceback(r'\1\2', doctests)
    121          doctests = _fix_exceptions(r'\1, \2', doctests)
    122          doctests = _fix_bytes(r'\1\2', doctests)
    123          return doctest.DocTestCase(
    124              doctest_parser.get_doctest(
    125                  doctests, {}, os.path.basename(filename), filename, 0))
    126
    127 -class HelperTestCase(unittest.TestCase):
    128 -    def tearDown(self):
    129          gc.collect()
    130
    131 -    def parse(self, text, parser=None):
    132          f = BytesIO(text)
    133          return etree.parse(f, parser=parser)
    134
    135 -    def _rootstring(self, tree):
    136          return etree.tostring(tree.getroot()).replace(
    137              _bytes(' '), _bytes('')).replace(_bytes('\n'), _bytes(''))
    138
    139      # assertFalse doesn't exist in Python 2.3
    140      try:
    141          unittest.TestCase.assertFalse
    142      except AttributeError:
    143          assertFalse = unittest.TestCase.failIf
    144
    145 -class SillyFileLike:
    146 -    def __init__(self, xml_data=_bytes('<foo><bar/></foo>')):
    147          self.xml_data = xml_data
    148
    149 -    def read(self, amount=None):
    150          if self.xml_data:
    151              if amount:
    152                  data = self.xml_data[:amount]
    153                  self.xml_data = self.xml_data[amount:]
    154              else:
    155                  data = self.xml_data
    156                  self.xml_data = _bytes('')
    157              return data
    158          return _bytes('')
    159
    160 -class LargeFileLike:
    161 -    def __init__(self, charlen=100, depth=4, children=5):
    162          self.data = BytesIO()
    163          self.chars  = _bytes('a') * charlen
    164          self.children = range(children)
    165          self.more = self.iterelements(depth)
    166
    167 -    def iterelements(self, depth):
    168          yield _bytes('<root>')
    169          depth -= 1
    170          if depth > 0:
    171              for child in self.children:
    172                  for element in self.iterelements(depth):
    173                      yield element
    174                  yield self.chars
    175          else:
    176              yield self.chars
    177          yield _bytes('</root>')
    178
    179 -    def read(self, amount=None):
    180          data = self.data
    181          append = data.write
    182          if amount:
    183              for element in self.more:
    184                  append(element)
    185                  if data.tell() >= amount:
    186                      break
    187          else:
    188              for element in self.more:
    189                  append(element)
    190          result = data.getvalue()
    191          data.seek(0)
    192          data.truncate()
    193          if amount:
    194              append(result[amount:])
    195              result = result[:amount]
    196          return result
    197
    198 -class LargeFileLikeUnicode(LargeFileLike):
    199 -    def __init__(self, charlen=100, depth=4, children=5):
    200          LargeFileLike.__init__(self, charlen, depth, children)
    201          self.data = StringIO()
    202          self.chars  = _str('a') * charlen
    203          self.more = self.iterelements(depth)
    204
    205 -    def iterelements(self, depth):
    206          yield _str('<root>')
    207          depth -= 1
    208          if depth > 0:
    209              for child in self.children:
    210                  for element in self.iterelements(depth):
    211                      yield element
    212                  yield self.chars
    213          else:
    214              yield self.chars
    215          yield _str('</root>')
    216
    217 -def fileInTestDir(name):
    218      _testdir = os.path.dirname(__file__)
    219      return os.path.join(_testdir, name)
    220
    221 -def read_file(name, mode='r'):
    222      f = open(name, mode)
    223      try:
    224          data = f.read()
    225      finally:
    226          f.close()
    227      return data
    228
    229 -def write_to_file(name, data, mode='w'):
    230      f = open(name, mode)
    231      try:
    232          data = f.write(data)
    233      finally:
    234          f.close()
    235
    236 -def readFileInTestDir(name, mode='r'):
    237      return read_file(fileInTestDir(name), mode)
    238
    239 -def canonicalize(xml):
    240      tree = etree.parse(BytesIO(xml))
    241      f = BytesIO()
    242      tree.write_c14n(f)
    243      return f.getvalue()
    244
    245 -def unentitify(xml):
    246      for entity_name, value in re.findall("(&#([0-9]+);)", xml):
    247          xml = xml.replace(entity_name, unichr(int(value)))
    248      return xml
    249