From 05552044f686c76d3ceb3bf4e7686c4660a7f972 Mon Sep 17 00:00:00 2001 From: Shawn Hartsock Date: Thu, 24 Jul 2014 14:25:27 -0400 Subject: [PATCH] python3: unicode - minimalist change set A minimal change set to deal with unicode modules and methods between python2 and python3. A deeper reworking of the unicode components is very likely called for, however, this change should get the library working on python3. partial: https://github.com/vmware/pyvmomi/issues/55 --- pyVmomi/Differ.py | 4 +++- pyVmomi/SoapAdapter.py | 9 ++++++--- pyVmomi/VmomiSupport.py | 15 +++++++++------ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pyVmomi/Differ.py b/pyVmomi/Differ.py index bf6c97372..a402e16fc 100644 --- a/pyVmomi/Differ.py +++ b/pyVmomi/Differ.py @@ -14,6 +14,8 @@ # limitations under the License. ## Diff any two objects +from six import text_type +from six import u from pyVmomi import VmomiSupport, types import itertools @@ -33,7 +35,7 @@ def IsPrimitiveType(obj): isinstance(obj, types.short) or isinstance(obj, types.int) or isinstance(obj, types.double) or isinstance(obj, types.float) or isinstance(obj, types.long) or isinstance(obj, types.str) or - isinstance(obj, unicode) or + isinstance(obj, text_type) or isinstance(obj, types.PropertyPath) or isinstance(obj, types.ManagedMethod) or isinstance(obj, types.datetime) or diff --git a/pyVmomi/SoapAdapter.py b/pyVmomi/SoapAdapter.py index 7ffeeb803..da6b2aad2 100644 --- a/pyVmomi/SoapAdapter.py +++ b/pyVmomi/SoapAdapter.py @@ -14,6 +14,9 @@ # limitations under the License. from __future__ import absolute_import from six.moves import http_client +from six import text_type +from six import u + import sys import os import time @@ -378,7 +381,7 @@ def _Serialize(self, val, info, defNS): else: nsattr, qName = self._QName(Type(val), currDefNS) attr += '%s %stype="%s"' % (nsattr, self.xsiPrefix, qName) - if not isinstance(val, unicode): + if not isinstance(val, text_type): # Use UTF-8 rather than self.encoding. self.encoding is for # output of serializer, while 'val' is our input. And regardless # of what our output is, our input should be always UTF-8. Yes, @@ -662,7 +665,7 @@ def EndElementHandler(self, tag): elif obj is str: try: obj = str(data) - except UnicodeError: + except ValueError: obj = data elif obj is datetime: obj = pyVmomi.Iso8601.ParseISO8601(data) @@ -772,7 +775,7 @@ def EndElementHandler(self, tag): if self.isFault and tag == "faultstring": try: self.msg = str(self.data) - except UnicodeError: + except ValueError: self.msg = self.data ## Base class that implements common functionality for stub adapters. diff --git a/pyVmomi/VmomiSupport.py b/pyVmomi/VmomiSupport.py index 73f91575c..f46849d61 100644 --- a/pyVmomi/VmomiSupport.py +++ b/pyVmomi/VmomiSupport.py @@ -17,6 +17,9 @@ from __future__ import absolute_import from __future__ import with_statement # 2.5 only +from six import text_type +from six import u + from datetime import datetime import pyVmomi.Iso8601 import base64 @@ -177,13 +180,13 @@ def __getattr__(self, attr): else: raise AttributeError(attr) -class Link(unicode): +class Link(text_type): def __new__(cls, obj): if isinstance(obj, basestring): - return unicode.__new__(cls, obj) + return text_type.__new__(cls, obj) elif isinstance(obj, DataObject): if obj.key: - return unicode.__new__(cls, obj.key) + return text_type.__new__(cls, obj.key) raise AttributeError("DataObject does not have a key to link") else: raise ValueError @@ -1238,7 +1241,7 @@ def InverseMap(map): double = type("double", (float,), {}) URI = type("URI", (str,), {}) binary = type("binary", (str,), {}) -PropertyPath = type("PropertyPath", (unicode,), {}) +PropertyPath = type("PropertyPath", (text_type,), {}) # _wsdlTypeMapNSs store namespaces added to _wsdlTypeMap in _SetWsdlType _wsdlTypeMapNSs = set() @@ -1278,8 +1281,8 @@ def InverseMap(map): # unicode is mapped to wsdl name 'string' (Cannot put in wsdlTypeMap or name # collision with non-unicode string) -_wsdlNameMap[unicode] = (XMLNS_XSD, 'string') -_wsdlNameMap[CreateArrayType(unicode)] = (XMLNS_VMODL_BASE, 'ArrayOfString') +_wsdlNameMap[text_type] = (XMLNS_XSD, 'string') +_wsdlNameMap[CreateArrayType(text_type)] = (XMLNS_VMODL_BASE, 'ArrayOfString') # _wsdlMethodNSs store namespaces added to _wsdlMethodMap in _SetWsdlMethod _wsdlMethodNSs = set()