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.
## 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

View File

@@ -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 @@ class SoapSerializer:
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 @@ class SoapDeserializer(ExpatDeserializerNSHandlers):
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 @@ class SoapResponseDeserializer(ExpatDeserializerNSHandlers):
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.

View File

@@ -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 @@ class LazyObject(Object):
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 @@ short = type("short", (int,), {})
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 @@ del name, typ
# 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()