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