Skip to content

Commit d27f8d2

Browse files
authored
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.
1 parent 58d72ca commit d27f8d2

File tree

9 files changed

+851
-900
lines changed

9 files changed

+851
-900
lines changed

Grammar/python.gram

Lines changed: 148 additions & 148 deletions
Large diffs are not rendered by default.

Include/internal/pycore_ast.h

Lines changed: 171 additions & 230 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_peg_generator/test_c_parser.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ def run_test(self, grammar_source, test_source):
9696

9797
def test_c_parser(self) -> None:
9898
grammar_source = """
99-
start[mod_ty]: a[asdl_stmt_seq*]=stmt* $ { Module(a, NULL, p->arena) }
99+
start[mod_ty]: a[asdl_stmt_seq*]=stmt* $ { _PyAST_Module(a, NULL, p->arena) }
100100
stmt[stmt_ty]: a=expr_stmt { a }
101-
expr_stmt[stmt_ty]: a=expression NEWLINE { _Py_Expr(a, EXTRA) }
102-
expression[expr_ty]: ( l=expression '+' r=term { _Py_BinOp(l, Add, r, EXTRA) }
103-
| l=expression '-' r=term { _Py_BinOp(l, Sub, r, EXTRA) }
101+
expr_stmt[stmt_ty]: a=expression NEWLINE { _PyAST_Expr(a, EXTRA) }
102+
expression[expr_ty]: ( l=expression '+' r=term { _PyAST_BinOp(l, Add, r, EXTRA) }
103+
| l=expression '-' r=term { _PyAST_BinOp(l, Sub, r, EXTRA) }
104104
| t=term { t }
105105
)
106-
term[expr_ty]: ( l=term '*' r=factor { _Py_BinOp(l, Mult, r, EXTRA) }
107-
| l=term '/' r=factor { _Py_BinOp(l, Div, r, EXTRA) }
106+
term[expr_ty]: ( l=term '*' r=factor { _PyAST_BinOp(l, Mult, r, EXTRA) }
107+
| l=term '/' r=factor { _PyAST_BinOp(l, Div, r, EXTRA) }
108108
| f=factor { f }
109109
)
110110
factor[expr_ty]: ('(' e=expression ')' { e }
@@ -237,12 +237,12 @@ def test_nasty_mutually_left_recursive(self) -> None:
237237

238238
def test_return_stmt_noexpr_action(self) -> None:
239239
grammar_source = """
240-
start[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
240+
start[mod_ty]: a=[statements] ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
241241
statements[asdl_stmt_seq*]: a[asdl_stmt_seq*]=statement+ { a }
242242
statement[stmt_ty]: simple_stmt
243243
simple_stmt[stmt_ty]: small_stmt
244244
small_stmt[stmt_ty]: return_stmt
245-
return_stmt[stmt_ty]: a='return' NEWLINE { _Py_Return(NULL, EXTRA) }
245+
return_stmt[stmt_ty]: a='return' NEWLINE { _PyAST_Return(NULL, EXTRA) }
246246
"""
247247
test_source = """
248248
stmt = "return"
@@ -252,8 +252,8 @@ def test_return_stmt_noexpr_action(self) -> None:
252252

253253
def test_gather_action_ast(self) -> None:
254254
grammar_source = """
255-
start[mod_ty]: a[asdl_stmt_seq*]=';'.pass_stmt+ NEWLINE ENDMARKER { Module(a, NULL, p->arena) }
256-
pass_stmt[stmt_ty]: a='pass' { _Py_Pass(EXTRA)}
255+
start[mod_ty]: a[asdl_stmt_seq*]=';'.pass_stmt+ NEWLINE ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
256+
pass_stmt[stmt_ty]: a='pass' { _PyAST_Pass(EXTRA)}
257257
"""
258258
test_source = """
259259
stmt = "pass; pass"
@@ -263,12 +263,12 @@ def test_gather_action_ast(self) -> None:
263263

264264
def test_pass_stmt_action(self) -> None:
265265
grammar_source = """
266-
start[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
266+
start[mod_ty]: a=[statements] ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
267267
statements[asdl_stmt_seq*]: a[asdl_stmt_seq*]=statement+ { a }
268268
statement[stmt_ty]: simple_stmt
269269
simple_stmt[stmt_ty]: small_stmt
270270
small_stmt[stmt_ty]: pass_stmt
271-
pass_stmt[stmt_ty]: a='pass' NEWLINE { _Py_Pass(EXTRA) }
271+
pass_stmt[stmt_ty]: a='pass' NEWLINE { _PyAST_Pass(EXTRA) }
272272
"""
273273
test_source = """
274274
stmt = "pass"
@@ -278,7 +278,7 @@ def test_pass_stmt_action(self) -> None:
278278

279279
def test_if_stmt_action(self) -> None:
280280
grammar_source = """
281-
start[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
281+
start[mod_ty]: a=[statements] ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
282282
statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
283283
statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) } | simple_stmt
284284
@@ -290,11 +290,11 @@ def test_if_stmt_action(self) -> None:
290290
291291
compound_stmt: if_stmt
292292
293-
if_stmt: 'if' a=full_expression ':' b=block { _Py_If(a, b, NULL, EXTRA) }
293+
if_stmt: 'if' a=full_expression ':' b=block { _PyAST_If(a, b, NULL, EXTRA) }
294294
295295
small_stmt[stmt_ty]: pass_stmt
296296
297-
pass_stmt[stmt_ty]: a='pass' { _Py_Pass(EXTRA) }
297+
pass_stmt[stmt_ty]: a='pass' { _PyAST_Pass(EXTRA) }
298298
299299
full_expression: NAME
300300
"""
@@ -306,15 +306,15 @@ def test_if_stmt_action(self) -> None:
306306

307307
def test_same_name_different_types(self) -> None:
308308
grammar_source = """
309-
start[mod_ty]: a[asdl_stmt_seq*]=import_from+ NEWLINE ENDMARKER { Module(a, NULL, p->arena)}
309+
start[mod_ty]: a[asdl_stmt_seq*]=import_from+ NEWLINE ENDMARKER { _PyAST_Module(a, NULL, p->arena)}
310310
import_from[stmt_ty]: ( a='from' !'import' c=simple_name 'import' d=import_as_names_from {
311-
_Py_ImportFrom(c->v.Name.id, d, 0, EXTRA) }
311+
_PyAST_ImportFrom(c->v.Name.id, d, 0, EXTRA) }
312312
| a='from' '.' 'import' c=import_as_names_from {
313-
_Py_ImportFrom(NULL, c, 1, EXTRA) }
313+
_PyAST_ImportFrom(NULL, c, 1, EXTRA) }
314314
)
315315
simple_name[expr_ty]: NAME
316316
import_as_names_from[asdl_alias_seq*]: a[asdl_alias_seq*]=','.import_as_name_from+ { a }
317-
import_as_name_from[alias_ty]: a=NAME 'as' b=NAME { _Py_alias(((expr_ty) a)->v.Name.id, ((expr_ty) b)->v.Name.id, p->arena) }
317+
import_as_name_from[alias_ty]: a=NAME 'as' b=NAME { _PyAST_alias(((expr_ty) a)->v.Name.id, ((expr_ty) b)->v.Name.id, p->arena) }
318318
"""
319319
test_source = """
320320
for stmt in ("from a import b as c", "from . import a as b"):
@@ -326,19 +326,19 @@ def test_same_name_different_types(self) -> None:
326326

327327
def test_with_stmt_with_paren(self) -> None:
328328
grammar_source = """
329-
start[mod_ty]: a=[statements] ENDMARKER { Module(a, NULL, p->arena) }
329+
start[mod_ty]: a=[statements] ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
330330
statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
331331
statement[asdl_stmt_seq*]: a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
332332
compound_stmt[stmt_ty]: with_stmt
333333
with_stmt[stmt_ty]: (
334334
a='with' '(' b[asdl_withitem_seq*]=','.with_item+ ')' ':' c=block {
335-
_Py_With(b, (asdl_stmt_seq*) _PyPegen_singleton_seq(p, c), NULL, EXTRA) }
335+
_PyAST_With(b, (asdl_stmt_seq*) _PyPegen_singleton_seq(p, c), NULL, EXTRA) }
336336
)
337337
with_item[withitem_ty]: (
338-
e=NAME o=['as' t=NAME { t }] { _Py_withitem(e, _PyPegen_set_expr_context(p, o, Store), p->arena) }
338+
e=NAME o=['as' t=NAME { t }] { _PyAST_withitem(e, _PyPegen_set_expr_context(p, o, Store), p->arena) }
339339
)
340340
block[stmt_ty]: a=pass_stmt NEWLINE { a } | NEWLINE INDENT a=pass_stmt DEDENT { a }
341-
pass_stmt[stmt_ty]: a='pass' { _Py_Pass(EXTRA) }
341+
pass_stmt[stmt_ty]: a='pass' { _PyAST_Pass(EXTRA) }
342342
"""
343343
test_source = """
344344
stmt = "with (\\n a as b,\\n c as d\\n): pass"
@@ -352,14 +352,14 @@ def test_with_stmt_with_paren(self) -> None:
352352

353353
def test_ternary_operator(self) -> None:
354354
grammar_source = """
355-
start[mod_ty]: a=expr ENDMARKER { Module(a, NULL, p->arena) }
356-
expr[asdl_stmt_seq*]: a=listcomp NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, _Py_Expr(a, EXTRA)) }
355+
start[mod_ty]: a=expr ENDMARKER { _PyAST_Module(a, NULL, p->arena) }
356+
expr[asdl_stmt_seq*]: a=listcomp NEWLINE { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, _PyAST_Expr(a, EXTRA)) }
357357
listcomp[expr_ty]: (
358-
a='[' b=NAME c=for_if_clauses d=']' { _Py_ListComp(b, c, EXTRA) }
358+
a='[' b=NAME c=for_if_clauses d=']' { _PyAST_ListComp(b, c, EXTRA) }
359359
)
360360
for_if_clauses[asdl_comprehension_seq*]: (
361361
a[asdl_comprehension_seq*]=(y=[ASYNC] 'for' a=NAME 'in' b=NAME c[asdl_expr_seq*]=('if' z=NAME { z })*
362-
{ _Py_comprehension(_Py_Name(((expr_ty) a)->v.Name.id, Store, EXTRA), b, c, (y == NULL) ? 0 : 1, p->arena) })+ { a }
362+
{ _PyAST_comprehension(_PyAST_Name(((expr_ty) a)->v.Name.id, Store, EXTRA), b, c, (y == NULL) ? 0 : 1, p->arena) })+ { a }
363363
)
364364
"""
365365
test_source = """

Parser/asdl_c.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ def visitProduct(self, product, name, depth):
264264
self.emit("", depth)
265265

266266

267+
def ast_func_name(name):
268+
return f"_PyAST_{name}"
269+
270+
267271
class PrototypeVisitor(EmitVisitor):
268272
"""Generate function prototypes for the .h file"""
269273

@@ -322,16 +326,7 @@ def emit_function(self, name, ctype, args, attrs, union=True):
322326
argstr += ", PyArena *arena"
323327
else:
324328
argstr = "PyArena *arena"
325-
margs = "a0"
326-
for i in range(1, len(args)+1):
327-
margs += ", a%d" % i
328-
# bpo-43244: <winbase.h> defines Yield macro. Don't redefine it in
329-
# pycore_ast.h: it is not needed outside Python-ast.c which calls
330-
# directly _Py_Yield().
331-
if name != "Yield":
332-
self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0,
333-
reflow=False)
334-
self.emit("%s _Py_%s(%s);" % (ctype, name, argstr), False)
329+
self.emit("%s %s(%s);" % (ctype, ast_func_name(name), argstr), False)
335330

336331
def visitProduct(self, prod, name):
337332
self.emit_function(name, get_c_type(name),
@@ -340,10 +335,6 @@ def visitProduct(self, prod, name):
340335
union=False)
341336

342337

343-
def pyfunc_name(name):
344-
return f"_Py_{name}"
345-
346-
347338
class FunctionVisitor(PrototypeVisitor):
348339
"""Visitor to generate constructor functions for AST."""
349340

@@ -357,7 +348,7 @@ def emit(s, depth=0, reflow=True):
357348
else:
358349
argstr = "PyArena *arena"
359350
self.emit("%s" % ctype, 0)
360-
emit("%s(%s)" % (pyfunc_name(name), argstr))
351+
emit("%s(%s)" % (ast_func_name(name), argstr))
361352
emit("{")
362353
emit("%s p;" % ctype, 1)
363354
for argtype, argname, opt in args:
@@ -496,7 +487,7 @@ def complexSum(self, sum, name):
496487
for f in t.fields:
497488
self.visitField(f, t.name, sum=sum, depth=2)
498489
args = [f.name for f in t.fields] + [a.name for a in sum.attributes]
499-
self.emit("*out = %s(%s);" % (pyfunc_name(t.name), self.buildArgs(args)), 2)
490+
self.emit("*out = %s(%s);" % (ast_func_name(t.name), self.buildArgs(args)), 2)
500491
self.emit("if (*out == NULL) goto failed;", 2)
501492
self.emit("return 0;", 2)
502493
self.emit("}", 1)
@@ -529,7 +520,7 @@ def visitProduct(self, prod, name):
529520
self.visitField(a, name, prod=prod, depth=1)
530521
args = [f.name for f in prod.fields]
531522
args.extend([a.name for a in prod.attributes])
532-
self.emit("*out = %s(%s);" % (pyfunc_name(name), self.buildArgs(args)), 1)
523+
self.emit("*out = %s(%s);" % (ast_func_name(name), self.buildArgs(args)), 1)
533524
self.emit("return 0;", 1)
534525
self.emit("failed:", 0)
535526
self.emit("Py_XDECREF(tmp);", 1)

0 commit comments

Comments
 (0)