Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Merge pull request #167 from alexcepoi/sep-017

Pablo Hoffmanc380910b40a9
diff --cc scrapy/utils/python.py
index 53158fa92,64117d1d1..44808e488
--- a/scrapy/utils/python.py
+++ b/scrapy/utils/python.py
@@@ -150,23 -149,51 +150,59 @@@ def get_func_args(func, stripself=False
"""Return the argument name list of a callable"""
if inspect.isfunction(func):
func_args, _, _, _ = inspect.getargspec(func)
+ elif inspect.isclass(func):
+ return get_func_args(func.__init__, True)
+ elif inspect.ismethod(func):
+ return get_func_args(func.__func__, True)
+ elif inspect.ismethoddescriptor(func):
+ return []
elif hasattr(func, '__call__'):
- try:
- func_args, _, _, _ = inspect.getargspec(func.__call__)
- except Exception:
- func_args = []
+ if inspect.isroutine(func):
+ return []
+ else:
+ return get_func_args(func.__call__, True)
else:
raise TypeError('%s is not callable' % type(func))
+ if stripself:
+ func_args.pop(0)
return func_args
+ def get_spec(func):
+ """Returns (args, kwargs) tuple for a function
+ >>> import re
+ >>> get_spec(re.match)
+ (['pattern', 'string'], {'flags': 0})
+
+ >>> class Test(object):
+ ... def __call__(self, val):
+ ... pass
+ ... def method(self, val, flags=0):
+ ... pass
+
+ >>> get_spec(Test)
+ (['self', 'val'], {})
+
+ >>> get_spec(Test.method)
+ (['self', 'val'], {'flags': 0})
+
+ >>> get_spec(Test().method)
+ (['self', 'val'], {'flags': 0})
+ """
+
+ if inspect.isfunction(func) or inspect.ismethod(func):
+ spec = inspect.getargspec(func)
+ elif hasattr(func, '__call__'):
+ spec = inspect.getargspec(func.__call__)
+ else:
+ raise TypeError('%s is not callable' % type(func))
+
+ defaults = spec.defaults or []
+
+ firstdefault = len(spec.args) - len(defaults)
+ args = spec.args[:firstdefault]
+ kwargs = dict(zip(spec.args[firstdefault:], defaults))
+ return args, kwargs
+
def equal_attributes(obj1, obj2, attributes):
"""Compare two objects attributes"""
# not attributes given return False by default