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