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      if make_version_tuple(ElementTree.VERSION)[:2] < (1,3):
     26          # compatibility tests require ET 1.3+
     27          ElementTree = None
     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      if make_version_tuple(cElementTree.VERSION)[:2] <= (1,0):
     39          # compatibility tests do not run with cET 1.0.7
     40          cElementTree = None
     41
     42  try:
     43      import doctest
     44      # check if the system version has everything we need
     45      doctest.DocFileSuite
     46      doctest.DocTestParser
     47      doctest.NORMALIZE_WHITESPACE
     48      doctest.ELLIPSIS
     49  except (ImportError, AttributeError):
     50      # we need our own version to make it work (Python 2.3?)
     51      import local_doctest as doctest
     52
     53  try:
     54      sorted
     55  except NameError:
     56 -    def sorted(seq, **kwargs):
     57          seq = list(seq)
     58          seq.sort(**kwargs)
     59          return seq
     60  else:
     61      locals()['sorted'] = sorted
     62
     63 -def _get_caller_relative_path(filename, frame_depth=2):
     64      module = sys.modules[sys._getframe(frame_depth).f_globals['__name__']]
     65      return os.path.normpath(os.path.join(
     66              os.path.dirname(getattr(module, '__file__', '')), filename))
     67
     68  if sys.version_info[0] >= 3:
     69      # Python 3
     70      unicode = str
     71 -    def _str(s, encoding="UTF-8"):
     72          return s
     73 -    def _bytes(s, encoding="UTF-8"):
     74          return s.encode(encoding)
     75      from io import StringIO, BytesIO as _BytesIO
     76 -    def BytesIO(*args):
     77          if args and isinstance(args[0], str):
     78              args = (args[0].encode("UTF-8"),)
     79          return _BytesIO(*args)
     80
     81      doctest_parser = doctest.DocTestParser()
     82      _fix_unicode = re.compile(r'(\s+)u(["\'])').sub
     83      _fix_exceptions = re.compile(r'(.*except [^(]*),\s*(.*:)').sub
     84 -    def make_doctest(filename):
     85          filename = _get_caller_relative_path(filename)
     86          doctests = open(filename).read()
     87          doctests = _fix_unicode(r'\1\2', doctests)
     88          doctests = _fix_exceptions(r'\1 as \2', doctests)
     89          return doctest.DocTestCase(
     90              doctest_parser.get_doctest(
     91                  doctests, {}, os.path.basename(filename), filename, 0))
     92  else:
     93      # Python 2
     94 -    def _str(s, encoding="UTF-8"):
     95          return unicode(s, encoding=encoding)
     96 -    def _bytes(s, encoding="UTF-8"):
     97          return s
     98      from StringIO import StringIO
     99      BytesIO = StringIO
    100
    101      doctest_parser = doctest.DocTestParser()
    102      _fix_traceback = re.compile(r'^(\s*)(?:\w+\.)+(\w*(?:Error|Exception|Invalid):)', re.M).sub
    103      _fix_exceptions = re.compile(r'(.*except [^(]*)\s+as\s+(.*:)').sub
    104      _fix_bytes = re.compile(r'(\s+)b(["\'])').sub
    105 -    def make_doctest(filename):
    106          filename = _get_caller_relative_path(filename)
    107          doctests = open(filename).read()
    108          doctests = _fix_traceback(r'\1\2', doctests)
    109          doctests = _fix_exceptions(r'\1, \2', doctests)
    110          doctests = _fix_bytes(r'\1\2', doctests)
    111          return doctest.DocTestCase(
    112              doctest_parser.get_doctest(
    113                  doctests, {}, os.path.basename(filename), filename, 0))
    114
    115 -class HelperTestCase(unittest.TestCase):
    116 -    def tearDown(self):
    117          gc.collect()
    118
    119 -    def parse(self, text, parser=None):
    120          f = BytesIO(text)
    121          return etree.parse(f, parser=parser)
    122
    123 -    def _rootstring(self, tree):
    124          return etree.tostring(tree.getroot()).replace(
    125              _bytes(' '), _bytes('')).replace(_bytes('\n'), _bytes(''))
    126
    127      # assertFalse doesn't exist in Python 2.3
    128      try:
    129          unittest.TestCase.assertFalse
    130      except AttributeError:
    131          assertFalse = unittest.TestCase.failIf
    132
    133 -class SillyFileLike:
    134 -    def __init__(self, xml_data=_bytes('<foo><bar/></foo>')):
    135          self.xml_data = xml_data
    136
    137 -    def read(self, amount=None):
    138          if self.xml_data:
    139              if amount:
    140                  data = self.xml_data[:amount]
    141                  self.xml_data = self.xml_data[amount:]
    142              else:
    143                  data = self.xml_data
    144                  self.xml_data = _bytes('')
    145              return data
    146          return _bytes('')
    147
    148 -class LargeFileLike:
    149 -    def __init__(self, charlen=100, depth=4, children=5):
    150          self.data = BytesIO()
    151          self.chars  = _bytes('a') * charlen
    152          self.children = range(children)
    153          self.more = self.iterelements(depth)
    154
    155 -    def iterelements(self, depth):
    156          yield _bytes('<root>')
    157          depth -= 1
    158          if depth > 0:
    159              for child in self.children:
    160                  for element in self.iterelements(depth):
    161                      yield element
    162                  yield self.chars
    163          else:
    164              yield self.chars
    165          yield _bytes('</root>')
    166
    167 -    def read(self, amount=None):
    168          data = self.data
    169          append = data.write
    170          if amount:
    171              for element in self.more:
    172                  append(element)
    173                  if data.tell() >= amount:
    174                      break
    175          else:
    176              for element in self.more:
    177                  append(element)
    178          result = data.getvalue()
    179          data.seek(0)
    180          data.truncate()
    181          if amount:
    182              append(result[amount:])
    183              result = result[:amount]
    184          return result
    185
    186 -class LargeFileLikeUnicode(LargeFileLike):
    187 -    def __init__(self, charlen=100, depth=4, children=5):
    188          LargeFileLike.__init__(self, charlen, depth, children)
    189          self.data = StringIO()
    190          self.chars  = _str('a') * charlen
    191          self.more = self.iterelements(depth)
    192
    193 -    def iterelements(self, depth):
    194          yield _str('<root>')
    195          depth -= 1
    196          if depth > 0:
    197              for child in self.children:
    198                  for element in self.iterelements(depth):
    199                      yield element
    200                  yield self.chars
    201          else:
    202              yield self.chars
    203          yield _str('</root>')
    204
    205 -def fileInTestDir(name):
    206      _testdir = os.path.dirname(__file__)
    207      return os.path.join(_testdir, name)
    208
    209 -def canonicalize(xml):
    210      f = BytesIO(xml)
    211      tree = etree.parse(f)
    212      f = BytesIO()
    213      tree.write_c14n(f)
    214      return f.getvalue()
    215
    216 -def unentitify(xml):
    217      for entity_name, value in re.findall("(&#([0-9]+);)", xml):
    218          xml = xml.replace(entity_name, unichr(int(value)))
    219      return xml
    220