Remove state sharing between calls in XML-RPC client
Fixes
from __future__ import absolute_import
+from contextlib import contextmanager
+
try:
import httplib
import xmlrpclib
@@ -206,47 +208,50 @@ class RemoteResult(object):
class XmlRpcRemoteClient(object):
def __init__(self, uri, timeout=None):
- if uri.startswith('https://'):
- transport = TimeoutHTTPSTransport(timeout=timeout)
+ self.uri = uri
+ self.timeout = timeout
+
+ @property
+ @contextmanager
+ def _server(self):
+ if self.uri.startswith('https://'):
+ transport = TimeoutHTTPSTransport(timeout=self.timeout)
else:
- transport = TimeoutHTTPTransport(timeout=timeout)
- self._server = xmlrpclib.ServerProxy(uri, encoding='UTF-8',
+ transport = TimeoutHTTPTransport(timeout=self.timeout)
+ server = xmlrpclib.ServerProxy(self.uri, encoding='UTF-8',
transport=transport)
-
- def get_keyword_names(self):
try:
- return self._server.get_keyword_names()
+ yield server
except (socket.error, xmlrpclib.Error) as err:
raise TypeError(err)
+ finally:
+ server('close')()
+
+ def get_keyword_names(self):
+ with self._server as server:
+ return server.get_keyword_names()
def get_keyword_arguments(self, name):
- try:
- return self._server.get_keyword_arguments(name)
- except xmlrpclib.Error:
- raise TypeError
+ with self._server as server:
+ return server.get_keyword_arguments(name)
def get_keyword_types(self, name):
- try:
- return self._server.get_keyword_types(name)
- except xmlrpclib.Error:
- raise TypeError
+ with self._server as server:
+ return server.get_keyword_types(name)
def get_keyword_tags(self, name):
- try:
- return self._server.get_keyword_tags(name)
- except xmlrpclib.Error:
- raise TypeError
+ with self._server as server:
+ return server.get_keyword_tags(name)
def get_keyword_documentation(self, name):
- try:
- return self._server.get_keyword_documentation(name)
- except xmlrpclib.Error:
- raise TypeError
+ with self._server as server:
+ return server.get_keyword_documentation(name)
def run_keyword(self, name, args, kwargs):
+ with self._server as server:
run_keyword_args = [name, args, kwargs] if kwargs else [name, args]
try:
- return self._server.run_keyword(*run_keyword_args)
+ return server.run_keyword(*run_keyword_args)
except xmlrpclib.Fault as err:
message = err.faultString
except socket.error as err:
