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
123
139
160
178
187
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
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
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
230 class TestFile:
231 def read(*args):
232 return 1
233 f = TestFile()
234 self.assertRaises(TypeError, self.etree.parse, f)
235
236
239
240 if ElementTree:
243
250
251 if __name__ == '__main__':
252 print('to test use test.py %s' % __file__)
253