Skip to content

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

Use getfullargspec under the scenes for py3 to stop DeprecationWarning (#2864)

Andrei Petre6e6b5cc29f15
diff --git a/scrapy/utils/python.py b/scrapy/utils/python.py
index 72f8f4311..732ca13a0 100644
--- a/scrapy/utils/python.py
+++ b/scrapy/utils/python.py
@@ -10,7 +10,6 @@ import errno
import six
from functools import partial, wraps
import sys
-import time
from scrapy.utils.decorators import deprecated
@@ -198,10 +197,30 @@ def binary_is_text(data):
return all(c not in _BINARYCHARS for c in data)
+def _getargspec_py23(func):
+ """_getargspec_py23(function) -> named tuple ArgSpec(args, varargs, keywords,
+ defaults)
+
+ Identical to inspect.getargspec() in python2, but uses
+ inspect.getfullargspec() for python3 behind the scenes to avoid
+ DeprecationWarning.
+
+ >>> def f(a, b=2, *ar, **kw):
+ ... pass
+
+ >>> _getargspec_py23(f)
+ ArgSpec(args=['a', 'b'], varargs='ar', keywords='kw', defaults=(2,))
+ """
+ if six.PY2:
+ return inspect.getargspec(func)
+
+ return inspect.ArgSpec(*inspect.getfullargspec(func)[:4])
+
+
def get_func_args(func, stripself=False):
"""Return the argument name list of a callable"""
if inspect.isfunction(func):
- func_args, _, _, _ = inspect.getargspec(func)
+ func_args, _, _, _ = _getargspec_py23(func)
elif inspect.isclass(func):
return get_func_args(func.__init__, True)
elif inspect.ismethod(func):
@@ -248,9 +267,9 @@ def get_spec(func):
"""
if inspect.isfunction(func) or inspect.ismethod(func):
- spec = inspect.getargspec(func)
+ spec = _getargspec_py23(func)
elif hasattr(func, '__call__'):
- spec = inspect.getargspec(func.__call__)
+ spec = _getargspec_py23(func.__call__)
else:
raise TypeError('%s is not callable' % type(func))