summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Marshall <catchjosh@gmail.com>2015-12-07 00:52:47 -0600
committerJosh Marshall <catchjosh@gmail.com>2015-12-07 00:52:47 -0600
commit6b69154a15c5623647a2fd2e4937db36746cb39d (patch)
tree246c9d9cdcccc50e38e78b41faf4972d8eed6169
parent394d5861366250cb43ab361fc79900df807f4cdb (diff)
parentbb79051c2f49b7f90e97756e5ccabf91fe513046 (diff)
downloadjsonrpclib-6b69154a15c5623647a2fd2e4937db36746cb39d.tar.gz
Merge pull request #24 from efokschaner/master
Fixing bug with jsonrpc._Method caused by debugger
-rw-r--r--jsonrpclib/jsonrpc.py15
-rw-r--r--tests.py74
2 files changed, 53 insertions, 36 deletions
diff --git a/jsonrpclib/jsonrpc.py b/jsonrpclib/jsonrpc.py
index e60f8c4..167bcd7 100644
--- a/jsonrpclib/jsonrpc.py
+++ b/jsonrpclib/jsonrpc.py
@@ -290,11 +290,16 @@ class _Method(XML_Method):
return self.__send(self.__name, kwargs)
def __getattr__(self, name):
- self.__name = '%s.%s' % (self.__name, name)
- return self
- # The old method returned a new instance, but this seemed wasteful.
- # The only thing that changes is the name.
- # return _Method(self.__send, "%s.%s" % (self.__name, name))
+ return _Method(self.__send, "%s.%s" % (self.__name, name))
+
+ def __repr__(self):
+ return '<{} "{}">'.format(self.__class__.__name__, self.__name)
+
+ def __str__(self):
+ return self.__repr__()
+
+ def __dir__(self):
+ return self.__dict__.keys()
class _Notify(object):
diff --git a/tests.py b/tests.py
index f64efc7..e241104 100644
--- a/tests.py
+++ b/tests.py
@@ -357,6 +357,13 @@ class InternalTests(unittest.TestCase):
with self.assertRaises(raises[i]):
func()
+ def test_proxy_object_reuse_is_allowed(self):
+ client = self.get_client()
+ sub_service_proxy = client.sub_service
+ result = sub_service_proxy.subtract(5, 10)
+ self.assertTrue(result == -5)
+ result = sub_service_proxy.add(21, 21)
+ self.assertTrue(result == 42)
if jsonrpc.USE_UNIX_SOCKETS:
# We won't do these tests unless Unix Sockets are supported
@@ -412,36 +419,43 @@ class UnixSocketErrorTests(unittest.TestCase):
jsonrpc.USE_UNIX_SOCKETS = self.original_value
-""" Test Methods """
-
-
-def subtract(minuend, subtrahend):
- """ Using the keywords from the JSON-RPC v2 doc """
- return minuend-subtrahend
-
-
-def add(x, y):
- return x + y
+class ExampleService(object):
+ @staticmethod
+ def subtract(minuend, subtrahend):
+ """ Using the keywords from the JSON-RPC v2 doc """
+ return minuend-subtrahend
+ @staticmethod
+ def add(x, y):
+ return x + y
-def update(*args):
- return args
+ @staticmethod
+ def update(*args):
+ return args
+ @staticmethod
+ def summation(*args):
+ return sum(args)
-def summation(*args):
- return sum(args)
+ @staticmethod
+ def notify_hello(*args):
+ return args
+ @staticmethod
+ def get_data():
+ return ['hello', 5]
-def notify_hello(*args):
- return args
+ @staticmethod
+ def ping():
+ return True
-def get_data():
- return ['hello', 5]
-
-
-def ping():
- return True
+class ExampleAggregateService(ExampleService):
+ """
+ Exposes the inherited ExampleService and a second copy as sub_service
+ """
+ def __init__(self):
+ self.sub_service = ExampleService()
def server_set_up(addr, address_family=socket.AF_INET):
@@ -452,15 +466,13 @@ def server_set_up(addr, address_family=socket.AF_INET):
pass
SimpleJSONRPCRequestHandler.log_request = log_request
server = SimpleJSONRPCServer(addr, address_family=address_family)
- server.register_function(summation, 'sum')
- server.register_function(summation, 'notify_sum')
- server.register_function(notify_hello)
- server.register_function(subtract)
- server.register_function(update)
- server.register_function(get_data)
- server.register_function(add)
- server.register_function(ping)
- server.register_function(summation, 'namespace.sum')
+ service = ExampleAggregateService()
+ # Expose an instance of the service
+ server.register_instance(service, allow_dotted_names=True)
+ # Expose some aliases for service methods
+ server.register_function(service.summation, 'sum')
+ server.register_function(service.summation, 'notify_sum')
+ server.register_function(service.summation, 'namespace.sum')
server_proc = Thread(target=server.serve_forever)
server_proc.daemon = True
server_proc.start()