|
7 | 7 | import inspect |
8 | 8 |
|
9 | 9 | getargspec = inspect.getargspec |
10 | | -getcallargs = inspect.getcallargs |
11 | 10 | getsource = inspect.getsource |
| 11 | +try: |
| 12 | + getcallargs = inspect.getcallargs |
| 13 | +except AttributeError: |
| 14 | + # No `getcallargs` on Python 2.6 |
| 15 | + def getcallargs(func, *positional, **named): |
| 16 | + """Get the mapping of arguments to values. |
| 17 | + |
| 18 | + A dict is returned, with keys the function argument names (including the |
| 19 | + names of the * and ** arguments, if any), and values the respective bound |
| 20 | + values from 'positional' and 'named'.""" |
| 21 | + # from inspect import * |
| 22 | + args, varargs, varkw, defaults = getargspec(func) |
| 23 | + f_name = func.__name__ |
| 24 | + arg2value = {} |
| 25 | + |
| 26 | + # The following closures are basically because of tuple parameter unpacking. |
| 27 | + assigned_tuple_params = [] |
| 28 | + def assign(arg, value): |
| 29 | + if isinstance(arg, str): |
| 30 | + arg2value[arg] = value |
| 31 | + else: |
| 32 | + assigned_tuple_params.append(arg) |
| 33 | + value = iter(value) |
| 34 | + for i, subarg in enumerate(arg): |
| 35 | + try: |
| 36 | + subvalue = next(value) |
| 37 | + except StopIteration: |
| 38 | + raise ValueError('need more than %d %s to unpack' % |
| 39 | + (i, 'values' if i > 1 else 'value')) |
| 40 | + assign(subarg,subvalue) |
| 41 | + try: |
| 42 | + next(value) |
| 43 | + except StopIteration: |
| 44 | + pass |
| 45 | + else: |
| 46 | + raise ValueError('too many values to unpack') |
| 47 | + def is_assigned(arg): |
| 48 | + if isinstance(arg,str): |
| 49 | + return arg in arg2value |
| 50 | + return arg in assigned_tuple_params |
| 51 | + if ismethod(func) and func.im_self is not None: |
| 52 | + # implicit 'self' (or 'cls' for classmethods) argument |
| 53 | + positional = (func.im_self,) + positional |
| 54 | + num_pos = len(positional) |
| 55 | + num_total = num_pos + len(named) |
| 56 | + num_args = len(args) |
| 57 | + num_defaults = len(defaults) if defaults else 0 |
| 58 | + for arg, value in zip(args, positional): |
| 59 | + assign(arg, value) |
| 60 | + if varargs: |
| 61 | + if num_pos > num_args: |
| 62 | + assign(varargs, positional[-(num_pos-num_args):]) |
| 63 | + else: |
| 64 | + assign(varargs, ()) |
| 65 | + elif 0 < num_args < num_pos: |
| 66 | + raise TypeError('%s() takes %s %d %s (%d given)' % ( |
| 67 | + f_name, 'at most' if defaults else 'exactly', num_args, |
| 68 | + 'arguments' if num_args > 1 else 'argument', num_total)) |
| 69 | + elif num_args == 0 and num_total: |
| 70 | + if varkw: |
| 71 | + if num_pos: |
| 72 | + # XXX: We should use num_pos, but Python also uses num_total: |
| 73 | + raise TypeError('%s() takes exactly 0 arguments ' |
| 74 | + '(%d given)' % (f_name, num_total)) |
| 75 | + else: |
| 76 | + raise TypeError('%s() takes no arguments (%d given)' % |
| 77 | + (f_name, num_total)) |
| 78 | + for arg in args: |
| 79 | + if isinstance(arg, str) and arg in named: |
| 80 | + if is_assigned(arg): |
| 81 | + raise TypeError("%s() got multiple values for keyword " |
| 82 | + "argument '%s'" % (f_name, arg)) |
| 83 | + else: |
| 84 | + assign(arg, named.pop(arg)) |
| 85 | + if defaults: # fill in any missing values with the defaults |
| 86 | + for arg, value in zip(args[-num_defaults:], defaults): |
| 87 | + if not is_assigned(arg): |
| 88 | + assign(arg, value) |
| 89 | + if varkw: |
| 90 | + assign(varkw, named) |
| 91 | + elif named: |
| 92 | + unexpected = next(iter(named)) |
| 93 | + if isinstance(unexpected, unicode): |
| 94 | + unexpected = unexpected.encode(sys.getdefaultencoding(), 'replace') |
| 95 | + raise TypeError("%s() got an unexpected keyword argument '%s'" % |
| 96 | + (f_name, unexpected)) |
| 97 | + unassigned = num_args - len([arg for arg in args if is_assigned(arg)]) |
| 98 | + if unassigned: |
| 99 | + num_required = num_args - num_defaults |
| 100 | + raise TypeError('%s() takes %s %d %s (%d given)' % ( |
| 101 | + f_name, 'at least' if defaults else 'exactly', num_required, |
| 102 | + 'arguments' if num_required > 1 else 'argument', num_total)) |
| 103 | + return arg2value |
| 104 | + |
12 | 105 |
|
13 | 106 | ############################################################################### |
14 | 107 |
|
|
0 commit comments