Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML serialization for certain types is broken on Python 3 #257

Merged
merged 1 commit into from
Dec 10, 2015

Conversation

stunes
Copy link

@stunes stunes commented Jul 14, 2015

XML serialization does not work for certain types on Python 3. This is best illustrated by example:

On Python 2.7:

>>> val = pyVmomi.vim.vm.device.VirtualDeviceSpec.FileOperation()
>>> pyVmomi.SoapAdapter.Serialize(val)
'<object xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns="urn:vim25" xsi:type="VirtualDeviceConfigSpecFileOperation"></object>'
>>> from six import text_type
>>> isinstance(val, text_type)
False
>>> val.replace
'replace'

On Python 3.4:

>>> val = pyVmomi.vim.vm.device.VirtualDeviceSpec.FileOperation()
>>> pyVmomi.SoapAdapter.Serialize(val)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mstunes/projects/uwsimscripts2/lib/python3.4/site-packages/pyVmomi/SoapAdapter.py", line 154, in Serialize
    SoapSerializer(writer, version, nsMap, encoding).Serialize(val, info)
  File "/home/mstunes/projects/uwsimscripts2/lib/python3.4/site-packages/pyVmomi/SoapAdapter.py", line 227, in Serialize
    self._Serialize(val, info, self.defaultNS)
  File "/home/mstunes/projects/uwsimscripts2/lib/python3.4/site-packages/pyVmomi/SoapAdapter.py", line 405, in _Serialize
    result = XmlEscape(val)
  File "/home/mstunes/projects/uwsimscripts2/lib/python3.4/site-packages/pyVmomi/SoapAdapter.py", line 109, in XmlEscape
    escaped = xmlStr.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
TypeError: 'vim.vm.device.VirtualDeviceSpec.FileOperation' object is not callable
>>> 
>>> from six import text_type
>>> isinstance(val, text_type)
True
>>> val.replace
'replace'

At SoapAdapter.py:395 (in SoapSerializer._Serialize), the object is checked for isinstance(val, text_type). The result of that comparison is different across versions: on Python 2.7, text_type is 'unicode'; on Python 3.4, text_type is 'str'. As a result, on Python 2.7, val is explicitly converted to a str, whereas no such conversion happens in 3.4. Also, this object has a 'replace' attribute, which is a real function on str objects, but on a FileOperation object, the 'replace' attribute is not a function. Thus, _Serialize fails at runtime when calling replace() on Python 3.4.

Some pyVmomi types present themselves as string types (i.e.,
isinstance(val, str) == True), but those objects shadow certain string
methods that the serializer relies on. Thus, serializing these objects
fails. This change causes the serializer to convert those objects to
their string representation before serializing.

A test case is included; this test fails on Py3 without the change in
question, and passes with it.
tianhao64 pushed a commit that referenced this pull request Dec 10, 2015
XML serialization for certain types is broken on Python 3
@tianhao64 tianhao64 merged commit 56d9328 into vmware:master Dec 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants