def test_pickle(self): markup = Markup('foo') buf = BytesIO() pickle.dump(markup, buf, 2) buf.seek(0) expected_foo = "u'foo'" if IS_PYTHON2 else "'foo'" self.assertEqual("<Markup %s>" % expected_foo, repr(pickle.load(buf)))
def test_pickle(self): expr = Expression('1 < 2') buf = BytesIO() pickle.dump(expr, buf, 2) buf.seek(0) unpickled = pickle.load(buf) assert unpickled.evaluate({}) is True
def test_pickle(self): attrs = Attrs([("attr1", "foo"), ("attr2", "bar")]) buf = BytesIO() pickle.dump(attrs, buf, 2) buf.seek(0) unpickled = pickle.load(buf) self.assertEquals("Attrs([('attr1', 'foo'), ('attr2', 'bar')])", repr(unpickled))
def test_pickle(self): xml = XML('<li>Foo</li>') buf = BytesIO() pickle.dump(xml, buf, 2) buf.seek(0) xml = pickle.load(buf) self.assertEquals('<li>Foo</li>', xml.render(encoding=None))
def test_pickle(self): stream = XML('<root>$var</root>') tmpl = MarkupTemplate(stream) buf = BytesIO() pickle.dump(tmpl, buf, 2) buf.seek(0) unpickled = pickle.load(buf) self.assertEqual('<root>42</root>', str(unpickled.generate(var=42)))
def test_pickle(self): ns = Namespace("http://www.example.org/namespace") buf = BytesIO() pickle.dump(ns, buf, 2) buf.seek(0) unpickled = pickle.load(buf) self.assertEquals("Namespace('http://www.example.org/namespace')", repr(unpickled)) self.assertEquals("http://www.example.org/namespace", unpickled.uri)
def test_pickle(self): qname = QName("http://www.example.org/namespace}elem") buf = BytesIO() pickle.dump(qname, buf, 2) buf.seek(0) unpickled = pickle.load(buf) self.assertEquals("{http://www.example.org/namespace}elem", unpickled) self.assertEquals("http://www.example.org/namespace", unpickled.namespace) self.assertEquals("elem", unpickled.localname)
def test_pickle(self): suite = Suite('foo = 42') buf = BytesIO() pickle.dump(suite, buf, 2) buf.seek(0) unpickled = pickle.load(buf) data = {} unpickled.execute(data) self.assertEqual(42, data['foo'])
def test_pickle(self): ns = Namespace('http://www.example.org/namespace') buf = BytesIO() pickle.dump(ns, buf, 2) buf.seek(0) unpickled = pickle.load(buf) self.assertEquals("Namespace('http://www.example.org/namespace')", repr(unpickled)) self.assertEquals('http://www.example.org/namespace', unpickled.uri)
def test_pickle(self): qname = QName('http://www.example.org/namespace}elem') buf = BytesIO() pickle.dump(qname, buf, 2) buf.seek(0) unpickled = pickle.load(buf) self.assertEquals('{http://www.example.org/namespace}elem', unpickled) self.assertEquals('http://www.example.org/namespace', unpickled.namespace) self.assertEquals('elem', unpickled.localname)
def HTML(text, encoding=None): """Parse the given HTML source and return a markup stream. Unlike with `HTMLParser`, the returned stream is reusable, meaning it can be iterated over multiple times: >>> html = HTML('<body><h1>Foo</h1></body>', encoding='utf-8') >>> print(html) <body><h1>Foo</h1></body> >>> print((html.select('h1'))) <h1>Foo</h1> >>> print((html.select('h1/text()'))) Foo :param text: the HTML source :return: the parsed XML event stream :raises ParseError: if the HTML text is not well-formed, and error recovery fails """ if isinstance(text, str): # If it's unicode text the encoding should be set to None. # The option to pass in an incorrect encoding is for ease # of writing doctests that work in both Python 2.x and 3.x. return Stream(list(HTMLParser(StringIO(text), encoding=None))) return Stream(list(HTMLParser(BytesIO(text), encoding=encoding)))
def test_latin1_encoded_xmldecl(self): text = """<?xml version="1.0" encoding="iso-8859-1" ?> <div>\xf6</div> """.encode('iso-8859-1') events = list(XMLParser(BytesIO(text))) kind, data, pos = events[2] self.assertEqual(Stream.TEXT, kind) self.assertEqual('\xf6', data)
def test_out_of_order_tags3(self): text = '<span><b>Foobar</i>'.encode('utf-8') events = list(HTMLParser(BytesIO(text), encoding='utf-8')) self.assertEqual(5, len(events)) self.assertEqual((Stream.START, ('span', ())), events[0][:2]) self.assertEqual((Stream.START, ('b', ())), events[1][:2]) self.assertEqual((Stream.TEXT, 'Foobar'), events[2][:2]) self.assertEqual((Stream.END, 'b'), events[3][:2]) self.assertEqual((Stream.END, 'span'), events[4][:2])
def test_out_of_order_tags2(self): text = '<span class="baz"><b><i>Foobar</span></b></i>'.encode('utf-8') events = list(HTMLParser(BytesIO(text), encoding='utf-8')) self.assertEqual(7, len(events)) self.assertEqual((Stream.START, ('span', Attrs([('class', 'baz')]))), events[0][:2]) self.assertEqual((Stream.START, ('b', ())), events[1][:2]) self.assertEqual((Stream.START, ('i', ())), events[2][:2]) self.assertEqual((Stream.TEXT, 'Foobar'), events[3][:2]) self.assertEqual((Stream.END, 'i'), events[4][:2]) self.assertEqual((Stream.END, 'b'), events[5][:2]) self.assertEqual((Stream.END, 'span'), events[6][:2])
def __init__(self, source, filepath=None, filename=None, loader=None, encoding=None, lookup='strict', allow_exec=True): """Initialize a template from either a string, a file-like object, or an already parsed markup stream. :param source: a string, file-like object, or markup stream to read the template from :param filepath: the absolute path to the template file :param filename: the path to the template file relative to the search path :param loader: the `TemplateLoader` to use for loading included templates :param encoding: the encoding of the `source` :param lookup: the variable lookup mechanism; either "strict" (the default), "lenient", or a custom lookup class :param allow_exec: whether Python code blocks in templates should be allowed :note: Changed in 0.5: Added the `allow_exec` argument """ self.filepath = filepath or filename self.filename = filename self.loader = loader self.lookup = lookup self.allow_exec = allow_exec self._init_filters() self._init_loader() self._prepared = False if not isinstance(source, Stream) and not hasattr(source, 'read'): if isinstance(source, str): source = StringIO(source) else: source = BytesIO(source) try: self._stream = self._parse(source, encoding) except ParseError as e: raise TemplateSyntaxError(e.msg, self.filepath, e.lineno, e.offset)
def HTML(text, encoding=None): """Parse the given HTML source and return a markup stream. Unlike with `HTMLParser`, the returned stream is reusable, meaning it can be iterated over multiple times: >>> html = HTML('<body><h1>Foo</h1></body>', encoding='utf-8') >>> print(html) <body><h1>Foo</h1></body> >>> print(html.select('h1')) <h1>Foo</h1> >>> print(html.select('h1/text()')) Foo :param text: the HTML source :return: the parsed XML event stream :raises ParseError: if the HTML text is not well-formed, and error recovery fails """ if isinstance(text, str): return Stream(list(HTMLParser(StringIO(text), encoding=encoding))) return Stream(list(HTMLParser(BytesIO(text), encoding=encoding)))
def _build_foreign(self, context, base, sysid, pubid): parser = self.expat.ExternalEntityParserCreate(context) parser.ParseFile(BytesIO(self._external_dtd)) return 1
def test_multibyte_character_on_chunk_boundary(self): text = u'a' * ((4 * 1024) - 1) + u'\xe6' events = list(HTMLParser(BytesIO(text.encode('utf-8')), encoding='utf-8')) self.assertEqual(1, len(events)) self.assertEqual((Stream.TEXT, text), events[0][:2])
def test_render_output_stream_utf8(self): xml = XML("<li>Über uns</li>") strio = BytesIO() self.assertEqual(None, xml.render(encoding="utf-8", out=strio)) self.assertEqual(u"<li>Über uns</li>".encode("utf-8"), strio.getvalue())
def test_pickle(self): markup = Markup('foo') buf = BytesIO() pickle.dump(markup, buf, 2) buf.seek(0) self.assertEquals("<Markup u'foo'>", repr(pickle.load(buf)))
def test_input_encoding_attribute(self): text = '<div title="\xf6"></div>'.encode('iso-8859-1') events = list(HTMLParser(BytesIO(text), encoding='iso-8859-1')) kind, (tag, attrib), pos = events[0] self.assertEqual(Stream.START, kind) self.assertEqual('\xf6', attrib.get('title'))
def test_render_output_stream_utf8(self): xml = XML('<li>Über uns</li>') strio = BytesIO() self.assertEqual(None, xml.render(encoding='utf-8', out=strio)) self.assertEqual('<li>Über uns</li>'.encode('utf-8'), strio.getvalue())
def test_latin1_encoded(self): text = '<div>\xf6</div>'.encode('iso-8859-1') events = list(XMLParser(BytesIO(text), encoding='iso-8859-1')) kind, data, pos = events[1] self.assertEqual(Stream.TEXT, kind) self.assertEqual('\xf6', data)
def test_input_encoding_text(self): text = u'<div>\xf6</div>'.encode('iso-8859-1') events = list(HTMLParser(BytesIO(text), encoding='iso-8859-1')) kind, data, pos = events[1] self.assertEqual(Stream.TEXT, kind) self.assertEqual(u'\xf6', data)
def test_render_output_stream_utf8(self): xml = XML('<li>Über uns</li>') strio = BytesIO() self.assertEqual(None, xml.render(encoding='utf-8', out=strio)) self.assertEqual(u'<li>Über uns</li>'.encode('utf-8'), strio.getvalue())