Skip to content

Commit ac455dc

Browse files
author
Tianhao He
committed
Fixes #519, #448, #420, #421 Fix SoapAdapter serializer to support serializing unicode chars
We use string formatter to contruct serialization result. The template used is str(byte) in python2 even though when the val is unicode. This is fine when val only contains ASCII chars since python can encode them to str implicitly using ASCII encoding. But when the val is non-ASCII unicodes, the conversion fails. Fix it by using a unicode template to avoid the conversion.
1 parent 5ce2854 commit ac455dc

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pyVmomi/SoapAdapter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def _Serialize(self, val, info, defNS):
448448
# a bug.
449449
val = val.decode(XML_ENCODING)
450450
result = XmlEscape(val)
451-
self.writer.write('<{0}{1}>{2}</{0}>'.format(info.name, attr, result))
451+
self.writer.write(u'<{0}{1}>{2}</{0}>'.format(info.name, attr, result))
452452

453453
## Serialize a a data object (internal)
454454
#

tests/test_serializer.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
13
# VMware vSphere Python SDK
24
# Copyright (c) 2008-2015 VMware, Inc. All Rights Reserved.
35
#
@@ -40,6 +42,14 @@ def test_serialize_float(self):
4042
pc.diskSpaceToAddressSpaceRatio = 1.0
4143
SoapAdapter.Serialize(pc, version='vim.version.version10')
4244

45+
def test_serialize_unicode(self):
46+
self.assertEqual(SoapAdapter.SerializeToUnicode('Ḃ'),
47+
u'<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:vim25" xsi:type="xsd:string">\u1e02</object>')
48+
self.assertEqual(SoapAdapter.SerializeToUnicode(u'Ḃ'),
49+
u'<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:vim25" xsi:type="xsd:string">\u1e02</object>')
50+
self.assertEqual(SoapAdapter.SerializeToUnicode(u'\u1e02'),
51+
u'<object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:vim25" xsi:type="xsd:string">\u1e02</object>')
52+
4353
def _base_serialize_test(self, soap_creator, request_matcher):
4454
my_vcr = config.VCR(
4555
custom_patches=(
@@ -99,3 +109,4 @@ def soap_creator():
99109
self._base_serialize_test(soap_creator, request_matcher)
100110
finally:
101111
GetRequestContext().pop("vcSessionCookie")
112+

0 commit comments

Comments
 (0)