Skip to content

Commit 763d57b

Browse files
author
James William Pye
committed
Fix etree usage on 3.2. (stdlib API change)
1 parent b0359ff commit 763d57b

2 files changed

Lines changed: 49 additions & 17 deletions

File tree

postgresql/documentation/changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Changes
1010
* Properly raise StopIteration in Cursor.__next__ (Elvis Pranskevichus)
1111
* Fix Startup() usage for Python 3.2.
1212
* Emit deprecation warning when 'gid' is given to xact().
13+
* Compensate for Python3.2's ElementTree API changes.
1314

1415
1.0.1 released on 2010-04-24
1516
----------------------------

postgresql/types/io/stdlib_xml_etree.py

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,61 @@
1212
XMLOID: etree.ElementTree,
1313
}
1414

15-
def xml_pack(xml, tostr = etree.tostring, et = etree.ElementTree):
16-
if isinstance(xml, str):
17-
# If it's a string, encode and return.
18-
return xml
19-
elif isinstance(xml, tuple):
20-
# If it's a tuple, encode and return the joined items.
21-
# We do not accept lists here--emphasizing lists being used for ARRAY
22-
# bounds.
23-
return ''.join((x if isinstance(x, str) else tostr(x) for x in xml))
24-
return tostr(xml)
25-
2615
def xml_unpack(xmldata, XML = etree.XML):
2716
try:
2817
return XML(xmldata)
2918
except Exception:
3019
# try it again, but return the sequence of children.
3120
return tuple(XML('<x>' + xmldata + '</x>'))
3221

33-
def xml_io_factory(typoid, typio, c = compose):
34-
return (
35-
c((xml_pack, typio.encode)),
36-
c((typio.decode, xml_unpack)),
37-
etree.ElementTree,
38-
)
22+
if not hasattr(etree, 'tostringlist'):
23+
# Python 3.1 support.
24+
def xml_pack(xml, tostr = etree.tostring, et = etree.ElementTree,
25+
str = str, isinstance = isinstance, tuple = tuple
26+
):
27+
if isinstance(xml, str):
28+
# If it's a string, encode and return.
29+
return xml
30+
elif isinstance(xml, tuple):
31+
# If it's a tuple, encode and return the joined items.
32+
# We do not accept lists here--emphasizing lists being used for ARRAY
33+
# bounds.
34+
return ''.join((x if isinstance(x, str) else tostr(x) for x in xml))
35+
return tostr(xml)
36+
37+
def xml_io_factory(typoid, typio, c = compose):
38+
return (
39+
c((xml_pack, typio.encode)),
40+
c((typio.decode, xml_unpack)),
41+
etree.ElementTree,
42+
)
43+
else:
44+
# New etree tostring API.
45+
def xml_pack(xml, encoding,
46+
tostr = etree.tostringlist, et = etree.ElementTree,
47+
str = str, isinstance = isinstance, tuple = tuple,
48+
):
49+
if isinstance(xml, str):
50+
# If it's a string, encode and return.
51+
return xml.encode(encoding)
52+
elif isinstance(xml, tuple):
53+
# If it's a tuple, encode and return the joined items.
54+
# We do not accept lists here--emphasizing lists being used for ARRAY
55+
# bounds.
56+
return b''.join((
57+
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:])
61+
62+
def xml_io_factory(typoid, typio, c = compose):
63+
def local_xml_pack(x, typio = typio):
64+
return xml_pack(x, typio.encoding)
65+
return (
66+
local_xml_pack,
67+
c((typio.decode, xml_unpack)),
68+
etree.ElementTree,
69+
)
3970

4071
oid_to_io = {
4172
XMLOID : xml_io_factory

0 commit comments

Comments
 (0)