bpo-43244: Rename pycore_ast.h functions to _PyAST_xxx() (GH-25252)

Rename AST functions of pycore_ast.h to use the "_PyAST_" prefix.
Remove macros creating aliases without prefix. For example, Module()
becomes _PyAST_Module(). Update Grammar/python.gram to use
_PyAST_xxx() functions.
This commit is contained in:
Victor Stinner 2021-04-07 21:34:22 +02:00 committed by GitHub
parent 58d72cab89
commit d27f8d2e07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 858 additions and 907 deletions

View file

@ -264,6 +264,10 @@ class StructVisitor(EmitVisitor):
self.emit("", depth)
def ast_func_name(name):
return f"_PyAST_{name}"
class PrototypeVisitor(EmitVisitor):
"""Generate function prototypes for the .h file"""
@ -322,16 +326,7 @@ class PrototypeVisitor(EmitVisitor):
argstr += ", PyArena *arena"
else:
argstr = "PyArena *arena"
margs = "a0"
for i in range(1, len(args)+1):
margs += ", a%d" % i
# bpo-43244: <winbase.h> defines Yield macro. Don't redefine it in
# pycore_ast.h: it is not needed outside Python-ast.c which calls
# directly _Py_Yield().
if name != "Yield":
self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0,
reflow=False)
self.emit("%s _Py_%s(%s);" % (ctype, name, argstr), False)
self.emit("%s %s(%s);" % (ctype, ast_func_name(name), argstr), False)
def visitProduct(self, prod, name):
self.emit_function(name, get_c_type(name),
@ -340,10 +335,6 @@ class PrototypeVisitor(EmitVisitor):
union=False)
def pyfunc_name(name):
return f"_Py_{name}"
class FunctionVisitor(PrototypeVisitor):
"""Visitor to generate constructor functions for AST."""
@ -357,7 +348,7 @@ class FunctionVisitor(PrototypeVisitor):
else:
argstr = "PyArena *arena"
self.emit("%s" % ctype, 0)
emit("%s(%s)" % (pyfunc_name(name), argstr))
emit("%s(%s)" % (ast_func_name(name), argstr))
emit("{")
emit("%s p;" % ctype, 1)
for argtype, argname, opt in args:
@ -496,7 +487,7 @@ class Obj2ModVisitor(PickleVisitor):
for f in t.fields:
self.visitField(f, t.name, sum=sum, depth=2)
args = [f.name for f in t.fields] + [a.name for a in sum.attributes]
self.emit("*out = %s(%s);" % (pyfunc_name(t.name), self.buildArgs(args)), 2)
self.emit("*out = %s(%s);" % (ast_func_name(t.name), self.buildArgs(args)), 2)
self.emit("if (*out == NULL) goto failed;", 2)
self.emit("return 0;", 2)
self.emit("}", 1)
@ -529,7 +520,7 @@ class Obj2ModVisitor(PickleVisitor):
self.visitField(a, name, prod=prod, depth=1)
args = [f.name for f in prod.fields]
args.extend([a.name for a in prod.attributes])
self.emit("*out = %s(%s);" % (pyfunc_name(name), self.buildArgs(args)), 1)
self.emit("*out = %s(%s);" % (ast_func_name(name), self.buildArgs(args)), 1)
self.emit("return 0;", 1)
self.emit("failed:", 0)
self.emit("Py_XDECREF(tmp);", 1)

File diff suppressed because it is too large Load diff

View file

@ -34,9 +34,9 @@ _PyPegen_add_type_comment_to_arg(Parser *p, arg_ty a, Token *tc)
if (tco == NULL) {
return NULL;
}
return arg(a->arg, a->annotation, tco,
a->lineno, a->col_offset, a->end_lineno, a->end_col_offset,
p->arena);
return _PyAST_arg(a->arg, a->annotation, tco,
a->lineno, a->col_offset, a->end_lineno, a->end_col_offset,
p->arena);
}
static int
@ -568,7 +568,7 @@ _PyPegen_dummy_name(Parser *p, ...)
if (!id) {
return NULL;
}
cache = Name(id, Load, 1, 0, 1, 0, p->arena);
cache = _PyAST_Name(id, Load, 1, 0, 1, 0, p->arena);
return cache;
}
@ -919,8 +919,8 @@ _PyPegen_name_token(Parser *p)
p->error_indicator = 1;
return NULL;
}
return Name(id, Load, t->lineno, t->col_offset, t->end_lineno, t->end_col_offset,
p->arena);
return _PyAST_Name(id, Load, t->lineno, t->col_offset, t->end_lineno,
t->end_col_offset, p->arena);
}
void *
@ -1035,8 +1035,8 @@ _PyPegen_number_token(Parser *p)
return NULL;
}
return Constant(c, NULL, t->lineno, t->col_offset, t->end_lineno, t->end_col_offset,
p->arena);
return _PyAST_Constant(c, NULL, t->lineno, t->col_offset, t->end_lineno,
t->end_col_offset, p->arena);
}
static int // bool
@ -1514,7 +1514,7 @@ _PyPegen_join_names_with_dot(Parser *p, expr_ty first_name, expr_ty second_name)
return NULL;
}
return _Py_Name(uni, Load, EXTRA_EXPR(first_name, second_name));
return _PyAST_Name(uni, Load, EXTRA_EXPR(first_name, second_name));
}
/* Counts the total number of dots in seq's tokens */
@ -1551,7 +1551,7 @@ _PyPegen_alias_for_star(Parser *p)
Py_DECREF(str);
return NULL;
}
return alias(str, NULL, p->arena);
return _PyAST_alias(str, NULL, p->arena);
}
/* Creates a new asdl_seq* with the identifiers of all the names in seq */
@ -1643,13 +1643,13 @@ _set_seq_context(Parser *p, asdl_expr_seq *seq, expr_context_ty ctx)
static expr_ty
_set_name_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _Py_Name(e->v.Name.id, ctx, EXTRA_EXPR(e, e));
return _PyAST_Name(e->v.Name.id, ctx, EXTRA_EXPR(e, e));
}
static expr_ty
_set_tuple_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _Py_Tuple(
return _PyAST_Tuple(
_set_seq_context(p, e->v.Tuple.elts, ctx),
ctx,
EXTRA_EXPR(e, e));
@ -1658,7 +1658,7 @@ _set_tuple_context(Parser *p, expr_ty e, expr_context_ty ctx)
static expr_ty
_set_list_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _Py_List(
return _PyAST_List(
_set_seq_context(p, e->v.List.elts, ctx),
ctx,
EXTRA_EXPR(e, e));
@ -1667,19 +1667,22 @@ _set_list_context(Parser *p, expr_ty e, expr_context_ty ctx)
static expr_ty
_set_subscript_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _Py_Subscript(e->v.Subscript.value, e->v.Subscript.slice, ctx, EXTRA_EXPR(e, e));
return _PyAST_Subscript(e->v.Subscript.value, e->v.Subscript.slice,
ctx, EXTRA_EXPR(e, e));
}
static expr_ty
_set_attribute_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _Py_Attribute(e->v.Attribute.value, e->v.Attribute.attr, ctx, EXTRA_EXPR(e, e));
return _PyAST_Attribute(e->v.Attribute.value, e->v.Attribute.attr,
ctx, EXTRA_EXPR(e, e));
}
static expr_ty
_set_starred_context(Parser *p, expr_ty e, expr_context_ty ctx)
{
return _Py_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx), ctx, EXTRA_EXPR(e, e));
return _PyAST_Starred(_PyPegen_set_expr_context(p, e->v.Starred.value, ctx),
ctx, EXTRA_EXPR(e, e));
}
/* Creates an `expr_ty` equivalent to `expr` but with `ctx` as context */
@ -1987,8 +1990,8 @@ _PyPegen_make_arguments(Parser *p, asdl_arg_seq *slash_without_default,
kwarg = star_etc->kwarg;
}
return _Py_arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg,
posdefaults, p->arena);
return _PyAST_arguments(posonlyargs, posargs, vararg, kwonlyargs,
kwdefaults, kwarg, posdefaults, p->arena);
}
/* Constructs an empty arguments_ty object, that gets used when a function accepts no
@ -2017,8 +2020,8 @@ _PyPegen_empty_arguments(Parser *p)
return NULL;
}
return _Py_arguments(posonlyargs, posargs, NULL, kwonlyargs, kwdefaults, NULL, posdefaults,
p->arena);
return _PyAST_arguments(posonlyargs, posargs, NULL, kwonlyargs,
kwdefaults, NULL, posdefaults, p->arena);
}
/* Encapsulates the value of an operator_ty into an AugOperator struct */
@ -2039,7 +2042,7 @@ _PyPegen_function_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty f
{
assert(function_def != NULL);
if (function_def->kind == AsyncFunctionDef_kind) {
return _Py_AsyncFunctionDef(
return _PyAST_AsyncFunctionDef(
function_def->v.FunctionDef.name, function_def->v.FunctionDef.args,
function_def->v.FunctionDef.body, decorators, function_def->v.FunctionDef.returns,
function_def->v.FunctionDef.type_comment, function_def->lineno,
@ -2047,12 +2050,13 @@ _PyPegen_function_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty f
p->arena);
}
return _Py_FunctionDef(function_def->v.FunctionDef.name, function_def->v.FunctionDef.args,
function_def->v.FunctionDef.body, decorators,
function_def->v.FunctionDef.returns,
function_def->v.FunctionDef.type_comment, function_def->lineno,
function_def->col_offset, function_def->end_lineno,
function_def->end_col_offset, p->arena);
return _PyAST_FunctionDef(
function_def->v.FunctionDef.name, function_def->v.FunctionDef.args,
function_def->v.FunctionDef.body, decorators,
function_def->v.FunctionDef.returns,
function_def->v.FunctionDef.type_comment, function_def->lineno,
function_def->col_offset, function_def->end_lineno,
function_def->end_col_offset, p->arena);
}
/* Construct a ClassDef equivalent to class_def, but with decorators */
@ -2060,10 +2064,11 @@ stmt_ty
_PyPegen_class_def_decorators(Parser *p, asdl_expr_seq *decorators, stmt_ty class_def)
{
assert(class_def != NULL);
return _Py_ClassDef(class_def->v.ClassDef.name, class_def->v.ClassDef.bases,
class_def->v.ClassDef.keywords, class_def->v.ClassDef.body, decorators,
class_def->lineno, class_def->col_offset, class_def->end_lineno,
class_def->end_col_offset, p->arena);
return _PyAST_ClassDef(
class_def->v.ClassDef.name, class_def->v.ClassDef.bases,
class_def->v.ClassDef.keywords, class_def->v.ClassDef.body, decorators,
class_def->lineno, class_def->col_offset, class_def->end_lineno,
class_def->end_col_offset, p->arena);
}
/* Construct a KeywordOrStarred */
@ -2214,8 +2219,9 @@ _PyPegen_concatenate_strings(Parser *p, asdl_seq *strings)
if (_PyArena_AddPyObject(p->arena, bytes_str) < 0) {
goto error;
}
return Constant(bytes_str, NULL, first->lineno, first->col_offset, last->end_lineno,
last->end_col_offset, p->arena);
return _PyAST_Constant(bytes_str, NULL, first->lineno,
first->col_offset, last->end_lineno,
last->end_col_offset, p->arena);
}
return _PyPegen_FstringParser_Finish(p, &state, first, last);
@ -2244,14 +2250,15 @@ _PyPegen_make_module(Parser *p, asdl_stmt_seq *a) {
if (tag == NULL) {
return NULL;
}
type_ignore_ty ti = TypeIgnore(p->type_ignore_comments.items[i].lineno, tag, p->arena);
type_ignore_ty ti = _PyAST_TypeIgnore(p->type_ignore_comments.items[i].lineno,
tag, p->arena);
if (ti == NULL) {
return NULL;
}
asdl_seq_SET(type_ignores, i, ti);
}
}
return Module(a, type_ignores, p->arena);
return _PyAST_Module(a, type_ignores, p->arena);
}
// Error reporting helpers
@ -2361,7 +2368,7 @@ expr_ty _PyPegen_collect_call_seqs(Parser *p, asdl_expr_seq *a, asdl_seq *b,
Py_ssize_t total_len = args_len;
if (b == NULL) {
return _Py_Call(_PyPegen_dummy_name(p), a, NULL, lineno, col_offset,
return _PyAST_Call(_PyPegen_dummy_name(p), a, NULL, lineno, col_offset,
end_lineno, end_col_offset, arena);
}
@ -2383,6 +2390,6 @@ expr_ty _PyPegen_collect_call_seqs(Parser *p, asdl_expr_seq *a, asdl_seq *b,
asdl_seq_SET(args, i, asdl_seq_GET(starreds, i - args_len));
}
return _Py_Call(_PyPegen_dummy_name(p), args, keywords, lineno,
col_offset, end_lineno, end_col_offset, arena);
return _PyAST_Call(_PyPegen_dummy_name(p), args, keywords, lineno,
col_offset, end_lineno, end_col_offset, arena);
}

View file

@ -797,10 +797,11 @@ fstring_find_expr(Parser *p, const char **str, const char *end, int raw, int rec
/* And now create the FormattedValue node that represents this
entire expression with the conversion and format spec. */
//TODO: Fix this
*expression = FormattedValue(simple_expression, conversion,
format_spec, first_token->lineno,
first_token->col_offset, last_token->end_lineno,
last_token->end_col_offset, p->arena);
*expression = _PyAST_FormattedValue(simple_expression, conversion,
format_spec, first_token->lineno,
first_token->col_offset,
last_token->end_lineno,
last_token->end_col_offset, p->arena);
if (!*expression) {
goto error;
}
@ -1044,8 +1045,9 @@ make_str_node_and_del(Parser *p, PyObject **str, Token* first_token, Token *last
return NULL;
}
return Constant(s, kind, first_token->lineno, first_token->col_offset,
last_token->end_lineno, last_token->end_col_offset, p->arena);
return _PyAST_Constant(s, kind, first_token->lineno, first_token->col_offset,
last_token->end_lineno, last_token->end_col_offset,
p->arena);
}
@ -1204,8 +1206,9 @@ _PyPegen_FstringParser_Finish(Parser *p, FstringParser *state, Token* first_toke
goto error;
}
return _Py_JoinedStr(seq, first_token->lineno, first_token->col_offset,
last_token->end_lineno, last_token->end_col_offset, p->arena);
return _PyAST_JoinedStr(seq, first_token->lineno, first_token->col_offset,
last_token->end_lineno, last_token->end_col_offset,
p->arena);
error:
_PyPegen_FstringParser_Dealloc(state);