From caa64908d2a2717270f1e20bc10f8dfd47a72ee5 Mon Sep 17 00:00:00 2001 From: Pablo Hoffman Date: Tue, 4 Sep 2012 18:07:44 -0300 Subject: [PATCH] added tests for get_func_args() and support for more cases --- scrapy/tests/test_utils_python.py | 30 +++++++++++++++++++++++++++++- scrapy/utils/python.py | 12 ++++++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/scrapy/tests/test_utils_python.py b/scrapy/tests/test_utils_python.py index ea9f0d949..e97833195 100644 --- a/scrapy/tests/test_utils_python.py +++ b/scrapy/tests/test_utils_python.py @@ -4,7 +4,7 @@ from itertools import count from scrapy.utils.python import str_to_unicode, unicode_to_str, \ memoizemethod_noargs, isbinarytext, equal_attributes, \ - WeakKeyCache, stringify_dict + WeakKeyCache, stringify_dict, get_func_args __doctests__ = ['scrapy.utils.python'] @@ -154,5 +154,33 @@ class UtilsPythonTestCase(unittest.TestCase): self.failIf(d is d2) # shouldn't modify in place self.failIf(any(isinstance(x, unicode) for x in d2.keys())) + def test_get_func_args(self): + def f1(a, b, c): + pass + + def f2(a, b=None, c=None): + pass + + class A(object): + def __init__(self, a, b, c): + pass + + def method(self, a, b, c): + pass + + class Callable(object): + + def __call__(self, a, b, c): + pass + + a = A(1, 2, 3) + cal = Callable() + + self.assertEqual(get_func_args(f1), ['a', 'b', 'c']) + self.assertEqual(get_func_args(f2), ['a', 'b', 'c']) + self.assertEqual(get_func_args(A), ['a', 'b', 'c']) + self.assertEqual(get_func_args(a.method), ['a', 'b', 'c']) + self.assertEqual(get_func_args(cal), ['a', 'b', 'c']) + if __name__ == "__main__": unittest.main() diff --git a/scrapy/utils/python.py b/scrapy/utils/python.py index 7cd02234e..64577415f 100644 --- a/scrapy/utils/python.py +++ b/scrapy/utils/python.py @@ -150,11 +150,15 @@ def get_func_args(func): """Return the argument name list of a callable""" if inspect.isfunction(func): func_args, _, _, _ = inspect.getargspec(func) + elif inspect.isclass(func): + func_args, _, _, _ = inspect.getargspec(func.__init__) + func_args.pop(0) # self + elif inspect.ismethod(func): + func_args, _, _, _ = inspect.getargspec(func.__func__) + func_args.pop(0) # self elif hasattr(func, '__call__'): - try: - func_args, _, _, _ = inspect.getargspec(func.__call__) - except Exception: - func_args = [] + func_args, _, _, _ = inspect.getargspec(func.__call__) + func_args.pop(0) # self else: raise TypeError('%s is not callable' % type(func)) return func_args