Пример #1
0
 def test_etree_parse_io_error(self):
     # this is a directory name that contains characters beyond latin-1
     dirnameEN = _str('Directory')
     dirnameRU = _str('Каталог')
     filename = _str('nosuchfile.xml')
     dn = tempfile.mkdtemp(prefix=dirnameEN)
     try:
         self.assertRaises(IOError, self.etree.parse,
                           os.path.join(dn, filename))
     finally:
         os.rmdir(dn)
     dn = tempfile.mkdtemp(prefix=dirnameRU)
     try:
         self.assertRaises(IOError, self.etree.parse,
                           os.path.join(dn, filename))
     finally:
         os.rmdir(dn)
Пример #2
0
 def test_parse_utf8_bom(self):
     utext = _str('Søk på nettet')
     uxml = '<?xml version="1.0" encoding="UTF-8"?><p>%s</p>' % utext
     bom = _bytes('\\xEF\\xBB\\xBF').decode("unicode_escape").encode(
         "latin1")
     self.assertEqual(3, len(bom))
     f = tempfile.NamedTemporaryFile(delete=False)
     try:
         try:
             f.write(bom)
             f.write(uxml.encode("utf-8"))
         finally:
             f.close()
         tree = self.etree.parse(f.name)
     finally:
         os.unlink(f.name)
     self.assertEqual(utext, tree.getroot().text)
Пример #3
0
    def test_tree_io_latin1(self):
        Element = self.etree.Element
        ElementTree = self.etree.ElementTree

        element = Element('top')
        element.text = _str("qwrtioüöäßá")
        tree = ElementTree(element)
        self.buildNodes(element, 10, 3)
        with open(self.getTestFilePath('testdump.xml'), 'wb') as f:
            tree.write(f, encoding='iso-8859-1')
        with open(self.getTestFilePath('testdump.xml'), 'rb') as f:
            tree = ElementTree(file=f)
        with open(self.getTestFilePath('testdump2.xml'), 'wb') as f:
            tree.write(f, encoding='iso-8859-1')
        with open(self.getTestFilePath('testdump.xml'), 'rb') as f:
            data1 = f.read()
        with open(self.getTestFilePath('testdump2.xml'), 'rb') as f:
            data2 = f.read()
        self.assertEqual(data1, data2)
Пример #4
0
    def test_iterparse_utf16_bom(self):
        utext = _str('Søk på nettet')
        uxml = '<?xml version="1.0" encoding="UTF-16"?><p>%s</p>' % utext
        boms = _bytes('\\xFE\\xFF \\xFF\\xFE').decode("unicode_escape").encode(
            "latin1")
        self.assertEqual(5, len(boms))
        xml = uxml.encode("utf-16")
        self.assertTrue(xml[:2] in boms, repr(xml[:2]))

        f = tempfile.NamedTemporaryFile(delete=False)
        try:
            try:
                f.write(xml)
            finally:
                f.close()
            elements = [el for _, el in self.etree.iterparse(f.name)]
            self.assertEqual(1, len(elements))
            root = elements[0]
        finally:
            os.unlink(f.name)
        self.assertEqual(utext, root.text)
Пример #5
0
    def test_write_compressed_text(self):
        Element = self.etree.Element
        SubElement = self.etree.SubElement
        ElementTree = self.etree.ElementTree
        text = _str("qwrtioüöä")

        root = Element('root')
        root.text = text
        child = SubElement(root, 'sub')
        child.text = 'TEXT'
        child.tail = 'TAIL'
        SubElement(root, 'sub').text = text

        tree = ElementTree(root)
        out = BytesIO()
        tree.write(out, method='text', encoding='utf8', compression=9)
        out.seek(0)

        f = gzip.GzipFile(fileobj=out)
        try:
            result = f.read().decode('utf8')
        finally:
            f.close()
        self.assertEqual(text + 'TEXTTAIL' + text, result)