Skip to content

Commit

Permalink
python3: KeyError uses '%' not format method
Browse files Browse the repository at this point in the history
* Malformed Faults: introduces test for fault handler
* fixes KeyError raises statements to use 'format' instead

partial: vmware#72
partial: vmware#55
  • Loading branch information
hartsock committed Jul 30, 2014
1 parent 297302c commit dac8a79
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 26 deletions.
49 changes: 29 additions & 20 deletions pyVmomi/SoapAdapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from six import PY2
from six import PY3

from six import reraise
from six.moves import http_client

if PY3:
Expand Down Expand Up @@ -442,6 +442,24 @@ def _SerializeDataObject(self, val, info, attr, currDefNS):
self.writer.write('</{0}>'.format(info.name))


class ParserError(KeyError):
def __init__(self, *args, **kwargs):
super(ParserError, self).__init__(*args, **kwargs)


def Parse(parser, data):
try:
if isinstance(data, str):
parser.Parse(data)
else:
parser.ParseFile(data)
except Exception as ex:
line = parser.CurrentLineNumber
col = parser.CurrentColumnNumber
pe = ParserError("{0} line:{1}, col:{2}".format(str(ex), line, col))
(ec, ev, tb) = sys.exc_info()
reraise(ParserError, pe, tb)

## Deserialize an object from a file or string
#
# This function will deserialize one top-level XML node.
Expand All @@ -453,10 +471,7 @@ def Deserialize(data, resultType=object, stub=None):
parser = ParserCreate(namespace_separator=NS_SEP)
ds = SoapDeserializer(stub)
ds.Deserialize(parser, resultType)
if isinstance(data, str):
parser.Parse(data)
else:
parser.ParseFile(data)
Parse(parser, data)
return ds.GetResult()


Expand Down Expand Up @@ -582,11 +597,7 @@ def StartElementHandler(self, tag, attr):
if not self.stack:
if self.isFault:
ns, name = self.SplitTag(tag)
try:
objType = self.LookupWsdlType(ns, name[:-5])
except KeyError:
message = "{0} was not found in the WSDL".format(name[:-5])
raise VmomiMessageFault(message)
objType = self.LookupWsdlType(ns, name[:-5])
# Only top level soap fault should be deserialized as method fault
deserializeAsLocalizedMethodFault = False
else:
Expand Down Expand Up @@ -761,10 +772,7 @@ def Deserialize(self, response, resultType, nsMap=None):
nsMap = {}
self.nsMap = nsMap
SetHandlers(self.parser, GetHandlers(self))
if isinstance(response, str):
self.parser.Parse(response)
else:
self.parser.ParseFile(response)
Parse(self.parser, response)
result = self.deser.GetResult()
if self.isFault:
if result is None:
Expand Down Expand Up @@ -1241,10 +1249,10 @@ def InvokeMethod(self, mo, info, args, outerStub=None):
# The server is probably sick, drop all of the cached connections.
self.DropConnections()
raise
cookie = resp.getheader('Set-Cookie')
cookie = resp.getheader('set-cookie')
if cookie is None:
# try lower-case header for backwards compat. with old vSphere
cookie = resp.getheader('set-cookie')
# try case-sensitive header for compatibility
cookie = resp.getheader('Set-Cookie')
status = resp.status

if cookie:
Expand All @@ -1257,12 +1265,13 @@ def InvokeMethod(self, mo, info, args, outerStub=None):
fd = GzipReader(resp, encoding=GzipReader.GZIP)
elif encoding == 'deflate':
fd = GzipReader(resp, encoding=GzipReader.DEFLATE)
obj = SoapResponseDeserializer(outerStub).Deserialize(fd, info.result)
except:
deserializer = SoapResponseDeserializer(outerStub)
obj = deserializer.Deserialize(fd, info.result)
except Exception as exc:
conn.close()
# The server might be sick, drop all of the cached connections.
self.DropConnections()
raise
raise exc
else:
resp.read()
self.ReturnConnection(conn)
Expand Down
18 changes: 12 additions & 6 deletions pyVmomi/VmomiSupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ def _SetWsdlType(ns, wsdlName, typ):
# @return type if found else throws KeyError
def GetWsdlType(ns, name):
if ns is None or name is None:
raise KeyError("%s %s" % (ns, name))
raise KeyError("{0} {1}".format(ns, name))

with _lazyLock:
# Check if the type is loaded in the map
Expand All @@ -1003,14 +1003,20 @@ def GetWsdlType(ns, name):
try:
return GetWsdlType(ns, name[7:]).Array
except KeyError:
raise KeyError("%s %s" % (ns, name))
raise KeyError("{0} {1}".format(ns, name))
else:
# Type is not loaded yet, load it
typ = _LoadVmodlType(_wsdlDefMap[(ns, name)][0])
if typ:
return typ

raise KeyError("%s %s" % (ns, name))
raise KeyError("{0} {1}".format(ns, name))


class UnknownWsdlTypeError(KeyError):
def __init__(self, *args, **kwargs):
super(UnknownWsdlTypeError, self).__init__(*args, **kwargs)


## Guess the type from wsdlname with no ns
# WARNING! This should not be used in general, as there is no guarantee for
Expand All @@ -1026,8 +1032,8 @@ def GuessWsdlType(name):
try:
return GetWsdlType(ns, name)
except KeyError:
pass
raise KeyError(name)
pass
raise UnknownWsdlTypeError(name)

## Return a map that contains all the wsdl types
# This function is rarely used
Expand Down Expand Up @@ -1203,7 +1209,7 @@ def GetWsdlMethod(ns, wsdlName):
LoadManagedType(*method)
return _wsdlMethodMap[(ns, wsdlName)]
else:
raise KeyError("%s %s" % (ns, name))
raise KeyError("{0} {1}".format(ns, name))

## Guess the method from wsdlname with no ns
# WARNING! This should not be used in general, as there is no guarantee for
Expand Down
Loading

0 comments on commit dac8a79

Please sign in to comment.