Skip to content

Commit 6b69154

Browse files
committed
Merge pull request joshmarshall#24 from efokschaner/master
Fixing bug with jsonrpc._Method caused by debugger
2 parents 394d586 + bb79051 commit 6b69154

File tree

2 files changed

+53
-36
lines changed

2 files changed

+53
-36
lines changed

jsonrpclib/jsonrpc.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,16 @@ def __call__(self, *args, **kwargs):
290290
return self.__send(self.__name, kwargs)
291291

292292
def __getattr__(self, name):
293-
self.__name = '%s.%s' % (self.__name, name)
294-
return self
295-
# The old method returned a new instance, but this seemed wasteful.
296-
# The only thing that changes is the name.
297-
# return _Method(self.__send, "%s.%s" % (self.__name, name))
293+
return _Method(self.__send, "%s.%s" % (self.__name, name))
294+
295+
def __repr__(self):
296+
return '<{} "{}">'.format(self.__class__.__name__, self.__name)
297+
298+
def __str__(self):
299+
return self.__repr__()
300+
301+
def __dir__(self):
302+
return self.__dict__.keys()
298303

299304

300305
class _Notify(object):

tests.py

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ def func():
357357
with self.assertRaises(raises[i]):
358358
func()
359359

360+
def test_proxy_object_reuse_is_allowed(self):
361+
client = self.get_client()
362+
sub_service_proxy = client.sub_service
363+
result = sub_service_proxy.subtract(5, 10)
364+
self.assertTrue(result == -5)
365+
result = sub_service_proxy.add(21, 21)
366+
self.assertTrue(result == 42)
360367

361368
if jsonrpc.USE_UNIX_SOCKETS:
362369
# We won't do these tests unless Unix Sockets are supported
@@ -412,36 +419,43 @@ def tearDown(self):
412419
jsonrpc.USE_UNIX_SOCKETS = self.original_value
413420

414421

415-
""" Test Methods """
416-
417-
418-
def subtract(minuend, subtrahend):
419-
""" Using the keywords from the JSON-RPC v2 doc """
420-
return minuend-subtrahend
421-
422-
423-
def add(x, y):
424-
return x + y
422+
class ExampleService(object):
423+
@staticmethod
424+
def subtract(minuend, subtrahend):
425+
""" Using the keywords from the JSON-RPC v2 doc """
426+
return minuend-subtrahend
425427

428+
@staticmethod
429+
def add(x, y):
430+
return x + y
426431

427-
def update(*args):
428-
return args
432+
@staticmethod
433+
def update(*args):
434+
return args
429435

436+
@staticmethod
437+
def summation(*args):
438+
return sum(args)
430439

431-
def summation(*args):
432-
return sum(args)
440+
@staticmethod
441+
def notify_hello(*args):
442+
return args
433443

444+
@staticmethod
445+
def get_data():
446+
return ['hello', 5]
434447

435-
def notify_hello(*args):
436-
return args
448+
@staticmethod
449+
def ping():
450+
return True
437451

438452

439-
def get_data():
440-
return ['hello', 5]
441-
442-
443-
def ping():
444-
return True
453+
class ExampleAggregateService(ExampleService):
454+
"""
455+
Exposes the inherited ExampleService and a second copy as sub_service
456+
"""
457+
def __init__(self):
458+
self.sub_service = ExampleService()
445459

446460

447461
def server_set_up(addr, address_family=socket.AF_INET):
@@ -452,15 +466,13 @@ def log_request(self, *args, **kwargs):
452466
pass
453467
SimpleJSONRPCRequestHandler.log_request = log_request
454468
server = SimpleJSONRPCServer(addr, address_family=address_family)
455-
server.register_function(summation, 'sum')
456-
server.register_function(summation, 'notify_sum')
457-
server.register_function(notify_hello)
458-
server.register_function(subtract)
459-
server.register_function(update)
460-
server.register_function(get_data)
461-
server.register_function(add)
462-
server.register_function(ping)
463-
server.register_function(summation, 'namespace.sum')
469+
service = ExampleAggregateService()
470+
# Expose an instance of the service
471+
server.register_instance(service, allow_dotted_names=True)
472+
# Expose some aliases for service methods
473+
server.register_function(service.summation, 'sum')
474+
server.register_function(service.summation, 'notify_sum')
475+
server.register_function(service.summation, 'namespace.sum')
464476
server_proc = Thread(target=server.serve_forever)
465477
server_proc.daemon = True
466478
server_proc.start()

0 commit comments

Comments
 (0)