diff options
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 127 |
1 files changed, 76 insertions, 51 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 2031755..fc9f612 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -33,7 +33,6 @@ import sys import os import types import itertools -import string import re import imp import tokenize @@ -931,6 +930,43 @@ def formatargvalues(args, varargs, varkw, locals, specs.append(formatvarkw(varkw) + formatvalue(locals[varkw])) return '(' + ', '.join(specs) + ')' +def _missing_arguments(f_name, argnames, pos, values): + names = [repr(name) for name in argnames if name not in values] + missing = len(names) + if missing == 1: + s = names[0] + elif missing == 2: + s = "{} and {}".format(*names) + else: + tail = ", {} and {}".format(names[-2:]) + del names[-2:] + s = ", ".join(names) + tail + raise TypeError("%s() missing %i required %s argument%s: %s" % + (f_name, missing, + "positional" if pos else "keyword-only", + "" if missing == 1 else "s", s)) + +def _too_many(f_name, args, kwonly, varargs, defcount, given, values): + atleast = len(args) - defcount + kwonly_given = len([arg for arg in kwonly if arg in values]) + if varargs: + plural = atleast != 1 + sig = "at least %d" % (atleast,) + elif defcount: + plural = True + sig = "from %d to %d" % (atleast, len(args)) + else: + plural = len(args) != 1 + sig = str(len(args)) + kwonly_sig = "" + if kwonly_given: + msg = " positional argument%s (and %d keyword-only argument%s)" + kwonly_sig = (msg % ("s" if given != 1 else "", kwonly_given, + "s" if kwonly_given != 1 else "")) + raise TypeError("%s() takes %s positional argument%s but %d%s %s given" % + (f_name, sig, "s" if plural else "", given, kwonly_sig, + "was" if given == 1 and not kwonly_given else "were")) + def getcallargs(func, *positional, **named): """Get the mapping of arguments to values. @@ -942,64 +978,53 @@ def getcallargs(func, *positional, **named): f_name = func.__name__ arg2value = {} + if ismethod(func) and func.__self__ is not None: # implicit 'self' (or 'cls' for classmethods) argument positional = (func.__self__,) + positional num_pos = len(positional) - num_total = num_pos + len(named) num_args = len(args) num_defaults = len(defaults) if defaults else 0 - for arg, value in zip(args, positional): - arg2value[arg] = value + + n = min(num_pos, num_args) + for i in range(n): + arg2value[args[i]] = positional[i] if varargs: - if num_pos > num_args: - arg2value[varargs] = positional[-(num_pos-num_args):] - else: - arg2value[varargs] = () - elif 0 < num_args < num_pos: - raise TypeError('%s() takes %s %d positional %s (%d given)' % ( - f_name, 'at most' if defaults else 'exactly', num_args, - 'arguments' if num_args > 1 else 'argument', num_total)) - elif num_args == 0 and num_total: - if varkw or kwonlyargs: - if num_pos: - # XXX: We should use num_pos, but Python also uses num_total: - raise TypeError('%s() takes exactly 0 positional arguments ' - '(%d given)' % (f_name, num_total)) - else: - raise TypeError('%s() takes no arguments (%d given)' % - (f_name, num_total)) - - for arg in itertools.chain(args, kwonlyargs): - if arg in named: - if arg in arg2value: - raise TypeError("%s() got multiple values for keyword " - "argument '%s'" % (f_name, arg)) - else: - arg2value[arg] = named.pop(arg) - for kwonlyarg in kwonlyargs: - if kwonlyarg not in arg2value: - try: - arg2value[kwonlyarg] = kwonlydefaults[kwonlyarg] - except KeyError: - raise TypeError("%s() needs keyword-only argument %s" % - (f_name, kwonlyarg)) - if defaults: # fill in any missing values with the defaults - for arg, value in zip(args[-num_defaults:], defaults): - if arg not in arg2value: - arg2value[arg] = value + arg2value[varargs] = tuple(positional[n:]) + possible_kwargs = set(args + kwonlyargs) if varkw: - arg2value[varkw] = named - elif named: - unexpected = next(iter(named)) - raise TypeError("%s() got an unexpected keyword argument '%s'" % - (f_name, unexpected)) - unassigned = num_args - len([arg for arg in args if arg in arg2value]) - if unassigned: - num_required = num_args - num_defaults - raise TypeError('%s() takes %s %d %s (%d given)' % ( - f_name, 'at least' if defaults else 'exactly', num_required, - 'arguments' if num_required > 1 else 'argument', num_total)) + arg2value[varkw] = {} + for kw, value in named.items(): + if kw not in possible_kwargs: + if not varkw: + raise TypeError("%s() got an unexpected keyword argument %r" % + (f_name, kw)) + arg2value[varkw][kw] = value + continue + if kw in arg2value: + raise TypeError("%s() got multiple values for argument %r" % + (f_name, kw)) + arg2value[kw] = value + if num_pos > num_args and not varargs: + _too_many(f_name, args, kwonlyargs, varargs, num_defaults, + num_pos, arg2value) + if num_pos < num_args: + req = args[:num_args - num_defaults] + for arg in req: + if arg not in arg2value: + _missing_arguments(f_name, req, True, arg2value) + for i, arg in enumerate(args[num_args - num_defaults:]): + if arg not in arg2value: + arg2value[arg] = defaults[i] + missing = 0 + for kwarg in kwonlyargs: + if kwarg not in arg2value: + if kwarg in kwonlydefaults: + arg2value[kwarg] = kwonlydefaults[kwarg] + else: + missing += 1 + if missing: + _missing_arguments(f_name, kwonlyargs, False, arg2value) return arg2value # -------------------------------------------------- stack frame extraction |