summaryrefslogtreecommitdiffstats
path: root/Tools/clinic/clinic.py
diff options
context:
space:
mode:
authorZachary Ware <zachary.ware@gmail.com>2014-01-25 04:52:30 (GMT)
committerZachary Ware <zachary.ware@gmail.com>2014-01-25 04:52:30 (GMT)
commit021bb87845e5727b370d6d05986d08f930bcc664 (patch)
tree05e20948c027648ae6fc9ef8a67e297da50052ec /Tools/clinic/clinic.py
parent66964cd4771b55b7d3c06aa3b10f16344e07383e (diff)
downloadcpython-021bb87845e5727b370d6d05986d08f930bcc664.zip
cpython-021bb87845e5727b370d6d05986d08f930bcc664.tar.gz
cpython-021bb87845e5727b370d6d05986d08f930bcc664.tar.bz2
Issue #20381: Fix sanity checking on default arguments when c_default is
also specified.
Diffstat (limited to 'Tools/clinic/clinic.py')
-rwxr-xr-xTools/clinic/clinic.py14
1 files changed, 11 insertions, 3 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
index 8bf8977..d33abae 100755
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -3279,11 +3279,11 @@ class DSLParser:
fail("You can't specify py_default without specifying a default value!")
else:
default = default.strip()
+ bad = False
ast_input = "x = {}".format(default)
try:
module = ast.parse(ast_input)
- bad = False
if 'c_default' not in kwargs:
# we can only represent very simple data values in C.
# detect whether default is okay, via a blacklist
@@ -3317,8 +3317,16 @@ class DSLParser:
bad = blacklist.bad
else:
# if they specify a c_default, we can be more lenient about the default value.
- # but at least ensure that we can turn it into text and reconstitute it correctly.
- bad = default != repr(eval(default))
+ # but at least make an attempt at ensuring it's a valid expression.
+ try:
+ value = eval(default)
+ if value == unspecified:
+ fail("'unspecified' is not a legal default value!")
+ except NameError:
+ pass # probably a named constant
+ except Exception as e:
+ fail("Malformed expression given as default value\n"
+ "{!r} caused {!r}".format(default, e))
if bad:
fail("Unsupported expression as default value: " + repr(default))