Skip to content

Commit 01ea892

Browse files
author
James William Pye
committed
Hopefully a real fix for the XML type.
This is getting a bit tricky..
1 parent 763d57b commit 01ea892

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

postgresql/test/test_driver.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,13 @@ def testXML(self):
11411141
return
11421142
foo = etree.XML('<foo/>')
11431143
bar = etree.XML('<bar/>')
1144-
tostr = etree.tostring
1144+
if hasattr(etree, 'tostringlist'):
1145+
# 3.2
1146+
def tostr(x):
1147+
return etree.tostring(x, encoding='utf-8')
1148+
else:
1149+
# 3.1 compat
1150+
tostr = etree.tostring
11451151
self.failUnlessEqual(tostr(xml.first(foo)), tostr(foo))
11461152
self.failUnlessEqual(tostr(xml.first(bar)), tostr(bar))
11471153
self.failUnlessEqual(tostr(textxml.first('<foo/>')), tostr(foo))

postgresql/types/io/stdlib_xml_etree.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,40 @@ def xml_io_factory(typoid, typio, c = compose):
4343
else:
4444
# New etree tostring API.
4545
def xml_pack(xml, encoding,
46-
tostr = etree.tostringlist, et = etree.ElementTree,
46+
tostr = etree.tostring, et = etree.ElementTree,
4747
str = str, isinstance = isinstance, tuple = tuple,
4848
):
49+
if isinstance(xml, bytes):
50+
return xml
4951
if isinstance(xml, str):
5052
# If it's a string, encode and return.
5153
return xml.encode(encoding)
5254
elif isinstance(xml, tuple):
5355
# If it's a tuple, encode and return the joined items.
5456
# We do not accept lists here--emphasizing lists being used for ARRAY
5557
# bounds.
56-
return b''.join((
58+
##
59+
# 3.2
60+
# XXX: tostring doesn't include declaration with utf-8?
61+
x = b''.join(
5762
x.encode(encoding) if isinstance(x, str) else
58-
b''.join(tostr(x, encoding = encoding)[1:]) for x in xml
59-
))
60-
return b''.join(tostr(xml, encoding = encoding)[1:])
63+
tostr(x, encoding = "utf-8")
64+
for x in xml
65+
)
66+
else:
67+
##
68+
# 3.2
69+
# XXX: tostring doesn't include declaration with utf-8?
70+
x = tostr(xml, encoding = "utf-8")
71+
if encoding in ('utf8','utf-8'):
72+
return x
73+
else:
74+
return x.decode('utf-8').encode(encoding)
6175

6276
def xml_io_factory(typoid, typio, c = compose):
63-
def local_xml_pack(x, typio = typio):
77+
def local_xml_pack(x, typio = typio, xml_pack = xml_pack):
6478
return xml_pack(x, typio.encoding)
65-
return (
66-
local_xml_pack,
67-
c((typio.decode, xml_unpack)),
68-
etree.ElementTree,
69-
)
79+
return (local_xml_pack, c((typio.decode, xml_unpack)), etree.ElementTree,)
7080

7181
oid_to_io = {
7282
XMLOID : xml_io_factory

0 commit comments

Comments
 (0)