Ver código fonte

[utils] .five.getfullargspec now returns the same fields as on Python3

Ask Solem 9 anos atrás
pai
commit
74d5bff213
2 arquivos alterados com 13 adições e 7 exclusões
  1. 11 1
      celery/five.py
  2. 2 6
      celery/utils/functional.py

+ 11 - 1
celery/five.py

@@ -28,7 +28,17 @@ except ImportError:
 try:  # pragma: no cover
     from inspect import formatargspec, getfullargspec
 except ImportError:  # Py2
-    from inspect import formatargspec, getargspec as getfullargspec  # noqa
+    from collections import namedtuple
+    from inspect import formatargspec, getargspec as _getargspec  # noqa
+
+    FullArgSpec = namedtuple('FullArgSpec', (
+        'args', 'varargs', 'varkw', 'defaults',
+        'kwonlyargs', 'kwonlydefaults', 'annotations',
+    ))
+
+    def getfullargspec(fun, _fill=(None, ) * 3):  # noqa
+        s = _getargspec(fun)
+        return FullArgSpec(*s + _fill)
 
 __all__ = [
     'class_property', 'reclassmethod', 'create_module', 'recreate_module',

+ 2 - 6
celery/utils/functional.py

@@ -367,15 +367,11 @@ def _argsfromspec(spec, replace_defaults=True):
         optional = list(zip(spec.args[-split:], defaults))
     else:
         positional, optional = spec.args, []
-    if IS_PY3:  # pragma: no cover
-        keywords = spec.varkw
-    elif IS_PY2:
-        keywords = spec.keywords  # noqa
     return ', '.join(filter(None, [
         ', '.join(positional),
         ', '.join('{0}={1}'.format(k, v) for k, v in optional),
         '*{0}'.format(spec.varargs) if spec.varargs else None,
-        '**{0}'.format(keywords) if keywords else None,
+        '**{0}'.format(spec.varkw) if spec.varkw else None,
     ]))
 
 
@@ -403,6 +399,6 @@ def head_from_fun(fun, bound=False, debug=False):
 def fun_takes_argument(name, fun, position=None):
     spec = getfullargspec(fun)
     return (
-        spec.keywords or spec.varargs or
+        spec.varkw or spec.varargs or
         (len(spec.args) >= position if position else name in spec.args)
     )