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

# Source Code for Module lxml.tests.test_external_document

      1  # -*- coding: utf-8 -*-
      2  """
      3  Test cases related to direct loading of external libxml2 documents
      4  """
      5
      6  from __future__ import absolute_import
      7
      8  import unittest
      9
     10  from .common_imports import HelperTestCase, etree
     11
     12  DOC_NAME = b'libxml2:xmlDoc'
     13  DESTRUCTOR_NAME = b'destructor:xmlFreeDoc'
     14
     15
     16 -class ExternalDocumentTestCase(HelperTestCase):
     17 -    def setUp(self):
     18          import ctypes
     19          from ctypes import pythonapi
     20          from ctypes.util import find_library
     21
     22          def wrap(func, restype, *argtypes):
     23              func.restype = restype
     24              func.argtypes = list(argtypes)
     25              return func
     26
     27          self.get_capsule_name = wrap(pythonapi.PyCapsule_GetName,
     28                                       ctypes.c_char_p, ctypes.py_object)
     29          self.capsule_is_valid = wrap(pythonapi.PyCapsule_IsValid, ctypes.c_int,
     30                                       ctypes.py_object, ctypes.c_char_p)
     31          self.new_capsule = wrap(pythonapi.PyCapsule_New, ctypes.py_object,
     32                                  ctypes.c_void_p, ctypes.c_char_p,
     33                                  ctypes.c_void_p)
     34          self.set_capsule_name = wrap(pythonapi.PyCapsule_SetName, ctypes.c_int,
     35                                       ctypes.py_object, ctypes.c_char_p)
     36          self.set_capsule_context = wrap(pythonapi.PyCapsule_SetContext,
     37                                          ctypes.c_int, ctypes.py_object,
     38                                          ctypes.c_char_p)
     39          self.get_capsule_context = wrap(pythonapi.PyCapsule_GetContext,
     40                                          ctypes.c_char_p, ctypes.py_object)
     41          self.get_capsule_pointer = wrap(pythonapi.PyCapsule_GetPointer,
     42                                          ctypes.c_void_p, ctypes.py_object,
     43                                          ctypes.c_char_p)
     44          self.set_capsule_pointer = wrap(pythonapi.PyCapsule_SetPointer,
     45                                          ctypes.c_int, ctypes.py_object,
     46                                          ctypes.c_void_p)
     47          self.set_capsule_destructor = wrap(pythonapi.PyCapsule_SetDestructor,
     48                                             ctypes.c_int, ctypes.py_object,
     49                                             ctypes.c_void_p)
     50          self.PyCapsule_Destructor = ctypes.CFUNCTYPE(None, ctypes.py_object)
     51          libxml2 = ctypes.CDLL(find_library('xml2'))
     52          self.create_doc = wrap(libxml2.xmlReadMemory, ctypes.c_void_p,
     53                                 ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p,
     54                                 ctypes.c_char_p, ctypes.c_int)
     55          self.free_doc = wrap(libxml2.xmlFreeDoc, None, ctypes.c_void_p)
     56
     57 -    def as_capsule(self, text, capsule_name=DOC_NAME):
     58          if not isinstance(text, bytes):
     59              text = text.encode('utf-8')
     60          doc = self.create_doc(text, len(text), b'base.xml', b'utf-8', 0)
     61          ans = self.new_capsule(doc, capsule_name, None)
     62          self.set_capsule_context(ans, DESTRUCTOR_NAME)
     63          return ans
     64
     65 -    def test_external_document_adoption(self):
     66          xml = '<r a="1">t</r>'
     67          self.assertRaises(TypeError, etree.adopt_external_document, None)
     68          capsule = self.as_capsule(xml)
     69          self.assertTrue(self.capsule_is_valid(capsule, DOC_NAME))
     70          self.assertEqual(DOC_NAME, self.get_capsule_name(capsule))
     71          # Create an lxml tree from the capsule (this is a move not a copy)
     72          root = etree.adopt_external_document(capsule).getroot()
     73          self.assertIsNone(self.get_capsule_name(capsule))
     74          self.assertEqual(root.text, 't')
     75          root.text = 'new text'
     76          # Now reset the capsule so we can copy it
     77          self.assertEqual(0, self.set_capsule_name(capsule, DOC_NAME))
     78          self.assertEqual(0, self.set_capsule_context(capsule, b'invalid'))
     79          # Create an lxml tree from the capsule (this is a copy not a move)
     80          root2 = etree.adopt_external_document(capsule).getroot()
     81          self.assertEqual(self.get_capsule_context(capsule), b'invalid')
     82          # Check that the modification to the tree using the transferred
     83          # document was successful
     84          self.assertEqual(root.text, root2.text)
     85          # Check that further modifications do not show up in the copy (they are
     86          # disjoint)
     87          root.text = 'other text'
     88          self.assertNotEqual(root.text, root2.text)
     89          # delete root and ensure root2 survives
     90          del root
     91          self.assertEqual(root2.text, 'new text')
     92
     93
     94 -def test_suite():
     95      suite = unittest.TestSuite()
     96      suite.addTests([unittest.makeSuite(ExternalDocumentTestCase)])
     97      return suite
     98
     99
    100  if __name__ == '__main__':
    101      print('to test use test.py %s' % __file__)
    102