diff options
Diffstat (limited to 'Lib/xmlrpc')
-rw-r--r-- | Lib/xmlrpc/client.py | 60 |
1 files changed, 26 insertions, 34 deletions
diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py index 9de5111..25e684f 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) @@ -852,7 +844,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__ @@ -1023,12 +1015,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 +1038,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 @@ -1149,7 +1135,7 @@ class Transport: if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE): raise - except http.client.BadStatusLine: #close after we sent request + except http.client.RemoteDisconnected: if i: raise @@ -1452,8 +1438,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__ @@ -1475,6 +1461,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 |