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

Source Code for Module lxml.tests.test_io

  1  # -*- coding: utf-8 -*- 
  2   
  3  """ 
  4  IO test cases that apply to both etree and ElementTree 
  5  """ 
  6   
  7  import unittest 
  8  import tempfile, gzip, os, os.path, sys, gc, shutil 
  9   
 10  this_dir = os.path.dirname(__file__) 
 11  if this_dir not in sys.path: 
 12      sys.path.insert(0, this_dir) # needed for Py3 
 13   
 14  from common_imports import etree, ElementTree, fileInTestDir, _str, _bytes 
 15  from common_imports import SillyFileLike, LargeFileLike, HelperTestCase 
 16   
17 -class IOTestCaseBase(HelperTestCase):
18 """(c)ElementTree compatibility for IO functions/methods 19 """ 20 etree = None 21
22 - def setUp(self):
23 """Setting up a minimal tree 24 """ 25 self.root = self.etree.Element('a') 26 self.root_str = self.etree.tostring(self.root) 27 self.tree = self.etree.ElementTree(self.root) 28 self._temp_dir = tempfile.mkdtemp()
29
30 - def tearDown(self):
31 gc.collect() 32 shutil.rmtree(self._temp_dir)
33
34 - def getTestFilePath(self, name):
35 return os.path.join(self._temp_dir, name)
36
37 - def buildNodes(self, element, children, depth):
38 Element = self.etree.Element 39 40 if depth == 0: 41 return 42 for i in range(children): 43 new_element = Element('element_%s_%s' % (depth, i)) 44 self.buildNodes(new_element, children, depth - 1) 45 element.append(new_element)
46
47 - def test_tree_io(self):
48 Element = self.etree.Element 49 ElementTree = self.etree.ElementTree 50 51 element = Element('top') 52 element.text = _str("qwrtioüöä\uAABB") 53 tree = ElementTree(element) 54 self.buildNodes(element, 10, 3) 55 f = open(self.getTestFilePath('testdump.xml'), 'wb') 56 tree.write(f, encoding='UTF-8') 57 f.close() 58 f = open(self.getTestFilePath('testdump.xml'), 'rb') 59 tree = ElementTree(file=f) 60 f.close() 61 f = open(self.getTestFilePath('testdump2.xml'), 'wb') 62 tree.write(f, encoding='UTF-8') 63 f.close() 64 f = open(self.getTestFilePath('testdump.xml'), 'rb') 65 data1 = f.read() 66 f.close() 67 f = open(self.getTestFilePath('testdump2.xml'), 'rb') 68 data2 = f.read() 69 f.close() 70 self.assertEquals(data1, data2)
71
72 - def test_tree_io_latin1(self):
73 Element = self.etree.Element 74 ElementTree = self.etree.ElementTree 75 76 element = Element('top') 77 element.text = _str("qwrtioüöäßá") 78 tree = ElementTree(element) 79 self.buildNodes(element, 10, 3) 80 f = open(self.getTestFilePath('testdump.xml'), 'wb') 81 tree.write(f, encoding='iso-8859-1') 82 f.close() 83 f = open(self.getTestFilePath('testdump.xml'), 'rb') 84 tree = ElementTree(file=f) 85 f.close() 86 f = open(self.getTestFilePath('testdump2.xml'), 'wb') 87 tree.write(f, encoding='iso-8859-1') 88 f.close() 89 f = open(self.getTestFilePath('testdump.xml'), 'rb') 90 data1 = f.read() 91 f.close() 92 f = open(self.getTestFilePath('testdump2.xml'), 'rb') 93 data2 = f.read() 94 f.close() 95 self.assertEquals(data1, data2)
96
97 - def test_write_filename(self):
98 # (c)ElementTree supports filename strings as write argument 99 100 handle, filename = tempfile.mkstemp(suffix=".xml") 101 self.tree.write(filename) 102 try: 103 self.assertEqual(open(filename, 'rb').read().replace(_bytes('\n'), _bytes('')), 104 self.root_str) 105 finally: 106 os.close(handle) 107 os.remove(filename)
108
110 # (c)ElementTree supports gzip instance as parse argument 111 handle, filename = tempfile.mkstemp(suffix=".xml.gz") 112 f = gzip.open(filename, 'wb') 113 f.write(self.root_str) 114 f.close() 115 try: 116 f_gz = gzip.open(filename, 'rb') 117 tree = self.etree.parse(f_gz) 118 f_gz.close() 119 self.assertEqual(self.etree.tostring(tree.getroot()), self.root_str) 120 finally: 121 os.close(handle) 122 os.remove(filename)
123
125 # (c)ElementTree class ElementTree has a 'parse' method that returns 126 # the root of the tree 127 128 # parse from filename 129 130 handle, filename = tempfile.mkstemp(suffix=".xml") 131 open(filename, 'wb').write(self.root_str) 132 try: 133 tree = self.etree.ElementTree() 134 root = tree.parse(filename) 135 self.assertEqual(self.etree.tostring(root), self.root_str) 136 finally: 137 os.close(handle) 138 os.remove(filename)
139
141 handle, filename = tempfile.mkstemp(suffix=".xml") 142 open(filename, 'wb').write(self.root_str) 143 try: 144 tree = self.etree.ElementTree() 145 root = tree.parse(filename) 146 # and now do it again; previous content should still be there 147 root2 = tree.parse(filename) 148 self.assertEquals('a', root.tag) 149 self.assertEquals('a', root2.tag) 150 # now remove all references to root2, and parse again 151 del root2 152 root3 = tree.parse(filename) 153 self.assertEquals('a', root.tag) 154 self.assertEquals('a', root3.tag) 155 # root2's memory should've been freed here 156 # XXX how to check? 157 finally: 158 os.close(handle) 159 os.remove(filename)
160
162 # (c)ElementTree class ElementTree has a 'parse' method that returns 163 # the root of the tree 164 165 # parse from file object 166 167 handle, filename = tempfile.mkstemp(suffix=".xml") 168 os.write(handle, self.root_str) 169 try: 170 f = open(filename, 'rb') 171 tree = self.etree.ElementTree() 172 root = tree.parse(f) 173 f.close() 174 self.assertEqual(self.etree.tostring(root), self.root_str) 175 finally: 176 os.close(handle) 177 os.remove(filename)
178
180 # (c)ElementTree class ElementTree has a 'parse' method that returns 181 # the root of the tree 182 183 # parse from unamed file object 184 f = SillyFileLike() 185 root = self.etree.ElementTree().parse(f) 186 self.assert_(root.tag.endswith('foo'))
187
189 # parse from unamed file object 190 f = LargeFileLike() 191 tree = self.etree.parse(f) 192 root = tree.getroot() 193 self.assert_(root.tag.endswith('root'))
194
196 class LocalError(Exception): 197 pass
198 class TestFile: 199 def read(*args): 200 raise LocalError
201 f = TestFile() 202 self.assertRaises(LocalError, self.etree.parse, f) 203
204 - def test_module_parse_fileobject_late_error(self):
205 class LocalError(Exception): 206 pass
207 class TestFile: 208 data = '<root>test</' 209 try: 210 next_char = iter(data).next 211 except AttributeError: 212 # Python 3 213 next_char = iter(data).__next__ 214 counter = 0 215 def read(self, amount=None): 216 if amount is None: 217 while True: 218 self.read(1) 219 else: 220 try: 221 self.counter += 1 222 return _bytes(self.next_char()) 223 except StopIteration: 224 raise LocalError 225 f = TestFile() 226 self.assertRaises(LocalError, self.etree.parse, f) 227 self.assertEquals(f.counter, len(f.data)+1) 228
229 - def test_module_parse_fileobject_type_error(self):
230 class TestFile: 231 def read(*args): 232 return 1
233 f = TestFile() 234 self.assertRaises(TypeError, self.etree.parse, f) 235 236
237 -class ETreeIOTestCase(IOTestCaseBase):
238 etree = etree
239 240 if ElementTree:
241 - class ElementTreeIOTestCase(IOTestCaseBase):
242 etree = ElementTree
243
244 -def test_suite():
245 suite = unittest.TestSuite() 246 suite.addTests([unittest.makeSuite(ETreeIOTestCase)]) 247 if ElementTree: 248 suite.addTests([unittest.makeSuite(ElementTreeIOTestCase)]) 249 return suite
250 251 if __name__ == '__main__': 252 print('to test use test.py %s' % __file__) 253