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  try:
     75      import pytest
     76  except ImportError:
     77 -    class skipif(object):
     78          "Using a class because a function would bind into a method when used in classes"
     79 -        def __init__(self, *args): pass
     80 -        def __call__(self, func, *args): return func
     81  else:
     82      skipif = pytest.mark.skipif
     83
     84 -def _get_caller_relative_path(filename, frame_depth=2):
     85      module = sys.modules[sys._getframe(frame_depth).f_globals['__name__']]
     86      return os.path.normpath(os.path.join(
     87              os.path.dirname(getattr(module, '__file__', '')), filename))
     88
     89  if sys.version_info[0] >= 3:
     90      # Python 3
     91      from builtins import str as unicode
     92 -    def _str(s, encoding="UTF-8"):
     93          return s
     94 -    def _bytes(s, encoding="UTF-8"):
     95          return s.encode(encoding)
     96      from io import StringIO, BytesIO as _BytesIO
     97 -    def BytesIO(*args):
     98          if args and isinstance(args[0], str):
     99              args = (args[0].encode("UTF-8"),)
    100          return _BytesIO(*args)
    101
    102      doctest_parser = doctest.DocTestParser()
    103      _fix_unicode = re.compile(r'(\s+)u(["\'])').sub
    104      _fix_exceptions = re.compile(r'(.*except [^(]*),\s*(.*:)').sub
    105 -    def make_doctest(filename):
    106          filename = _get_caller_relative_path(filename)
    107          doctests = read_file(filename)
    108          doctests = _fix_unicode(r'\1\2', doctests)
    109          doctests = _fix_exceptions(r'\1 as \2', doctests)
    110          return doctest.DocTestCase(
    111              doctest_parser.get_doctest(
    112                  doctests, {}, os.path.basename(filename), filename, 0))
    113  else:
    114      # Python 2
    115      from __builtin__ import unicode
    116 -    def _str(s, encoding="UTF-8"):
    117          return unicode(s, encoding=encoding)
    118 -    def _bytes(s, encoding="UTF-8"):
    119          return s
    120      from StringIO import StringIO
    121      BytesIO = StringIO
    122
    123      doctest_parser = doctest.DocTestParser()
    124      _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
    125      _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub
    126      _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
    127 -    def make_doctest(filename):
    128          filename = _get_caller_relative_path(filename)
    129          doctests = read_file(filename)
    130          doctests = _fix_traceback(r'\1\2', doctests)
    131          doctests = _fix_exceptions(r'\1, \2', doctests)
    132          doctests = _fix_bytes(r'\1\2', doctests)
    133          return doctest.DocTestCase(
    134              doctest_parser.get_doctest(
    135                  doctests, {}, os.path.basename(filename), filename, 0))
    136
    137 -class HelperTestCase(unittest.TestCase):
    138 -    def tearDown(self):
    139          gc.collect()
    140
    141 -    def parse(self, text, parser=None):
    142          f = BytesIO(text)
    143          return etree.parse(f, parser=parser)
    144
    145 -    def _rootstring(self, tree):
    146          return etree.tostring(tree.getroot()).replace(
    147              _bytes(' '), _bytes('')).replace(_bytes('\n'), _bytes(''))
    148
    149      # assertFalse doesn't exist in Python 2.3
    150      try:
    151          unittest.TestCase.assertFalse
    152      except AttributeError:
    153          assertFalse = unittest.TestCase.failIf
    154
    155 -class SillyFileLike:
    156 -    def __init__(self, xml_data=_bytes('<foo><bar/></foo>')):
    157          self.xml_data = xml_data
    158
    159 -    def read(self, amount=None):
    160          if self.xml_data:
    161              if amount:
    162                  data = self.xml_data[:amount]
    163                  self.xml_data = self.xml_data[amount:]
    164              else:
    165                  data = self.xml_data
    166                  self.xml_data = _bytes('')
    167              return data
    168          return _bytes('')
    169
    170 -class LargeFileLike:
    171 -    def __init__(self, charlen=100, depth=4, children=5):
    172          self.data = BytesIO()
    173          self.chars  = _bytes('a') * charlen
    174          self.children = range(children)
    175          self.more = self.iterelements(depth)
    176
    177 -    def iterelements(self, depth):
    178          yield _bytes('<root>')
    179          depth -= 1
    180          if depth > 0:
    181              for child in self.children:
    182                  for element in self.iterelements(depth):
    183                      yield element
    184                  yield self.chars
    185          else:
    186              yield self.chars
    187          yield _bytes('</root>')
    188
    189 -    def read(self, amount=None):
    190          data = self.data
    191          append = data.write
    192          if amount:
    193              for element in self.more:
    194                  append(element)
    195                  if data.tell() >= amount:
    196                      break
    197          else:
    198              for element in self.more:
    199                  append(element)
    200          result = data.getvalue()
    201          data.seek(0)
    202          data.truncate()
    203          if amount:
    204              append(result[amount:])
    205              result = result[:amount]
    206          return result
    207
    208 -class LargeFileLikeUnicode(LargeFileLike):
    209 -    def __init__(self, charlen=100, depth=4, children=5):
    210          LargeFileLike.__init__(self, charlen, depth, children)
    211          self.data = StringIO()
    212          self.chars  = _str('a') * charlen
    213          self.more = self.iterelements(depth)
    214
    215 -    def iterelements(self, depth):
    216          yield _str('<root>')
    217          depth -= 1
    218          if depth > 0:
    219              for child in self.children:
    220                  for element in self.iterelements(depth):
    221                      yield element
    222                  yield self.chars
    223          else:
    224              yield self.chars
    225          yield _str('</root>')
    226
    227 -def fileInTestDir(name):
    228      _testdir = os.path.dirname(__file__)
    229      return os.path.join(_testdir, name)
    230
    231 -def read_file(name, mode='r'):
    232      f = open(name, mode)
    233      try:
    234          data = f.read()
    235      finally:
    236          f.close()
    237      return data
    238
    239 -def write_to_file(name, data, mode='w'):
    240      f = open(name, mode)
    241      try:
    242          data = f.write(data)
    243      finally:
    244          f.close()
    245
    246 -def readFileInTestDir(name, mode='r'):
    247      return read_file(fileInTestDir(name), mode)
    248
    249 -def canonicalize(xml):
    250      tree = etree.parse(BytesIO(xml))
    251      f = BytesIO()
    252      tree.write_c14n(f)
    253      return f.getvalue()
    254
    255 -def unentitify(xml):
    256      for entity_name, value in re.findall("(&#([0-9]+);)", xml):
    257          xml = xml.replace(entity_name, unichr(int(value)))
    258      return xml
    259