]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42000: Cleanup the AST related C-code (GH-22641)
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>
Sat, 10 Oct 2020 17:14:59 +0000 (20:14 +0300)
committerGitHub <noreply@github.com>
Sat, 10 Oct 2020 17:14:59 +0000 (10:14 -0700)
- Use the proper asdl sequence when creating empty arguments
- Remove reduntant casts (thanks to new typed asdl_sequences)
- Remove MarshalPrototypeVisitor and some utilities from asdl generator
- Fix the header of `Python/ast.c` (kept from pgen times)

Automerge-Triggered-By: @pablogsal
Parser/asdl_c.py
Parser/pegen.c
Python/ast.c

index 242eccf3d37d783eeb2485db82f2ece395757e12..481261cd85359a7dcfb93436c6c8b39291145b24 100755 (executable)
@@ -618,16 +618,6 @@ class Obj2ModVisitor(PickleVisitor):
         self.emit("}", depth)
 
 
-class MarshalPrototypeVisitor(PickleVisitor):
-
-    def prototype(self, sum, name):
-        ctype = get_c_type(name)
-        self.emit("static int marshal_write_%s(PyObject **, int *, %s);"
-                  % (name, ctype), 0)
-
-    visitProduct = visitSum = prototype
-
-
 class SequenceConstructorVisitor(EmitVisitor):
     def visitModule(self, mod):
         for dfn in mod.dfns:
@@ -1167,25 +1157,6 @@ PyInit__ast(void)
         self.emit("Py_INCREF(state->%s_type);" % name, 1)
 
 
-_SPECIALIZED_SEQUENCES = ('stmt', 'expr')
-
-def find_sequence(fields, doing_specialization):
-    """Return True if any field uses a sequence."""
-    for f in fields:
-        if f.seq:
-            if not doing_specialization:
-                return True
-            if str(f.type) not in _SPECIALIZED_SEQUENCES:
-                return True
-    return False
-
-def has_sequence(types, doing_specialization):
-    for t in types:
-        if find_sequence(t.fields, doing_specialization):
-            return True
-    return False
-
-
 class StaticVisitor(PickleVisitor):
     CODE = '''Very simple, always emit this static code.  Override CODE'''
 
@@ -1283,18 +1254,6 @@ class ObjVisitor(PickleVisitor):
         emit("goto failed;", 1)
         emit("Py_DECREF(value);", 0)
 
-    def emitSeq(self, field, value, depth, emit):
-        emit("seq = %s;" % value, 0)
-        emit("n = asdl_seq_LEN(seq);", 0)
-        emit("value = PyList_New(n);", 0)
-        emit("if (!value) goto failed;", 0)
-        emit("for (i = 0; i < n; i++) {", 0)
-        self.set("value", field, "asdl_seq_GET(seq, i)", depth + 1)
-        emit("if (!value1) goto failed;", 1)
-        emit("PyList_SET_ITEM(value, i, value1);", 1)
-        emit("value1 = NULL;", 1)
-        emit("}", 0)
-
     def set(self, field, value, depth):
         if field.seq:
             # XXX should really check for is_simple, but that requires a symbol table
@@ -1313,7 +1272,6 @@ class ObjVisitor(PickleVisitor):
             else:
                 self.emit("value = ast2obj_list(state, (asdl_seq*)%s, ast2obj_%s);" % (value, field.type), depth)
         else:
-            ctype = get_c_type(field.type)
             self.emit("value = ast2obj_%s(state, %s);" % (field.type, value), depth, reflow=False)
 
 
index 1de495eaf398e84723febd0143fbdf88cb240ee6..efa5ed9f288ee0bc1a6d9669e9aaf7bd1a04abc3 100644 (file)
@@ -1897,7 +1897,7 @@ _PyPegen_empty_arguments(Parser *p)
         return NULL;
     }
 
-    return _Py_arguments(posonlyargs, posargs, NULL, kwonlyargs, kwdefaults, NULL, kwdefaults,
+    return _Py_arguments(posonlyargs, posargs, NULL, kwonlyargs, kwdefaults, NULL, posdefaults,
                          p->arena);
 }
 
index 4b7bbd229c99b256bca997760aca700435f6620e..5e74f65a2c013b9cb96dcf50b41766b0c6f6befa 100644 (file)
@@ -1,18 +1,12 @@
 /*
- * This file includes functions to transform a concrete syntax tree (CST) to
- * an abstract syntax tree (AST). The main function is PyAST_FromNode().
- *
+ * This file exposes PyAST_Validate interface to check the integrity
+ * of the given abstract syntax tree (potentially constructed manually).
  */
 #include "Python.h"
 #include "Python-ast.h"
 #include "ast.h"
-#include "token.h"
-#include "pythonrun.h"
 
 #include <assert.h>
-#include <stdbool.h>
-
-#define MAXLEVEL 200    /* Max parentheses level */
 
 static int validate_stmts(asdl_stmt_seq *);
 static int validate_exprs(asdl_expr_seq*, expr_context_ty, int);
@@ -62,7 +56,7 @@ validate_keywords(asdl_keyword_seq *keywords)
 {
     Py_ssize_t i;
     for (i = 0; i < asdl_seq_LEN(keywords); i++)
-        if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
+        if (!validate_expr((asdl_seq_GET(keywords, i))->value, Load))
             return 0;
     return 1;
 }
@@ -556,7 +550,7 @@ _PyAST_GetDocString(asdl_stmt_seq *body)
     if (!asdl_seq_LEN(body)) {
         return NULL;
     }
-    stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
+    stmt_ty st = asdl_seq_GET(body, 0);
     if (st->kind != Expr_kind) {
         return NULL;
     }