lxml.tests.common_imports

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

# Source Code for Module lxml.tests.common_imports

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