Skip to content

Commit

Permalink
Correct UnboundLocalError
Browse files Browse the repository at this point in the history
Occurs when jsonrpc method has PEP3107 annotations, and is called with
incorrect parameters.
  • Loading branch information
sethrh committed Mar 22, 2016
1 parent f35f16b commit 9914ab5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
23 changes: 23 additions & 0 deletions jsonrpc/tests/test_pep3107.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest
from manager import JSONRPCResponseManager


class TestJSONRPCResponseManager(unittest.TestCase):
def test_typeerror_with_annotations(self):
"""If a function has Python3 annotations and is called with improper
arguments, make sure the framework doesn't fail with inspect.getargspec
"""
def distance(a: float, b: float) -> float:
return (a**2 + b**2)**0.5

dispatcher = {
"distance": distance,
}

req = '{"jsonrpc": "2.0", "method": "distance", "params": [], "id": 3}' # noqa
result = JSONRPCResponseManager.handle(req, dispatcher)

# Make sure this returns JSONRPCInvalidParams rather than raising
# UnboundLocalError
self.assertEqual(result.error['code'], -32602)

7 changes: 6 additions & 1 deletion jsonrpc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ def is_invalid_params(func, *args, **kwargs):
if not inspect.isfunction(func):
return True

funcargs, varargs, varkwargs, defaults = inspect.getargspec(func)
try:
funcargs, varargs, varkwargs, defaults = inspect.getargspec(func)
except ValueError:
argspec = inspect.getfullargspec(func)
funcargs, varargs, varkwargs, defaults = argspec[:4]

if defaults:
funcargs = funcargs[:-len(defaults)]

Expand Down

0 comments on commit 9914ab5

Please sign in to comment.