Skip to content

Commit

Permalink
python3: unicode - minimalist change set
Browse files Browse the repository at this point in the history
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: vmware#55
  • Loading branch information
hartsock committed Jul 29, 2014
1 parent 4c68638 commit 1e4c9f3
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion pyVmomi/Differ.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions pyVmomi/SoapAdapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
# limitations under the License.

from six.moves import http_client
from six import text_type
from six import u

import sys
import os
import time
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = Iso8601.ParseISO8601(data)
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 9 additions & 6 deletions pyVmomi/VmomiSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
## VMOMI support code
from __future__ import with_statement # 2.5 only

from six import text_type
from six import u

from datetime import datetime
import Iso8601
import base64
Expand Down Expand Up @@ -176,13 +179,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
Expand Down Expand Up @@ -1237,7 +1240,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()
Expand Down Expand Up @@ -1277,8 +1280,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()
Expand Down

0 comments on commit 1e4c9f3

Please sign in to comment.