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      unicode = str
     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 = open(filename).read()
     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 -    def _str(s, encoding="UTF-8"):
    106          return unicode(s, encoding=encoding)
    107 -    def _bytes(s, encoding="UTF-8"):
    108          return s
    109      from StringIO import StringIO
    110      BytesIO = StringIO
    111
    112      doctest_parser = doctest.DocTestParser()
    113      _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
    114      _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub
    115      _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
    116 -    def make_doctest(filename):
    117          filename = _get_caller_relative_path(filename)
    118          doctests = open(filename).read()
    119          doctests = _fix_traceback(r'\1\2', doctests)
    120          doctests = _fix_exceptions(r'\1, \2', doctests)
    121          doctests = _fix_bytes(r'\1\2', doctests)
    122          return doctest.DocTestCase(
    123              doctest_parser.get_doctest(
    124                  doctests, {}, os.path.basename(filename), filename, 0))
    125
    126 -class HelperTestCase(unittest.TestCase):
    127 -    def tearDown(self):
    128          gc.collect()
    129
    130 -    def parse(self, text, parser=None):
    131          f = BytesIO(text)
    132          return etree.parse(f, parser=parser)
    133
    134 -    def _rootstring(self, tree):
    135          return etree.tostring(tree.getroot()).replace(
    136              _bytes(' '), _bytes('')).replace(_bytes('\n'), _bytes(''))
    137
    138      # assertFalse doesn't exist in Python 2.3
    139      try:
    140          unittest.TestCase.assertFalse
    141      except AttributeError:
    142          assertFalse = unittest.TestCase.failIf
    143
    144 -class SillyFileLike:
    145 -    def __init__(self, xml_data=_bytes('<foo><bar/></foo>')):
    146          self.xml_data = xml_data
    147
    148 -    def read(self, amount=None):
    149          if self.xml_data:
    150              if amount:
    151                  data = self.xml_data[:amount]
    152                  self.xml_data = self.xml_data[amount:]
    153              else:
    154                  data = self.xml_data
    155                  self.xml_data = _bytes('')
    156              return data
    157          return _bytes('')
    158
    159 -class LargeFileLike:
    160 -    def __init__(self, charlen=100, depth=4, children=5):
    161          self.data = BytesIO()
    162          self.chars  = _bytes('a') * charlen
    163          self.children = range(children)
    164          self.more = self.iterelements(depth)
    165
    166 -    def iterelements(self, depth):
    167          yield _bytes('<root>')
    168          depth -= 1
    169          if depth > 0:
    170              for child in self.children:
    171                  for element in self.iterelements(depth):
    172                      yield element
    173                  yield self.chars
    174          else:
    175              yield self.chars
    176          yield _bytes('</root>')
    177
    178 -    def read(self, amount=None):
    179          data = self.data
    180          append = data.write
    181          if amount:
    182              for element in self.more:
    183                  append(element)
    184                  if data.tell() >= amount:
    185                      break
    186          else:
    187              for element in self.more:
    188                  append(element)
    189          result = data.getvalue()
    190          data.seek(0)
    191          data.truncate()
    192          if amount:
    193              append(result[amount:])
    194              result = result[:amount]
    195          return result
    196
    197 -class LargeFileLikeUnicode(LargeFileLike):
    198 -    def __init__(self, charlen=100, depth=4, children=5):
    199          LargeFileLike.__init__(self, charlen, depth, children)
    200          self.data = StringIO()
    201          self.chars  = _str('a') * charlen
    202          self.more = self.iterelements(depth)
    203
    204 -    def iterelements(self, depth):
    205          yield _str('<root>')
    206          depth -= 1
    207          if depth > 0:
    208              for child in self.children:
    209                  for element in self.iterelements(depth):
    210                      yield element
    211                  yield self.chars
    212          else:
    213              yield self.chars
    214          yield _str('</root>')
    215
    216 -def fileInTestDir(name):
    217      _testdir = os.path.dirname(__file__)
    218      return os.path.join(_testdir, name)
    219
    220 -def canonicalize(xml):
    221      f = BytesIO(xml)
    222      tree = etree.parse(f)
    223      f = BytesIO()
    224      tree.write_c14n(f)
    225      return f.getvalue()
    226
    227 -def unentitify(xml):
    228      for entity_name, value in re.findall("(&#([0-9]+);)", xml):
    229          xml = xml.replace(entity_name, unichr(int(value)))
    230      return xml
    231