|
12 | 12 | import threading |
13 | 13 | import traceback |
14 | 14 | import collections |
| 15 | +from functools import partial |
| 16 | + |
| 17 | + |
| 18 | +def getargspec(func): |
| 19 | + """ |
| 20 | + Used because getargspec for python 2.7 does not accept functools.partial |
| 21 | + which is the type for pytest fixtures. |
| 22 | +
|
| 23 | + getargspec excerpted from: |
| 24 | +
|
| 25 | + sphinx.util.inspect |
| 26 | + ~~~~~~~~~~~~~~~~~~~ |
| 27 | + Helpers for inspecting Python modules. |
| 28 | + :copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS. |
| 29 | + :license: BSD, see LICENSE for details. |
| 30 | +
|
| 31 | + Like inspect.getargspec but supports functools.partial as well. |
| 32 | + """ |
| 33 | + # type: (Any) -> Any |
| 34 | + if inspect.ismethod(func): |
| 35 | + func = func.__func__ |
| 36 | + parts = 0, () # type: Tuple[int, Tuple[unicode, ...]] |
| 37 | + if type(func) is partial: |
| 38 | + keywords = func.keywords |
| 39 | + if keywords is None: |
| 40 | + keywords = {} |
| 41 | + parts = len(func.args), keywords.keys() |
| 42 | + func = func.func |
| 43 | + if not inspect.isfunction(func): |
| 44 | + raise TypeError('%r is not a Python function' % func) |
| 45 | + args, varargs, varkw = inspect.getargs(func.__code__) |
| 46 | + func_defaults = func.__defaults__ |
| 47 | + if func_defaults is None: |
| 48 | + func_defaults = [] |
| 49 | + else: |
| 50 | + func_defaults = list(func_defaults) |
| 51 | + if parts[0]: |
| 52 | + args = args[parts[0]:] |
| 53 | + if parts[1]: |
| 54 | + for arg in parts[1]: |
| 55 | + i = args.index(arg) - len(args) # type: ignore |
| 56 | + del args[i] |
| 57 | + try: |
| 58 | + del func_defaults[i] |
| 59 | + except IndexError: |
| 60 | + pass |
| 61 | + return inspect.ArgSpec(args, varargs, varkw, func_defaults) # type: ignore |
15 | 62 |
|
16 | 63 |
|
17 | 64 | if six.PY3: |
@@ -252,7 +299,7 @@ def func_parameters(func, *args, **kwargs): |
252 | 299 |
|
253 | 300 | """ |
254 | 301 | parameters = {} |
255 | | - arg_spec = inspect.getargspec(func) if six.PY2 else inspect.getfullargspec(func) |
| 302 | + arg_spec = getargspec(func) if six.PY2 else inspect.getfullargspec(func) |
256 | 303 | arg_order = list(arg_spec.args) |
257 | 304 | args_dict = dict(zip(arg_spec.args, args)) |
258 | 305 |
|
|
0 commit comments