Skip to content

Commit b72b88c

Browse files
author
James William Pye
committed
More XML fixes..
1 parent 01ea892 commit b72b88c

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

postgresql/test/test_driver.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,8 +1130,7 @@ def testDomainSupport(self):
11301130
)
11311131
)
11321132

1133-
@pg_tmp
1134-
def testXML(self):
1133+
def check_xml(self):
11351134
try:
11361135
xml = db.prepare('select $1::xml')
11371136
textxml = db.prepare('select $1::text::xml')
@@ -1184,6 +1183,23 @@ def tostr(x):
11841183
(tostr(foo), tostr(bar))
11851184
)
11861185

1186+
@pg_tmp
1187+
def testXML(self):
1188+
self.check_xml()
1189+
1190+
@pg_tmp
1191+
def testXML_ascii(self):
1192+
# check a non-utf8 encoding (3.2 and up)
1193+
db.settings['client_encoding'] = 'sql_ascii'
1194+
self.check_xml()
1195+
1196+
@pg_tmp
1197+
def testXML_utf8(self):
1198+
# in 3.2 we always serialize at utf-8, so check that
1199+
# that path is being ran by forcing the client_encoding to utf8.
1200+
db.settings['client_encoding'] = 'utf8'
1201+
self.check_xml()
1202+
11871203
@pg_tmp
11881204
def testUUID(self):
11891205
# doesn't exist in all versions supported by py-postgresql.

postgresql/types/io/stdlib_xml_etree.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ def xml_io_factory(typoid, typio, c = compose):
4242
)
4343
else:
4444
# New etree tostring API.
45-
def xml_pack(xml, encoding,
45+
def xml_pack(xml, encoding, encoder,
4646
tostr = etree.tostring, et = etree.ElementTree,
4747
str = str, isinstance = isinstance, tuple = tuple,
4848
):
4949
if isinstance(xml, bytes):
5050
return xml
5151
if isinstance(xml, str):
5252
# If it's a string, encode and return.
53-
return xml.encode(encoding)
53+
return encoder(xml)
5454
elif isinstance(xml, tuple):
5555
# If it's a tuple, encode and return the joined items.
5656
# We do not accept lists here--emphasizing lists being used for ARRAY
@@ -59,7 +59,7 @@ def xml_pack(xml, encoding,
5959
# 3.2
6060
# XXX: tostring doesn't include declaration with utf-8?
6161
x = b''.join(
62-
x.encode(encoding) if isinstance(x, str) else
62+
x.encode('utf-8') if isinstance(x, str) else
6363
tostr(x, encoding = "utf-8")
6464
for x in xml
6565
)
@@ -71,11 +71,11 @@ def xml_pack(xml, encoding,
7171
if encoding in ('utf8','utf-8'):
7272
return x
7373
else:
74-
return x.decode('utf-8').encode(encoding)
74+
return encoder(x.decode('utf-8'))
7575

7676
def xml_io_factory(typoid, typio, c = compose):
77-
def local_xml_pack(x, typio = typio, xml_pack = xml_pack):
78-
return xml_pack(x, typio.encoding)
77+
def local_xml_pack(x, encoder = typio.encode, typio = typio, xml_pack = xml_pack):
78+
return xml_pack(x, typio.encoding, encoder)
7979
return (local_xml_pack, c((typio.decode, xml_unpack)), etree.ElementTree,)
8080

8181
oid_to_io = {

0 commit comments

Comments
 (0)