diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-09-27 14:42:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-27 14:42:37 (GMT) |
commit | 3f22811fef73aec848d961593d95fa877f77ecbf (patch) | |
tree | 025ca176b2993e8d85a0961f994794c3f9801032 /Tools | |
parent | a94ee12c26aa8dd7dce01373779df8055aff765b (diff) | |
download | cpython-3f22811fef73aec848d961593d95fa877f77ecbf.zip cpython-3f22811fef73aec848d961593d95fa877f77ecbf.tar.gz cpython-3f22811fef73aec848d961593d95fa877f77ecbf.tar.bz2 |
bpo-32892: Use ast.Constant instead of specific constant AST types. (GH-9445)
Diffstat (limited to 'Tools')
-rwxr-xr-x | Tools/clinic/clinic.py | 17 | ||||
-rw-r--r-- | Tools/parser/unparse.py | 23 |
2 files changed, 16 insertions, 24 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index a6a43d1..ca8096f 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -3840,9 +3840,6 @@ class DSLParser: # "starred": "a = [1, 2, 3]; *a" visit_Starred = bad_node - # allow ellipsis, for now - # visit_Ellipsis = bad_node - blacklist = DetectBadNodes() blacklist.visit(module) bad = blacklist.bad @@ -3868,10 +3865,15 @@ class DSLParser: py_default = 'None' c_default = "NULL" elif (isinstance(expr, ast.BinOp) or - (isinstance(expr, ast.UnaryOp) and not isinstance(expr.operand, ast.Num))): + (isinstance(expr, ast.UnaryOp) and + not (isinstance(expr.operand, ast.Num) or + (hasattr(ast, 'Constant') and + isinstance(expr.operand, ast.Constant) and + type(expr.operand.value) in (int, float, complex))) + )): c_default = kwargs.get("c_default") if not (isinstance(c_default, str) and c_default): - fail("When you specify an expression (" + repr(default) + ") as your default value,\nyou MUST specify a valid c_default.") + fail("When you specify an expression (" + repr(default) + ") as your default value,\nyou MUST specify a valid c_default." + ast.dump(expr)) py_default = default value = unknown elif isinstance(expr, ast.Attribute): @@ -3946,6 +3948,11 @@ class DSLParser: self.function.parameters[parameter_name] = p def parse_converter(self, annotation): + if (hasattr(ast, 'Constant') and + isinstance(annotation, ast.Constant) and + type(annotation.value) is str): + return annotation.value, True, {} + if isinstance(annotation, ast.Str): return annotation.s, True, {} diff --git a/Tools/parser/unparse.py b/Tools/parser/unparse.py index 7e1cc4e..82c3c77 100644 --- a/Tools/parser/unparse.py +++ b/Tools/parser/unparse.py @@ -329,12 +329,6 @@ class Unparser: self.leave() # expr - def _Bytes(self, t): - self.write(repr(t.s)) - - def _Str(self, tree): - self.write(repr(tree.s)) - def _JoinedStr(self, t): self.write("f") string = io.StringIO() @@ -352,10 +346,6 @@ class Unparser: meth = getattr(self, "_fstring_" + type(value).__name__) meth(value, write) - def _fstring_Str(self, t, write): - value = t.s.replace("{", "{{").replace("}", "}}") - write(value) - def _fstring_Constant(self, t, write): assert isinstance(t.value, str) value = t.value.replace("{", "{{").replace("}", "}}") @@ -384,6 +374,7 @@ class Unparser: def _write_constant(self, value): if isinstance(value, (float, complex)): + # Substitute overflowing decimal literal for AST infinities. self.write(repr(value).replace("inf", INFSTR)) else: self.write(repr(value)) @@ -398,16 +389,11 @@ class Unparser: else: interleave(lambda: self.write(", "), self._write_constant, value) self.write(")") + elif value is ...: + self.write("...") else: self._write_constant(t.value) - def _NameConstant(self, t): - self.write(repr(t.value)) - - def _Num(self, t): - # Substitute overflowing decimal literal for AST infinities. - self.write(repr(t.n).replace("inf", INFSTR)) - def _List(self, t): self.write("[") interleave(lambda: self.write(", "), self.dispatch, t.elts) @@ -539,8 +525,7 @@ class Unparser: # Special case: 3.__abs__() is a syntax error, so if t.value # is an integer literal then we need to either parenthesize # it or add an extra space to get 3 .__abs__(). - if ((isinstance(t.value, ast.Num) and isinstance(t.value.n, int)) - or (isinstance(t.value, ast.Constant) and isinstance(t.value.value, int))): + if isinstance(t.value, ast.Constant) and isinstance(t.value.value, int): self.write(" ") self.write(".") self.write(t.attr) |