args_from_args() to work with an instance of UserType

When the args_from_args() function fails to convert a value to a certain
type it tries to get the name of that type by using the __name__ attribute
of that type, UserType object instances doesn't have the __name__
attribute so the function fails when it tries to access it, this patch
makes WSME to check if it's an UserType instance before getting it's name.

Change-Id: If78ce0e642997421c97559cc28604421f5c03922
Closes-Bug: #1265590
This commit is contained in:
Lucas Alvares Gomes 2014-01-02 19:25:45 +00:00
parent e26d1b608c
commit 228bfbd084
2 changed files with 29 additions and 2 deletions

View File

@ -171,10 +171,14 @@ def args_from_args(funcdef, args, kwargs):
try:
newargs.append(from_param(argdef.datatype, arg))
except Exception:
if isinstance(argdef.datatype, UserType):
datatype_name = argdef.datatype.name
else:
datatype_name = argdef.datatype.__name__
raise InvalidInput(
argdef.name,
arg,
"unable to convert to %s" % argdef.datatype.__name__)
"unable to convert to %s" % datatype_name)
newkwargs = {}
for argname, value in kwargs.items():
newkwargs[argname] = from_param(

View File

@ -3,7 +3,9 @@
import datetime
import unittest
from wsme.rest.args import from_param, from_params
from wsme.api import FunctionArgument, FunctionDefinition
from wsme.rest.args import from_param, from_params, args_from_args
from wsme.exc import InvalidInput
from wsme.types import UserType, Unset, ArrayType, DictType
@ -52,3 +54,24 @@ class TestProtocolsCommons(unittest.TestCase):
def test_from_params_dict_unset(self):
assert from_params(DictType(int, str), {}, 'a', set()) is Unset
def test_args_from_args_usertype(self):
class FakeType(UserType):
name = 'fake-type'
basetype = int
fake_type = FakeType()
fd = FunctionDefinition(FunctionDefinition)
fd.arguments.append(FunctionArgument('fake-arg', fake_type, True, 0))
new_args = args_from_args(fd, [1], {})
self.assertEqual([1], new_args[0])
# can't convert str to int
try:
args_from_args(fd, ['invalid-argument'], {})
except InvalidInput as e:
assert fake_type.name in str(e)
else:
self.fail('Should have thrown an InvalidInput')