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
This commit is contained in:
Shawn Hartsock
2014-07-24 14:25:27 -04:00
parent 1febb9a935
commit 05552044f6
3 changed files with 18 additions and 10 deletions

View File

@@ -14,6 +14,8 @@
# limitations under the License. # limitations under the License.
## Diff any two objects ## Diff any two objects
from six import text_type
from six import u
from pyVmomi import VmomiSupport, types from pyVmomi import VmomiSupport, types
import itertools import itertools
@@ -33,7 +35,7 @@ def IsPrimitiveType(obj):
isinstance(obj, types.short) or isinstance(obj, types.int) or isinstance(obj, types.short) or isinstance(obj, types.int) or
isinstance(obj, types.double) or isinstance(obj, types.float) or isinstance(obj, types.double) or isinstance(obj, types.float) or
isinstance(obj, types.long) or isinstance(obj, types.str) 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.PropertyPath) or
isinstance(obj, types.ManagedMethod) or isinstance(obj, types.ManagedMethod) or
isinstance(obj, types.datetime) or isinstance(obj, types.datetime) or

View File

@@ -14,6 +14,9 @@
# limitations under the License. # limitations under the License.
from __future__ import absolute_import from __future__ import absolute_import
from six.moves import http_client from six.moves import http_client
from six import text_type
from six import u
import sys import sys
import os import os
import time import time
@@ -378,7 +381,7 @@ class SoapSerializer:
else: else:
nsattr, qName = self._QName(Type(val), currDefNS) nsattr, qName = self._QName(Type(val), currDefNS)
attr += '%s %stype="%s"' % (nsattr, self.xsiPrefix, qName) 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 # Use UTF-8 rather than self.encoding. self.encoding is for
# output of serializer, while 'val' is our input. And regardless # output of serializer, while 'val' is our input. And regardless
# of what our output is, our input should be always UTF-8. Yes, # of what our output is, our input should be always UTF-8. Yes,
@@ -662,7 +665,7 @@ class SoapDeserializer(ExpatDeserializerNSHandlers):
elif obj is str: elif obj is str:
try: try:
obj = str(data) obj = str(data)
except UnicodeError: except ValueError:
obj = data obj = data
elif obj is datetime: elif obj is datetime:
obj = pyVmomi.Iso8601.ParseISO8601(data) obj = pyVmomi.Iso8601.ParseISO8601(data)
@@ -772,7 +775,7 @@ class SoapResponseDeserializer(ExpatDeserializerNSHandlers):
if self.isFault and tag == "faultstring": if self.isFault and tag == "faultstring":
try: try:
self.msg = str(self.data) self.msg = str(self.data)
except UnicodeError: except ValueError:
self.msg = self.data self.msg = self.data
## Base class that implements common functionality for stub adapters. ## Base class that implements common functionality for stub adapters.

View File

@@ -17,6 +17,9 @@
from __future__ import absolute_import from __future__ import absolute_import
from __future__ import with_statement # 2.5 only from __future__ import with_statement # 2.5 only
from six import text_type
from six import u
from datetime import datetime from datetime import datetime
import pyVmomi.Iso8601 import pyVmomi.Iso8601
import base64 import base64
@@ -177,13 +180,13 @@ class LazyObject(Object):
else: else:
raise AttributeError(attr) raise AttributeError(attr)
class Link(unicode): class Link(text_type):
def __new__(cls, obj): def __new__(cls, obj):
if isinstance(obj, basestring): if isinstance(obj, basestring):
return unicode.__new__(cls, obj) return text_type.__new__(cls, obj)
elif isinstance(obj, DataObject): elif isinstance(obj, DataObject):
if obj.key: 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") raise AttributeError("DataObject does not have a key to link")
else: else:
raise ValueError raise ValueError
@@ -1238,7 +1241,7 @@ short = type("short", (int,), {})
double = type("double", (float,), {}) double = type("double", (float,), {})
URI = type("URI", (str,), {}) URI = type("URI", (str,), {})
binary = type("binary", (str,), {}) binary = type("binary", (str,), {})
PropertyPath = type("PropertyPath", (unicode,), {}) PropertyPath = type("PropertyPath", (text_type,), {})
# _wsdlTypeMapNSs store namespaces added to _wsdlTypeMap in _SetWsdlType # _wsdlTypeMapNSs store namespaces added to _wsdlTypeMap in _SetWsdlType
_wsdlTypeMapNSs = set() _wsdlTypeMapNSs = set()
@@ -1278,8 +1281,8 @@ del name, typ
# unicode is mapped to wsdl name 'string' (Cannot put in wsdlTypeMap or name # unicode is mapped to wsdl name 'string' (Cannot put in wsdlTypeMap or name
# collision with non-unicode string) # collision with non-unicode string)
_wsdlNameMap[unicode] = (XMLNS_XSD, 'string') _wsdlNameMap[text_type] = (XMLNS_XSD, 'string')
_wsdlNameMap[CreateArrayType(unicode)] = (XMLNS_VMODL_BASE, 'ArrayOfString') _wsdlNameMap[CreateArrayType(text_type)] = (XMLNS_VMODL_BASE, 'ArrayOfString')
# _wsdlMethodNSs store namespaces added to _wsdlMethodMap in _SetWsdlMethod # _wsdlMethodNSs store namespaces added to _wsdlMethodMap in _SetWsdlMethod
_wsdlMethodNSs = set() _wsdlMethodNSs = set()