diff options
author | Yury Selivanov <yselivanov@sprymix.com> | 2014-01-29 15:46:14 (GMT) |
---|---|---|
committer | Yury Selivanov <yselivanov@sprymix.com> | 2014-01-29 15:46:14 (GMT) |
commit | b77511da9257bae4082556e0235b23d008dfb789 (patch) | |
tree | 365ee26bea4e1c2af0a3ba7485325308ba021368 /Lib/inspect.py | |
parent | 9f2e46de34b28d20485ef4f1078c544238183537 (diff) | |
download | cpython-b77511da9257bae4082556e0235b23d008dfb789.zip cpython-b77511da9257bae4082556e0235b23d008dfb789.tar.gz cpython-b77511da9257bae4082556e0235b23d008dfb789.tar.bz2 |
inspect.Signature: Make from_builtin to raise an exception if no signature can
be provided #20422
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r-- | Lib/inspect.py | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py index 0072820..fefcddb 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1514,18 +1514,24 @@ def _signature_bound_method(sig): return sig.replace(parameters=params) +def _signature_is_builtin(obj): + # Internal helper to test if `obj` is a callable that might + # support Argument Clinic's __text_signature__ protocol. + return (isinstance(obj, _NonUserDefinedCallables) or + ismethoddescriptor(obj) or + # Can't test 'isinstance(type)' here, as it would + # also be True for regular python classes + obj in (type, object)) + + def signature(obj): '''Get a signature object for the passed callable.''' if not callable(obj): raise TypeError('{!r} is not a callable object'.format(obj)) - if (isinstance(obj, _NonUserDefinedCallables) or - ismethoddescriptor(obj) or - isinstance(obj, type)): - sig = Signature.from_builtin(obj) - if sig: - return sig + if _signature_is_builtin(obj): + return Signature.from_builtin(obj) if isinstance(obj, types.MethodType): # In this case we skip the first parameter of the underlying @@ -2017,9 +2023,13 @@ class Signature: @classmethod def from_builtin(cls, func): + if not _signature_is_builtin(func): + raise TypeError("{!r} is not a Python builtin " + "function".format(func)) + s = getattr(func, "__text_signature__", None) if not s: - return None + raise ValueError("no signature found for builtin {!r}".format(func)) Parameter = cls._parameter_cls @@ -2038,9 +2048,10 @@ class Signature: try: module = ast.parse(s) except SyntaxError: - return None + module = None + if not isinstance(module, ast.Module): - return None + raise ValueError("{!r} builtin has invalid signature".format(func)) f = module.body[0] @@ -2149,7 +2160,6 @@ class Signature: return cls(parameters, return_annotation=cls.empty) - @property def parameters(self): return self._parameters |