From e09bd389a7d94f2bff28290355ced6f6b24436c7 Mon Sep 17 00:00:00 2001 From: Edmund Fokschaner Date: Fri, 6 Dec 2013 20:29:33 -0800 Subject: Fixing bug with jsonrpc._Method caused by debugger Adding __repr__, __str__ and __dir__ to jsonrpc._Method in order to avoid issues with PyCharm debugger due to these functions being called and accessing __getattr__ which mutates the _Method object --- jsonrpclib/jsonrpc.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/jsonrpclib/jsonrpc.py b/jsonrpclib/jsonrpc.py index e11939a..c46f1be 100644 --- a/jsonrpclib/jsonrpc.py +++ b/jsonrpclib/jsonrpc.py @@ -282,6 +282,15 @@ class _Method(XML_Method): # The only thing that changes is the 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): def __init__(self, request): self._request = request -- cgit v1.2.1 From 812a6834eec8b8286f4312b3fb8f748da714329d Mon Sep 17 00:00:00 2001 From: Edmund Fokschaner Date: Sat, 26 Sep 2015 17:56:44 -0700 Subject: Revert to xmlrpclib implementation of _Method.__getattr__ --- jsonrpclib/jsonrpc.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/jsonrpclib/jsonrpc.py b/jsonrpclib/jsonrpc.py index c46f1be..3812b34 100644 --- a/jsonrpclib/jsonrpc.py +++ b/jsonrpclib/jsonrpc.py @@ -276,11 +276,7 @@ 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) -- cgit v1.2.1 From bb79051c2f49b7f90e97756e5ccabf91fe513046 Mon Sep 17 00:00:00 2001 From: efokschaner Date: Thu, 8 Oct 2015 16:46:49 -0700 Subject: Add a test that relies on _Method's immutability --- tests.py | 74 +++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 31 deletions(-) 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() -- cgit v1.2.1