1
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)
13
14 from common_imports import etree, ElementTree, fileInTestDir, _str, _bytes
15 from common_imports import SillyFileLike, LargeFileLike, HelperTestCase
16 from common_imports import read_file, write_to_file
17
19 """(c)ElementTree compatibility for IO functions/methods
20 """
21 etree = None
22
30
32 gc.collect()
33 shutil.rmtree(self._temp_dir)
34
37
39 Element = self.etree.Element
40
41 if depth == 0:
42 return
43 for i in range(children):
44 new_element = Element('element_%s_%s' % (depth, i))
45 self.buildNodes(new_element, children, depth - 1)
46 element.append(new_element)
47
49 Element = self.etree.Element
50 ElementTree = self.etree.ElementTree
51
52 element = Element('top')
53 element.text = _str("qwrtioüöä\uAABB")
54 tree = ElementTree(element)
55 self.buildNodes(element, 10, 3)
56 f = open(self.getTestFilePath('testdump.xml'), 'wb')
57 tree.write(f, encoding='UTF-8')
58 f.close()
59 f = open(self.getTestFilePath('testdump.xml'), 'rb')
60 tree = ElementTree(file=f)
61 f.close()
62 f = open(self.getTestFilePath('testdump2.xml'), 'wb')
63 tree.write(f, encoding='UTF-8')
64 f.close()
65 f = open(self.getTestFilePath('testdump.xml'), 'rb')
66 data1 = f.read()
67 f.close()
68 f = open(self.getTestFilePath('testdump2.xml'), 'rb')
69 data2 = f.read()
70 f.close()
71 self.assertEquals(data1, data2)
72
74 Element = self.etree.Element
75 ElementTree = self.etree.ElementTree
76
77 element = Element('top')
78 element.text = _str("qwrtioüöäßá")
79 tree = ElementTree(element)
80 self.buildNodes(element, 10, 3)
81 f = open(self.getTestFilePath('testdump.xml'), 'wb')
82 tree.write(f, encoding='iso-8859-1')
83 f.close()
84 f = open(self.getTestFilePath('testdump.xml'), 'rb')
85 tree = ElementTree(file=f)
86 f.close()
87 f = open(self.getTestFilePath('testdump2.xml'), 'wb')
88 tree.write(f, encoding='iso-8859-1')
89 f.close()
90 f = open(self.getTestFilePath('testdump.xml'), 'rb')
91 data1 = f.read()
92 f.close()
93 f = open(self.getTestFilePath('testdump2.xml'), 'rb')
94 data2 = f.read()
95 f.close()
96 self.assertEquals(data1, data2)
97
109
111 filename = os.path.join(
112 os.path.join('hopefullynonexistingpathname'),
113 'invalid_file.xml')
114 try:
115 self.tree.write(filename)
116 except IOError:
117 pass
118 else:
119 self.assertTrue(
120 False, "writing to an invalid file path should fail")
121
136
152
173
191
200
207
209 class LocalError(Exception):
210 pass
211 class TestFile:
212 def read(*args):
213 raise LocalError
214 f = TestFile()
215 self.assertRaises(LocalError, self.etree.parse, f)
216
218 class LocalError(Exception):
219 pass
220 class TestFile:
221 data = '<root>test</'
222 try:
223 next_char = iter(data).next
224 except AttributeError:
225
226 next_char = iter(data).__next__
227 counter = 0
228 def read(self, amount=None):
229 if amount is None:
230 while True:
231 self.read(1)
232 else:
233 try:
234 self.counter += 1
235 return _bytes(self.next_char())
236 except StopIteration:
237 raise LocalError
238 f = TestFile()
239 self.assertRaises(LocalError, self.etree.parse, f)
240 self.assertEquals(f.counter, len(f.data)+1)
241
243 class TestFile:
244 def read(*args):
245 return 1
246 f = TestFile()
247 self.assertRaises(TypeError, self.etree.parse, f)
248
249
252
253 if ElementTree:
256
263
264 if __name__ == '__main__':
265 print('to test use test.py %s' % __file__)
266