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, _str, _bytes
15 from common_imports import SillyFileLike, LargeFileLike, HelperTestCase
16 from common_imports import read_file, write_to_file, BytesIO
17
18 if sys.version_info < (2,6):
20 - def __init__(self, delete=True, **kwargs):
21 self._tmpfile = tempfile.NamedTemporaryFile(**kwargs)
25 return getattr(self._tmpfile, name)
26 else:
27 NamedTemporaryFile = tempfile.NamedTemporaryFile
28
29
31 """(c)ElementTree compatibility for IO functions/methods
32 """
33 etree = None
34
42
44 gc.collect()
45 shutil.rmtree(self._temp_dir)
46
48 return os.path.join(self._temp_dir, name)
49
59
61 Element = self.etree.Element
62 ElementTree = self.etree.ElementTree
63
64 element = Element('top')
65 element.text = _str("qwrtioüöä\uAABB")
66 tree = ElementTree(element)
67 self.buildNodes(element, 10, 3)
68 f = open(self.getTestFilePath('testdump.xml'), 'wb')
69 tree.write(f, encoding='UTF-8')
70 f.close()
71 f = open(self.getTestFilePath('testdump.xml'), 'rb')
72 tree = ElementTree(file=f)
73 f.close()
74 f = open(self.getTestFilePath('testdump2.xml'), 'wb')
75 tree.write(f, encoding='UTF-8')
76 f.close()
77 f = open(self.getTestFilePath('testdump.xml'), 'rb')
78 data1 = f.read()
79 f.close()
80 f = open(self.getTestFilePath('testdump2.xml'), 'rb')
81 data2 = f.read()
82 f.close()
83 self.assertEqual(data1, data2)
84
86 Element = self.etree.Element
87 ElementTree = self.etree.ElementTree
88
89 element = Element('top')
90 element.text = _str("qwrtioüöäßá")
91 tree = ElementTree(element)
92 self.buildNodes(element, 10, 3)
93 f = open(self.getTestFilePath('testdump.xml'), 'wb')
94 tree.write(f, encoding='iso-8859-1')
95 f.close()
96 f = open(self.getTestFilePath('testdump.xml'), 'rb')
97 tree = ElementTree(file=f)
98 f.close()
99 f = open(self.getTestFilePath('testdump2.xml'), 'wb')
100 tree.write(f, encoding='iso-8859-1')
101 f.close()
102 f = open(self.getTestFilePath('testdump.xml'), 'rb')
103 data1 = f.read()
104 f.close()
105 f = open(self.getTestFilePath('testdump2.xml'), 'rb')
106 data2 = f.read()
107 f.close()
108 self.assertEqual(data1, data2)
109
121
123 filename = os.path.join(
124 os.path.join('hopefullynonexistingpathname'),
125 'invalid_file.xml')
126 try:
127 self.tree.write(filename)
128 except IOError:
129 pass
130 else:
131 self.assertTrue(
132 False, "writing to an invalid file path should fail")
133
148
164
185
203
212
219
221 class LocalError(Exception):
222 pass
223 class TestFile:
224 def read(*args):
225 raise LocalError
226 f = TestFile()
227 self.assertRaises(LocalError, self.etree.parse, f)
228
230 class LocalError(Exception):
231 pass
232 class TestFile:
233 data = '<root>test</'
234 try:
235 next_char = iter(data).next
236 except AttributeError:
237
238 next_char = iter(data).__next__
239 counter = 0
240 def read(self, amount=None):
241 if amount is None:
242 while True:
243 self.read(1)
244 else:
245 try:
246 self.counter += 1
247 return _bytes(self.next_char())
248 except StopIteration:
249 raise LocalError
250 f = TestFile()
251 self.assertRaises(LocalError, self.etree.parse, f)
252 self.assertEqual(f.counter, len(f.data)+1)
253
255 class TestFile:
256 def read(*args):
257 return 1
258 f = TestFile()
259
260 try:
261 expect_exc = (TypeError, self.etree.ParseError)
262 except AttributeError:
263 expect_exc = TypeError
264 self.assertRaises(expect_exc, self.etree.parse, f)
265
267
268 dirnameEN = _str('Directory')
269 dirnameRU = _str('Каталог')
270 filename = _str('nosuchfile.xml')
271 dn = tempfile.mkdtemp(prefix=dirnameEN)
272 try:
273 self.assertRaises(IOError, self.etree.parse, os.path.join(dn, filename))
274 finally:
275 os.rmdir(dn)
276 dn = tempfile.mkdtemp(prefix=dirnameRU)
277 try:
278 self.assertRaises(IOError, self.etree.parse, os.path.join(dn, filename))
279 finally:
280 os.rmdir(dn)
281
283 utext = _str('Søk på nettet')
284 uxml = '<?xml version="1.0" encoding="UTF-8"?><p>%s</p>' % utext
285 bom = _bytes('\\xEF\\xBB\\xBF').decode(
286 "unicode_escape").encode("latin1")
287 self.assertEqual(3, len(bom))
288 f = NamedTemporaryFile(delete=False)
289 try:
290 try:
291 f.write(bom)
292 f.write(uxml.encode("utf-8"))
293 finally:
294 f.close()
295 tree = self.etree.parse(f.name)
296 finally:
297 os.unlink(f.name)
298 self.assertEqual(utext, tree.getroot().text)
299
301 utext = _str('Søk på nettet')
302 uxml = '<?xml version="1.0" encoding="UTF-8"?><p>%s</p>' % utext
303 bom = _bytes('\\xEF\\xBB\\xBF').decode(
304 "unicode_escape").encode("latin1")
305 self.assertEqual(3, len(bom))
306 f = NamedTemporaryFile(delete=False)
307 try:
308 try:
309 f.write(bom)
310 f.write(uxml.encode("utf-8"))
311 finally:
312 f.close()
313 elements = [el for _, el in self.etree.iterparse(f.name)]
314 self.assertEqual(1, len(elements))
315 root = elements[0]
316 finally:
317 os.unlink(f.name)
318 self.assertEqual(utext, root.text)
319
321 utext = _str('Søk på nettet')
322 uxml = '<?xml version="1.0" encoding="UTF-16"?><p>%s</p>' % utext
323 boms = _bytes('\\xFE\\xFF \\xFF\\xFE').decode(
324 "unicode_escape").encode("latin1")
325 self.assertEqual(5, len(boms))
326 xml = uxml.encode("utf-16")
327 self.assertTrue(xml[:2] in boms, repr(xml[:2]))
328
329 f = NamedTemporaryFile(delete=False)
330 try:
331 try:
332 f.write(xml)
333 finally:
334 f.close()
335 elements = [el for _, el in self.etree.iterparse(f.name)]
336 self.assertEqual(1, len(elements))
337 root = elements[0]
338 finally:
339 os.unlink(f.name)
340 self.assertEqual(utext, root.text)
341
342
370
371
372 if ElementTree:
375
376
383
384
385 if __name__ == '__main__':
386 print('to test use test.py %s' % __file__)
387