diff options
Diffstat (limited to 'sphinx/util')
-rw-r--r-- | sphinx/util/inspect.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index a0574003..c136393f 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -9,6 +9,40 @@ :license: BSD, see LICENSE for details. """ +inspect = __import__('inspect') +import sys + +if sys.version_info >= (2, 5): + from functools import partial + def getargspec(func): + """Like inspect.getargspec but supports functools.partial as well.""" + if inspect.ismethod(func): + func = func.im_func + parts = 0, () + if type(func) is partial: + parts = len(func.args), func.keywords.keys() + func = func.func + if not inspect.isfunction(func): + raise TypeError('%r is not a Python function' % func) + args, varargs, varkw = inspect.getargs(func.func_code) + func_defaults = func.func_defaults + if func_defaults: + func_defaults = list(func_defaults) + if parts[0]: + args = args[parts[0]:] + if parts[1]: + for arg in parts[1]: + i = args.index(arg) - len(args) + del args[i] + try: + del func_defaults[i] + except IndexError: + pass + return inspect.ArgSpec(args, varargs, varkw, func_defaults) +else: + getargspec = inspect.getargspec + + def isdescriptor(x): """Check if the object is some kind of descriptor.""" for item in '__get__', '__set__', '__delete__': |