Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix calling bytes on serializer. #18

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions repoze/xmliter/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
doctype_re_u = re.compile(u"^<!DOCTYPE\\s[^>]+>\\s*", re.MULTILINE)

class XMLSerializer(object):

def __init__(self, tree, serializer=None, pretty_print=False, doctype=None):
if serializer is None:
serializer = lxml.etree.tostring
Expand All @@ -21,7 +21,10 @@ def __init__(self, tree, serializer=None, pretty_print=False, doctype=None):
def serialize(self, encoding=None):
# Defer to the xsl:output settings if appropriate
if isinstance(self.tree, lxml.etree._XSLTResultTree):
result = str(self.tree)
if encoding is str:
result = str(self.tree)
else:
result = bytes(self.tree)
tseaver marked this conversation as resolved.
Show resolved Hide resolved
else:
result = self.serializer(self.tree, encoding=encoding, pretty_print=self.pretty_print)
if self.doctype is not None:
Expand All @@ -36,7 +39,7 @@ def __iter__(self):

def __str__(self):
return self.serialize(str)

def __bytes__(self):
return self.serialize()

Expand Down
34 changes: 16 additions & 18 deletions repoze/xmliter/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import lxml.html
import lxml.etree

import sys
if sys.version_info > (3,):
unicode = str

class TestIterator(unittest.TestCase):

Expand All @@ -34,7 +31,7 @@ def test_html_serialization(self):
"""Test HTML serialization."""

@decorator.lazy(serializer=lxml.html.tostring)
def app(a, b, c=u""):
def app(a, b, c=""):
tree = self.create_tree()
tree.find('body').attrib['class'] = " ".join((a, b, c))
return tree
Expand All @@ -48,13 +45,13 @@ def app(a, b, c=u""):
# With Unicode encoding:
self.assertEqual(
lxml.html.tostring(result.tree, encoding='unicode'),
u"".join(result.serialize(encoding=unicode)))
"".join(result.serialize(encoding=str)))

def test_xml_serialization(self):
"""Test XML serialization."""

@decorator.lazy
def app(a, b, c=u""):
def app(a, b, c=""):
tree = self.create_tree()
tree.find('body').attrib['class'] = " ".join((a, b, c))
return tree
Expand All @@ -68,7 +65,7 @@ def app(a, b, c=u""):
# With Unicode encoding:
self.assertEqual(
lxml.etree.tostring(result.tree, encoding='unicode'),
u"".join(result.serialize(encoding=unicode)))
"".join(result.serialize(encoding=str)))

def test_decorator_instancemethod(self):
class test(object):
Expand All @@ -86,7 +83,7 @@ def __call__(self, tree):

self.assertEqual(
lxml.etree.tostring(result.tree, encoding='unicode'),
u"".join(result.serialize(encoding=unicode)))
"".join(result.serialize(encoding=str)))

def test_getXMLSerializer(self):
t = utils.getXMLSerializer(self.create_iterable())
Expand All @@ -100,8 +97,8 @@ def test_getXMLSerializer(self):
b"".join(t2))

self.assertEqual(
u"<html><head><title>My homepage</title></head><body>Hello, wörld!</body></html>",
u"".join(t2.serialize(encoding=unicode)))
"<html><head><title>My homepage</title></head><body>Hello, wörld!</body></html>",
"".join(t2.serialize(encoding=str)))

def test_length(self):
t = utils.getXMLSerializer(self.create_iterable())
Expand All @@ -120,8 +117,8 @@ def test_getHTMLSerializer(self):
b"".join(t2).strip())

self.assertEqual(
u'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">\n<html>\n<head><title>My homepage</title></head>\n<body>Hello, wörld!<img src="foo.png">\n</body>\n</html>',
u"".join(t2.serialize(encoding=unicode)).strip())
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">\n<html>\n<head><title>My homepage</title></head>\n<body>Hello, wörld!<img src="foo.png">\n</body>\n</html>',
"".join(t2.serialize(encoding=str)).strip())

def test_getHTMLSerializer_doctype_xhtml_serializes_to_xhtml(self):
t = utils.getHTMLSerializer(self.create_iterable(preamble='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n', body='<img src="foo.png" />'), pretty_print=True)
Expand All @@ -135,8 +132,8 @@ def test_getHTMLSerializer_doctype_xhtml_serializes_to_xhtml(self):
b"".join(t2).strip())

self.assertEqual(
u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n <head>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n <title>My homepage</title>\n </head>\n <body>Hello, wörld!<img src="foo.png" /></body>\n</html>',
u"".join(t2.serialize(encoding=unicode)).strip())
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n <head>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n <title>My homepage</title>\n </head>\n <body>Hello, wörld!<img src="foo.png" /></body>\n</html>',
"".join(t2.serialize(encoding=str)).strip())

def test_xsl(self):
t = utils.getHTMLSerializer(self.create_iterable(body='<br>'))
Expand All @@ -156,6 +153,7 @@ def test_xsl(self):
'''))
t.tree = transform(t.tree)
self.assertTrue('<br />' in str(t))
self.assertTrue(b'<br />' in bytes(t))

def test_replace_doctype(self):
t = utils.getHTMLSerializer(self.create_iterable(preamble='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n', body='<img src="foo.png" />'), pretty_print=True, doctype="<!DOCTYPE html>")
Expand All @@ -169,8 +167,8 @@ def test_replace_doctype(self):
b"".join(t2).strip())

self.assertEqual(
u'<!DOCTYPE html>\n<html xmlns="http://www.w3.org/1999/xhtml">\n <head>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n <title>My homepage</title>\n </head>\n <body>Hello, wörld!<img src="foo.png" /></body>\n</html>',
u"".join(t2.serialize(encoding=unicode)).strip())
'<!DOCTYPE html>\n<html xmlns="http://www.w3.org/1999/xhtml">\n <head>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n <title>My homepage</title>\n </head>\n <body>Hello, wörld!<img src="foo.png" /></body>\n</html>',
"".join(t2.serialize(encoding=str)).strip())

def test_replace_doctype_blank(self):
t = utils.getHTMLSerializer(self.create_iterable(preamble='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n', body='<img src="foo.png" />'), pretty_print=True, doctype="")
Expand All @@ -184,5 +182,5 @@ def test_replace_doctype_blank(self):
b"".join(t2).strip())

self.assertEqual(
u'<html xmlns="http://www.w3.org/1999/xhtml">\n <head>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n <title>My homepage</title>\n </head>\n <body>Hello, wörld!<img src="foo.png" /></body>\n</html>',
u"".join(t2.serialize(encoding=unicode)).strip())
'<html xmlns="http://www.w3.org/1999/xhtml">\n <head>\n <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\n <title>My homepage</title>\n </head>\n <body>Hello, wörld!<img src="foo.png" /></body>\n</html>',
"".join(t2.serialize(encoding=str)).strip())