summaryrefslogtreecommitdiffstats
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorLarry Hastings <larry@hastings.org>2014-01-07 19:53:01 (GMT)
committerLarry Hastings <larry@hastings.org>2014-01-07 19:53:01 (GMT)
commit16c5191ab3443aa5c1f835848514f94c696a8c4d (patch)
treea064b4a173dddc48b34bc48b95601809c3bf9ba7 /Lib/inspect.py
parent0bce6e746274980fb934ee8f2a06cbf8f8a54e3e (diff)
downloadcpython-16c5191ab3443aa5c1f835848514f94c696a8c4d.zip
cpython-16c5191ab3443aa5c1f835848514f94c696a8c4d.tar.gz
cpython-16c5191ab3443aa5c1f835848514f94c696a8c4d.tar.bz2
Issue #20144: Argument Clinic now supports simple constants as parameter
default values. inspect.Signature correspondingly supports them in __text_signature__ fields for builtins.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py56
1 files changed, 49 insertions, 7 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 7c954eb..c9d10dc 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -1974,18 +1974,60 @@ class Signature:
parameters = []
empty = Parameter.empty
+ invalid = object()
+
+ def parse_attribute(node):
+ if not isinstance(node.ctx, ast.Load):
+ return None
+
+ value = node.value
+ o = parse_node(value)
+ if o is invalid:
+ return invalid
+
+ if isinstance(value, ast.Name):
+ name = o
+ if name not in sys.modules:
+ return invalid
+ o = sys.modules[name]
+
+ return getattr(o, node.attr, invalid)
+
+ def parse_node(node):
+ if isinstance(node, ast.arg):
+ if node.annotation != None:
+ raise ValueError("Annotations are not currently supported")
+ return node.arg
+ if isinstance(node, ast.Num):
+ return node.n
+ if isinstance(node, ast.Str):
+ return node.s
+ if isinstance(node, ast.NameConstant):
+ return node.value
+ if isinstance(node, ast.Attribute):
+ return parse_attribute(node)
+ if isinstance(node, ast.Name):
+ if not isinstance(node.ctx, ast.Load):
+ return invalid
+ return node.id
+ return invalid
def p(name_node, default_node, default=empty):
- name = name_node.arg
-
- if isinstance(default_node, ast.Num):
- default = default.n
- elif isinstance(default_node, ast.NameConstant):
- default = default_node.value
+ name = parse_node(name_node)
+ if name is invalid:
+ return None
+ if default_node:
+ o = parse_node(default_node)
+ if o is invalid:
+ return None
+ default = o if o is not invalid else default
parameters.append(Parameter(name, kind, default=default, annotation=empty))
# non-keyword-only parameters
- for name, default in reversed(list(itertools.zip_longest(reversed(f.args.args), reversed(f.args.defaults), fillvalue=None))):
+ args = reversed(f.args.args)
+ defaults = reversed(f.args.defaults)
+ iter = itertools.zip_longest(args, defaults, fillvalue=None)
+ for name, default in reversed(list(iter)):
p(name, default)
# *args