Skip to content

Commit 02a1603

Browse files
authored
bpo-42000: Cleanup the AST related C-code (GH-22641)
- 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
1 parent 637a09b commit 02a1603

File tree

3 files changed

+5
-53
lines changed

3 files changed

+5
-53
lines changed

Parser/asdl_c.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -618,16 +618,6 @@ def visitField(self, field, name, sum=None, prod=None, depth=0):
618618
self.emit("}", depth)
619619

620620

621-
class MarshalPrototypeVisitor(PickleVisitor):
622-
623-
def prototype(self, sum, name):
624-
ctype = get_c_type(name)
625-
self.emit("static int marshal_write_%s(PyObject **, int *, %s);"
626-
% (name, ctype), 0)
627-
628-
visitProduct = visitSum = prototype
629-
630-
631621
class SequenceConstructorVisitor(EmitVisitor):
632622
def visitModule(self, mod):
633623
for dfn in mod.dfns:
@@ -1167,25 +1157,6 @@ def addObj(self, name):
11671157
self.emit("Py_INCREF(state->%s_type);" % name, 1)
11681158

11691159

1170-
_SPECIALIZED_SEQUENCES = ('stmt', 'expr')
1171-
1172-
def find_sequence(fields, doing_specialization):
1173-
"""Return True if any field uses a sequence."""
1174-
for f in fields:
1175-
if f.seq:
1176-
if not doing_specialization:
1177-
return True
1178-
if str(f.type) not in _SPECIALIZED_SEQUENCES:
1179-
return True
1180-
return False
1181-
1182-
def has_sequence(types, doing_specialization):
1183-
for t in types:
1184-
if find_sequence(t.fields, doing_specialization):
1185-
return True
1186-
return False
1187-
1188-
11891160
class StaticVisitor(PickleVisitor):
11901161
CODE = '''Very simple, always emit this static code. Override CODE'''
11911162

@@ -1283,18 +1254,6 @@ def emit(s, d):
12831254
emit("goto failed;", 1)
12841255
emit("Py_DECREF(value);", 0)
12851256

1286-
def emitSeq(self, field, value, depth, emit):
1287-
emit("seq = %s;" % value, 0)
1288-
emit("n = asdl_seq_LEN(seq);", 0)
1289-
emit("value = PyList_New(n);", 0)
1290-
emit("if (!value) goto failed;", 0)
1291-
emit("for (i = 0; i < n; i++) {", 0)
1292-
self.set("value", field, "asdl_seq_GET(seq, i)", depth + 1)
1293-
emit("if (!value1) goto failed;", 1)
1294-
emit("PyList_SET_ITEM(value, i, value1);", 1)
1295-
emit("value1 = NULL;", 1)
1296-
emit("}", 0)
1297-
12981257
def set(self, field, value, depth):
12991258
if field.seq:
13001259
# XXX should really check for is_simple, but that requires a symbol table
@@ -1313,7 +1272,6 @@ def set(self, field, value, depth):
13131272
else:
13141273
self.emit("value = ast2obj_list(state, (asdl_seq*)%s, ast2obj_%s);" % (value, field.type), depth)
13151274
else:
1316-
ctype = get_c_type(field.type)
13171275
self.emit("value = ast2obj_%s(state, %s);" % (field.type, value), depth, reflow=False)
13181276

13191277

Parser/pegen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,7 @@ _PyPegen_empty_arguments(Parser *p)
18971897
return NULL;
18981898
}
18991899

1900-
return _Py_arguments(posonlyargs, posargs, NULL, kwonlyargs, kwdefaults, NULL, kwdefaults,
1900+
return _Py_arguments(posonlyargs, posargs, NULL, kwonlyargs, kwdefaults, NULL, posdefaults,
19011901
p->arena);
19021902
}
19031903

Python/ast.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
/*
2-
* This file includes functions to transform a concrete syntax tree (CST) to
3-
* an abstract syntax tree (AST). The main function is PyAST_FromNode().
4-
*
2+
* This file exposes PyAST_Validate interface to check the integrity
3+
* of the given abstract syntax tree (potentially constructed manually).
54
*/
65
#include "Python.h"
76
#include "Python-ast.h"
87
#include "ast.h"
9-
#include "token.h"
10-
#include "pythonrun.h"
118

129
#include <assert.h>
13-
#include <stdbool.h>
14-
15-
#define MAXLEVEL 200 /* Max parentheses level */
1610

1711
static int validate_stmts(asdl_stmt_seq *);
1812
static int validate_exprs(asdl_expr_seq*, expr_context_ty, int);
@@ -62,7 +56,7 @@ validate_keywords(asdl_keyword_seq *keywords)
6256
{
6357
Py_ssize_t i;
6458
for (i = 0; i < asdl_seq_LEN(keywords); i++)
65-
if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load))
59+
if (!validate_expr((asdl_seq_GET(keywords, i))->value, Load))
6660
return 0;
6761
return 1;
6862
}
@@ -556,7 +550,7 @@ _PyAST_GetDocString(asdl_stmt_seq *body)
556550
if (!asdl_seq_LEN(body)) {
557551
return NULL;
558552
}
559-
stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0);
553+
stmt_ty st = asdl_seq_GET(body, 0);
560554
if (st->kind != Expr_kind) {
561555
return NULL;
562556
}

0 commit comments

Comments
 (0)