summaryrefslogtreecommitdiffstats
path: root/Tools/clinic
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 /Tools/clinic
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 'Tools/clinic')
-rwxr-xr-xTools/clinic/clinic.py28
1 files changed, 23 insertions, 5 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index 5351b6d..3b167ea 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -1362,15 +1362,15 @@ class CConverter(metaclass=CConverterAutoRegister):
# Only used by format units ending with '#'.
length = False
- def __init__(self, name, function, default=unspecified, *, doc_default=None, required=False, annotation=unspecified, **kwargs):
+ def __init__(self, name, function, default=unspecified, *, doc_default=None, c_default=None, py_default=None, required=False, annotation=unspecified, **kwargs):
self.function = function
self.name = name
if default is not unspecified:
self.default = default
- self.py_default = py_repr(default)
+ self.py_default = py_default if py_default is not None else py_repr(default)
self.doc_default = doc_default if doc_default is not None else self.py_default
- self.c_default = c_repr(default)
+ self.c_default = c_default if c_default is not None else c_repr(default)
elif doc_default is not None:
fail(function.fullname + " argument " + name + " specified a 'doc_default' without having a 'default'")
if annotation != unspecified:
@@ -2315,18 +2315,36 @@ class DSLParser:
function_args = module.body[0].args
parameter = function_args.args[0]
+ py_default = None
+
+ parameter_name = parameter.arg
+ name, legacy, kwargs = self.parse_converter(parameter.annotation)
+
if function_args.defaults:
expr = function_args.defaults[0]
# mild hack: explicitly support NULL as a default value
if isinstance(expr, ast.Name) and expr.id == 'NULL':
value = NULL
+ elif isinstance(expr, ast.Attribute):
+ a = []
+ n = expr
+ while isinstance(n, ast.Attribute):
+ a.append(n.attr)
+ n = n.value
+ if not isinstance(n, ast.Name):
+ fail("Malformed default value (looked like a Python constant)")
+ a.append(n.id)
+ py_default = ".".join(reversed(a))
+ value = None
+ c_default = kwargs.get("c_default")
+ if not (isinstance(c_default, str) and c_default):
+ fail("When you specify a named constant (" + repr(py_default) + ") as your default value,\nyou MUST specify a valid c_default.")
+ kwargs["py_default"] = py_default
else:
value = ast.literal_eval(expr)
else:
value = unspecified
- parameter_name = parameter.arg
- name, legacy, kwargs = self.parse_converter(parameter.annotation)
dict = legacy_converters if legacy else converters
legacy_str = "legacy " if legacy else ""
if name not in dict: