Skip to content

Commit 60cf1da

Browse files
author
James William Pye
committed
Don't crash if given an XML fragment.
Rather, make it a document and return all the children as a list.
1 parent cda120d commit 60cf1da

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

postgresql/protocol/typio.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@
4848
from decimal import Decimal, DecimalTuple
4949
import datetime
5050

51-
try:
52-
import xml.etree.cElementTree as etree
53-
except ImportError:
54-
import xml.etree.ElementTree as etree
55-
5651
from .. import types as pg_types
5752
from ..encodings import aliases as pg_enc_aliases
5853
from . import typstruct as ts
@@ -676,7 +671,12 @@ def xml_pack(self, xml):
676671
return self._encode(etree.tostring(xml))[0]
677672

678673
def xml_unpack(self, xmldata):
679-
return pg_types.etree.XML(self._decode(xmldata)[0])
674+
xml_or_frag = self._decode(xmldata)[0]
675+
try:
676+
return pg_types.etree.XML(xml_or_frag)
677+
except Exception:
678+
# try it again, but return the children.
679+
return list(pg_types.etree.XML('<x>' + xml_or_frag + '</x>'))
680680

681681
def attribute_map(self, pq_descriptor):
682682
return {

postgresql/types.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
PostgreSQL types and identifiers
77
"""
88
import math
9+
try:
10+
import xml.etree.cElementTree as etree
11+
except ImportError:
12+
import xml.etree.ElementTree as etree
913

1014
InvalidOid = 0
1115

0 commit comments

Comments
 (0)