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
18 """(c)ElementTree compatibility for IO functions/methods
19 """
20 etree = None
21
29
31 gc.collect()
32 shutil.rmtree(self._temp_dir)
33
36
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
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
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
108
110 filename = os.path.join(
111 os.path.join('hopefullynonexistingpathname'),
112 'invalid_file.xml')
113 try:
114 self.tree.write(filename)
115 except IOError:
116 pass
117 else:
118 self.assertTrue(
119 False, "writing to an invalid file path should fail")
120
135
151
172
190
199
206
208 class LocalError(Exception):
209 pass
210 class TestFile:
211 def read(*args):
212 raise LocalError
213 f = TestFile()
214 self.assertRaises(LocalError, self.etree.parse, f)
215
217 class LocalError(Exception):
218 pass
219 class TestFile:
220 data = '<root>test</'
221 try:
222 next_char = iter(data).next
223 except AttributeError:
224
225 next_char = iter(data).__next__
226 counter = 0
227 def read(self, amount=None):
228 if amount is None:
229 while True:
230 self.read(1)
231 else:
232 try:
233 self.counter += 1
234 return _bytes(self.next_char())
235 except StopIteration:
236 raise LocalError
237 f = TestFile()
238 self.assertRaises(LocalError, self.etree.parse, f)
239 self.assertEquals(f.counter, len(f.data)+1)
240
242 class TestFile:
243 def read(*args):
244 return 1
245 f = TestFile()
246 self.assertRaises(TypeError, self.etree.parse, f)
247
248
251
252 if ElementTree:
255
262
263 if __name__ == '__main__':
264 print('to test use test.py %s' % __file__)
265