@@ -43,30 +43,40 @@ def xml_io_factory(typoid, typio, c = compose):
4343else :
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
7181oid_to_io = {
7282 XMLOID : xml_io_factory
0 commit comments