diff options
Diffstat (limited to 'Lib/xmlrpc/client.py')
-rw-r--r-- | Lib/xmlrpc/client.py | 73 |
1 files changed, 33 insertions, 40 deletions
diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py index 9de5111..bbf9ee6 100644 --- a/Lib/xmlrpc/client.py +++ b/Lib/xmlrpc/client.py @@ -208,8 +208,8 @@ class ProtocolError(Error): self.headers = headers def __repr__(self): return ( - "<ProtocolError for %s: %s %s>" % - (self.url, self.errcode, self.errmsg) + "<%s for %s: %s %s>" % + (self.__class__.__name__, self.url, self.errcode, self.errmsg) ) ## @@ -237,7 +237,8 @@ class Fault(Error): self.faultCode = faultCode self.faultString = faultString def __repr__(self): - return "<Fault %s: %r>" % (self.faultCode, self.faultString) + return "<%s %s: %r>" % (self.__class__.__name__, + self.faultCode, self.faultString) # -------------------------------------------------------------------- # Special values @@ -339,10 +340,6 @@ class DateTime: s, o = self.make_comparable(other) return s == o - def __ne__(self, other): - s, o = self.make_comparable(other) - return s != o - def timetuple(self): return time.strptime(self.value, "%Y%m%dT%H:%M:%S") @@ -355,7 +352,7 @@ class DateTime: return self.value def __repr__(self): - return "<DateTime %r at %x>" % (self.value, id(self)) + return "<%s %r at %#x>" % (self.__class__.__name__, self.value, id(self)) def decode(self, data): self.value = str(data).strip() @@ -406,11 +403,6 @@ class Binary: other = other.data return self.data == other - def __ne__(self, other): - if isinstance(other, Binary): - other = other.data - return self.data != other - def decode(self, data): self.data = base64.decodebytes(data) @@ -648,6 +640,7 @@ class Unmarshaller: self._stack = [] self._marks = [] self._data = [] + self._value = False self._methodname = None self._encoding = "utf-8" self.append = self._stack.append @@ -677,6 +670,8 @@ class Unmarshaller: if tag == "array" or tag == "struct": self._marks.append(len(self._stack)) self._data = [] + if self._value and tag not in self.dispatch: + raise ResponseError("unknown tag %r" % tag) self._value = (tag == "value") def data(self, text): @@ -852,7 +847,7 @@ class MultiCall: self.__call_list = [] def __repr__(self): - return "<MultiCall at %x>" % id(self) + return "<%s at %#x>" % (self.__class__.__name__, id(self)) __str__ = __repr__ @@ -963,8 +958,6 @@ def dumps(params, methodname=None, methodresponse=None, encoding=None, # standard XML-RPC wrappings if methodname: # a method call - if not isinstance(methodname, str): - methodname = methodname.encode(encoding) data = ( xmlheader, "<methodCall>\n" @@ -1023,12 +1016,9 @@ def gzip_encode(data): if not gzip: raise NotImplementedError f = BytesIO() - gzf = gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1) - gzf.write(data) - gzf.close() - encoded = f.getvalue() - f.close() - return encoded + with gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1) as gzf: + gzf.write(data) + return f.getvalue() ## # Decode a string using the gzip content encoding such as specified by the @@ -1049,17 +1039,14 @@ def gzip_decode(data, max_decode=20971520): """ if not gzip: raise NotImplementedError - f = BytesIO(data) - gzf = gzip.GzipFile(mode="rb", fileobj=f) - try: - if max_decode < 0: # no limit - decoded = gzf.read() - else: - decoded = gzf.read(max_decode + 1) - except OSError: - raise ValueError("invalid data") - f.close() - gzf.close() + with gzip.GzipFile(mode="rb", fileobj=BytesIO(data)) as gzf: + try: + if max_decode < 0: # no limit + decoded = gzf.read() + else: + decoded = gzf.read(max_decode + 1) + except OSError: + raise ValueError("invalid data") if max_decode >= 0 and len(decoded) > max_decode: raise ValueError("max gzipped payload length exceeded") return decoded @@ -1145,13 +1132,13 @@ class Transport: for i in (0, 1): try: return self.single_request(host, handler, request_body, verbose) + except http.client.RemoteDisconnected: + if i: + raise except OSError as e: if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE): raise - except http.client.BadStatusLine: #close after we sent request - if i: - raise def single_request(self, host, handler, request_body, verbose=False): # issue XML-RPC request @@ -1436,7 +1423,7 @@ class ServerProxy: # call a method on the remote server request = dumps(params, methodname, encoding=self.__encoding, - allow_none=self.__allow_none).encode(self.__encoding) + allow_none=self.__allow_none).encode(self.__encoding, 'xmlcharrefreplace') response = self.__transport.request( self.__host, @@ -1452,8 +1439,8 @@ class ServerProxy: def __repr__(self): return ( - "<ServerProxy for %s%s>" % - (self.__host, self.__handler) + "<%s for %s%s>" % + (self.__class__.__name__, self.__host, self.__handler) ) __str__ = __repr__ @@ -1462,7 +1449,7 @@ class ServerProxy: # magic method dispatcher return _Method(self.__request, name) - # note: to call a remote object with an non-standard name, use + # note: to call a remote object with a non-standard name, use # result getattr(server, "strange-python-name")(args) def __call__(self, attr): @@ -1475,6 +1462,12 @@ class ServerProxy: return self.__transport raise AttributeError("Attribute %r not found" % (attr,)) + def __enter__(self): + return self + + def __exit__(self, *args): + self.__close() + # compatibility Server = ServerProxy |