class SimpleFileLikeXmlFileTestCase(_XmlFileTestCaseBase): class SimpleFileLike(object): def __init__(self, target): self._target = target self.write = target.write self.closed = False def close(self): assert not self.closed self.closed = True self._target.close() def setUp(self): self._target = BytesIO() self._file = self.SimpleFileLike(self._target) def _read_file(self): return self._target.getvalue() def _parse_file(self): self._target.seek(0) return etree.parse(self._target) def test_filelike_not_closing(self): with etree.xmlfile(self._file) as xf: with xf.element('test'): pass self.assertFalse(self._file.closed) def test_filelike_close(self): with etree.xmlfile(self._file, close=True) as xf: with xf.element('test'): pass self.assertTrue(self._file.closed) self._file = None # prevent closing in tearDown()
def test_xslt_encoding_override(self): tree = self.parse(_bytes('<a><b>\\uF8D2</b><c>\\uF8D2</c></a>' ).decode("unicode_escape")) style = self.parse('''\ <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output encoding="UTF-8"/> <xsl:template match="/"> <foo><xsl:value-of select="/a/b/text()" /></foo> </xsl:template> </xsl:stylesheet>''') st = etree.XSLT(style) res = st(tree) expected = _bytes("""\ <?xml version='1.0' encoding='UTF-16'?>\ <foo>\\uF8D2</foo>""").decode("unicode_escape") f = BytesIO() res.write(f, encoding='UTF-16') if is_python3: result = str(f.getvalue(), 'UTF-16').replace('\n', '') else: result = unicode(str(f.getvalue()), 'UTF-16').replace('\n', '') self.assertEquals(expected, result)
class SimpleFileLikeXmlFileTestCase(_XmlFileTestCaseBase): class SimpleFileLike(object): def __init__(self, target): self._target = target self.write = target.write self.close = target.close def setUp(self): self._target = BytesIO() self._file = self.SimpleFileLike(self._target) def _read_file(self): return self._target.getvalue() def _parse_file(self): self._target.seek(0) return etree.parse(self._target)
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)
def test_module_parse_html_norecover(self): parser = self.etree.HTMLParser(recover=False) parse = self.etree.parse f = BytesIO(self.broken_html_str) self.assertRaises(self.etree.XMLSyntaxError, parse, f, parser)
def _saxify_serialize(self, tree): new_tree = self._saxify_unsaxify(tree) f = BytesIO() new_tree.write(f) return f.getvalue().replace(_bytes('\n'), _bytes(''))
class ETreeXMLSchemaResolversTestCase(HelperTestCase): resolver_schema_int = BytesIO("""\ <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:etype="http://codespeak.net/lxml/test/external" targetNamespace="http://codespeak.net/lxml/test/internal"> <xsd:import namespace="http://codespeak.net/lxml/test/external" schemaLocation="XXX.xsd" /> <xsd:element name="a" type="etype:AType"/> </xsd:schema>""") resolver_schema_int2 = BytesIO("""\ <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:etype="http://codespeak.net/lxml/test/external" targetNamespace="http://codespeak.net/lxml/test/internal"> <xsd:import namespace="http://codespeak.net/lxml/test/external" schemaLocation="YYY.xsd" /> <xsd:element name="a" type="etype:AType"/> </xsd:schema>""") resolver_schema_ext = """\ <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://codespeak.net/lxml/test/external"> <xsd:complexType name="AType"> <xsd:sequence><xsd:element name="b" type="xsd:string" minOccurs="0" maxOccurs="unbounded" /></xsd:sequence> </xsd:complexType> </xsd:schema>""" class simple_resolver(etree.Resolver): def __init__(self, schema): self.schema = schema def resolve(self, url, id, context): assert url == 'XXX.xsd' return self.resolve_string(self.schema, context) # tests: def test_xmlschema_resolvers(self): # test that resolvers work with schema. parser = etree.XMLParser() parser.resolvers.add(self.simple_resolver(self.resolver_schema_ext)) schema_doc = etree.parse(self.resolver_schema_int, parser=parser) schema = etree.XMLSchema(schema_doc) def test_xmlschema_resolvers_root(self): # test that the default resolver will get called if there's no # specific parser resolver. root_resolver = self.simple_resolver(self.resolver_schema_ext) etree.get_default_parser().resolvers.add(root_resolver) schema_doc = etree.parse(self.resolver_schema_int) schema = etree.XMLSchema(schema_doc) etree.get_default_parser().resolvers.remove(root_resolver) def test_xmlschema_resolvers_noroot(self): # test that the default resolver will not get called when a # more specific resolver is registered. class res_root(etree.Resolver): def resolve(self, url, id, context): assert False return None root_resolver = res_root() etree.get_default_parser().resolvers.add(root_resolver) parser = etree.XMLParser() parser.resolvers.add(self.simple_resolver(self.resolver_schema_ext)) schema_doc = etree.parse(self.resolver_schema_int, parser=parser) schema = etree.XMLSchema(schema_doc) etree.get_default_parser().resolvers.remove(root_resolver) def test_xmlschema_nested_resolvers(self): # test that resolvers work in a nested fashion. resolver_schema = self.resolver_schema_ext class res_nested(etree.Resolver): def __init__(self, ext_schema): self.ext_schema = ext_schema def resolve(self, url, id, context): assert url == 'YYY.xsd' return self.resolve_string(self.ext_schema, context) class res(etree.Resolver): def __init__(self, ext_schema_1, ext_schema_2): self.ext_schema_1 = ext_schema_1 self.ext_schema_2 = ext_schema_2 def resolve(self, url, id, context): assert url == 'XXX.xsd' new_parser = etree.XMLParser() new_parser.resolvers.add(res_nested(self.ext_schema_2)) new_schema_doc = etree.parse(self.ext_schema_1, parser=new_parser) new_schema = etree.XMLSchema(new_schema_doc) return self.resolve_string(resolver_schema, context) parser = etree.XMLParser() parser.resolvers.add( res(self.resolver_schema_int2, self.resolver_schema_ext)) schema_doc = etree.parse(self.resolver_schema_int, parser=parser) schema = etree.XMLSchema(schema_doc)
def run_parse(): thread_root = self.etree.parse(BytesIO(xml)).getroot() result.append(thread_root[0]) result.append(thread_root[-1])
def setUp(self): self._target = BytesIO() self._file = self.SimpleFileLike(self._target)
def test_dtd_assertValid(self): root = etree.XML("<b><a/></b>") dtd = etree.DTD(BytesIO("<!ELEMENT b (a)><!ELEMENT a EMPTY>")) dtd.assertValid(root)
def test_dtd_invalid(self): root = etree.XML("<b><a/></b>") dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>")) self.assertRaises(etree.DocumentInvalid, dtd.assertValid, root)
def test_dtd_stringio(self): root = etree.XML(_bytes("<b/>")) dtd = etree.DTD(BytesIO("<!ELEMENT b EMPTY>")) self.assertTrue(dtd.validate(root))
def test_dtd_broken(self): self.assertRaises(etree.DTDParseError, etree.DTD, BytesIO("<!ELEMENT b HONKEY>"))
def test_module_parse_html_error(self): parser = self.etree.HTMLParser(recover=False) parse = self.etree.parse f = BytesIO("<html></body>") self.assertRaises(self.etree.XMLSyntaxError, parse, f, parser)
def test_html_iterparse_broken_no_recover(self): iterparse = self.etree.iterparse f = BytesIO('<p>P<br></div>') iterator = iterparse(f, html=True, recover=False) self.assertRaises(self.etree.XMLSyntaxError, list, iterator)
def setUp(self): self._file = BytesIO()
_bytes('{http://nsa/\\uf8d2}b').decode("unicode_escape"), r[0].tag) expr = etree.ETXPath( _bytes("/a/{http://nsb/\\uf8d1}b").decode("unicode_escape")) r = expr(x) self.assertEquals(1, len(r)) self.assertEquals( _bytes('{http://nsb/\\uf8d1}b').decode("unicode_escape"), r[0].tag) SAMPLE_XML = etree.parse( BytesIO(""" <body> <tag>text</tag> <section> <tag>subtext</tag> </section> <tag /> <tag /> </body> """)) def tag(elem): return elem.tag def tag_or_value(elem): return getattr(elem, 'tag', elem) def stringTest(ctxt, s1):