Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

work around problems with tricky to port ET test modules selftest.py …

…and selftest2.py in Py3

--HG--
extra : rebase_source : 926ab2043a46787515030863cb80a75b859d0a16
  • Loading branch information...
commit 79ea643f62c11ce5508cd76de601b4544a1973eb 1 parent cec0f39
@scoder scoder authored
Showing with 40 additions and 18 deletions.
  1. +27 −14 selftest.py
  2. +13 −4 selftest2.py
View
41 selftest.py
@@ -9,7 +9,17 @@
# TODO: add xml/html parsing tests
# TODO: etc
-import re, sys, string
+import re, sys
+
+def stdout():
+ if sys.version_info[0] < 3:
+ return sys.stdout
+ class bytes_stdout(object):
+ def write(self, data):
+ if isinstance(data, bytes):
+ data = data.decode('ISO8859-1')
+ sys.stdout.write(data)
+ return bytes_stdout()
try:
from StringIO import StringIO as BytesIO
@@ -38,10 +48,13 @@ def serialize(elem, **options):
file = BytesIO()
tree = ElementTree.ElementTree(elem)
tree.write(file, **options)
- try:
- encoding = options["encoding"]
- except KeyError:
- encoding = "utf-8"
+ if sys.version_info[0] < 3:
+ try:
+ encoding = options["encoding"]
+ except KeyError:
+ encoding = "utf-8"
+ else:
+ encoding = 'ISO8859-1'
result = fix_compatibility(file.getvalue().decode(encoding))
if sys.version_info[0] < 3:
result = result.encode(encoding)
@@ -308,7 +321,7 @@ def parsefile():
>>> tree = ElementTree.parse("samples/simple.xml")
>>> normalize_crlf(tree)
- >>> tree.write(sys.stdout)
+ >>> tree.write(stdout())
<root>
<element key="value">text</element>
<element>text</element>tail
@@ -316,7 +329,7 @@ def parsefile():
</root>
>>> tree = ElementTree.parse("samples/simple-ns.xml")
>>> normalize_crlf(tree)
- >>> tree.write(sys.stdout)
+ >>> tree.write(stdout())
<root xmlns="http://namespace/">
<element key="value">text</element>
<element>text</element>tail
@@ -347,19 +360,19 @@ def parsehtml():
def parseliteral():
r"""
>>> element = ElementTree.XML("<html><body>text</body></html>")
- >>> ElementTree.ElementTree(element).write(sys.stdout)
+ >>> ElementTree.ElementTree(element).write(stdout())
<html><body>text</body></html>
>>> element = ElementTree.fromstring("<html><body>text</body></html>")
- >>> ElementTree.ElementTree(element).write(sys.stdout)
+ >>> ElementTree.ElementTree(element).write(stdout())
<html><body>text</body></html>
## >>> sequence = ["<html><body>", "text</bo", "dy></html>"]
## >>> element = ElementTree.fromstringlist(sequence)
-## >>> ElementTree.ElementTree(element).write(sys.stdout)
+## >>> ElementTree.ElementTree(element).write(stdout())
## <html><body>text</body></html>
- >>> print(ElementTree.tostring(element))
- <html><body>text</body></html>
+ >>> print(repr(ElementTree.tostring(element)).lstrip('b'))
+ '<html><body>text</body></html>'
# looks different in lxml
# >>> print(ElementTree.tostring(element, "ascii"))
@@ -518,10 +531,10 @@ def writefile():
def writestring():
"""
>>> elem = ElementTree.XML("<html><body>text</body></html>")
- >>> ElementTree.tostring(elem)
+ >>> print(repr(ElementTree.tostring(elem)).lstrip('b'))
'<html><body>text</body></html>'
>>> elem = ElementTree.fromstring("<html><body>text</body></html>")
- >>> ElementTree.tostring(elem)
+ >>> print(repr(ElementTree.tostring(elem)).lstrip('b'))
'<html><body>text</body></html>'
"""
View
17 selftest2.py
@@ -14,6 +14,16 @@
from lxml import etree as ElementTree
+def stdout():
+ if sys.version_info[0] < 3:
+ return sys.stdout
+ class bytes_stdout(object):
+ def write(self, data):
+ if isinstance(data, bytes):
+ data = data.decode('ISO8859-1')
+ sys.stdout.write(data)
+ return bytes_stdout()
+
def unserialize(text):
file = StringIO(text)
tree = ElementTree.parse(file)
@@ -25,11 +35,10 @@ def serialize(elem, encoding=None):
if encoding:
tree.write(file, encoding=encoding)
else:
- encoding = "utf-8"
tree.write(file)
result = file.getvalue()
if sys.version_info[0] >= 3:
- result = result.decode(encoding)
+ result = result.decode('ISO8859-1')
result = result.replace(' />', '/>')
if result[-1:] == '\n':
result = result[:-1]
@@ -118,14 +127,14 @@ def parsefile():
mode, and doctest doesn't filter out carriage returns.
>>> tree = ElementTree.parse(open("samples/simple.xml", "rb"))
- >>> tree.write(sys.stdout)
+ >>> tree.write(stdout())
<root>
<element key="value">text</element>
<element>text</element>tail
<empty-element/>
</root>
>>> tree = ElementTree.parse(open("samples/simple-ns.xml", "rb"))
- >>> tree.write(sys.stdout)
+ >>> tree.write(stdout())
<root xmlns="http://namespace/">
<element key="value">text</element>
<element>text</element>tail
Please sign in to comment.
Something went wrong with that request. Please try again.