diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2014-01-31 20:21:51 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2014-01-31 20:21:51 (GMT) |
commit | 5334bcdf97e3a926fd9c30e520ed31ccb01038db (patch) | |
tree | 0fa0fbba3901a3d01512def1a5213aacb5d5bd39 /Lib/inspect.py | |
parent | 63da7c7b0ca728a41b6269c4678392efb7f26625 (diff) | |
download | cpython-5334bcdf97e3a926fd9c30e520ed31ccb01038db.zip cpython-5334bcdf97e3a926fd9c30e520ed31ccb01038db.tar.gz cpython-5334bcdf97e3a926fd9c30e520ed31ccb01038db.tar.bz2 |
inspect.Signauture.from_function: validate duck functions in Signature constructor #17159
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index bc7eace..4842965 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2097,10 +2097,14 @@ class Signature: def from_function(cls, func): '''Constructs Signature for the given python function''' - if not (isfunction(func) or _signature_is_functionlike(func)): - # If it's not a pure Python function, and not a duck type - # of pure function: - raise TypeError('{!r} is not a Python function'.format(func)) + is_duck_function = False + if not isfunction(func): + if _signature_is_functionlike(func): + is_duck_function = True + else: + # If it's not a pure Python function, and not a duck type + # of pure function: + raise TypeError('{!r} is not a Python function'.format(func)) Parameter = cls._parameter_cls @@ -2164,9 +2168,11 @@ class Signature: parameters.append(Parameter(name, annotation=annotation, kind=_VAR_KEYWORD)) + # Is 'func' is a pure Python function - don't validate the + # parameters list (for correct order and defaults), it should be OK. return cls(parameters, return_annotation=annotations.get('return', _empty), - __validate_parameters__=False) + __validate_parameters__=is_duck_function) @classmethod def from_builtin(cls, func): |