changeset 19:636405ae3a66

Don't accept args and kwargs in calls
author Ben Croston <ben@croston.org>
date Sun, 11 Sep 2011 12:17:44 +0100
parents 072ee6d41aa7
children 4b62687da58a
files AuthRPC/client/__init__.py AuthRPC/tests.py
diffstat 2 files changed, 17 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/AuthRPC/client/__init__.py	Mon Sep 05 21:32:16 2011 +0100
+++ b/AuthRPC/client/__init__.py	Sun Sep 11 12:17:44 2011 +0100
@@ -46,14 +46,17 @@
         request['id'] = str(uuid4())
         request['method'] = self.name
 
-        if len(kwargs) is not 0:
+        if len(args) > 0 and len(kwargs) > 0:
+            raise ProtocolError('Cannot use both positional and keyword arguments.')
+
+        if len(args) > 0:
+            params = copy.copy(args)
+        elif len(kwargs) > 0:
             params = copy.copy(kwargs)
             index = 0
             for arg in args:
                 params[str(index)] = arg
                 index = index + 1
-        elif len(args) is not 0:
-            params = copy.copy(args)
         else:
             params = None
         request['params'] = params
@@ -135,6 +138,10 @@
     def __init__(self):
         Exception.__init__(self,'HTTP 502 - Bad Gateway')
 
+class ProtocolError(Exception):
+    """Raised when the JSONRPC protocol has been broken"""
+    pass
+
 class ServerProxy(object):
     """
     A client class to communicate with a AuthRPC server
--- a/AuthRPC/tests.py	Mon Sep 05 21:32:16 2011 +0100
+++ b/AuthRPC/tests.py	Sun Sep 11 12:17:44 2011 +0100
@@ -138,6 +138,12 @@
 class ReturnNothing(AuthRPCTests):
     def runTest(self):
         self.assertEqual(self.client.returnnothing(), None)
+
+class ProtocolErrorTest(AuthRPCTests):
+    def runTest(self):
+        from client import ProtocolError
+        with self.assertRaises(ProtocolError):
+            self.client.test(1, '2', three=3)
 ##### client ^^^ #####
 
 finished = False
@@ -162,6 +168,7 @@
     suite.addTest(BadRequestTest())
     suite.addTest(EchoTest())
     suite.addTest(ReturnNothing())
+    suite.addTest(ProtocolErrorTest())
     return suite
 
 if __name__ == '__main__':