Skip to content

Commit d36d6a9

Browse files
authored
bpo-43244: Remove Yield macro from pycore_ast.h (GH-25243)
* pycore_ast.h no longer defines the Yield macro. * Fix a compiler warning on Windows: "warning C4005: 'Yield': macro redefinition". * Python-ast.c now defines directly functions with their real _Py_xxx() name, rather than xxx(). * Remove "#undef Yield" in C files including pycore_ast.h.
1 parent 67969f5 commit d36d6a9

File tree

8 files changed

+319
-325
lines changed

8 files changed

+319
-325
lines changed

Include/internal/pycore_ast.h

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

Parser/asdl_c.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,12 @@ def emit_function(self, name, ctype, args, attrs, union=True):
325325
margs = "a0"
326326
for i in range(1, len(args)+1):
327327
margs += ", a%d" % i
328-
self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0,
329-
reflow=False)
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)
330334
self.emit("%s _Py_%s(%s);" % (ctype, name, argstr), False)
331335

332336
def visitProduct(self, prod, name):
@@ -336,6 +340,10 @@ def visitProduct(self, prod, name):
336340
union=False)
337341

338342

343+
def pyfunc_name(name):
344+
return f"_Py_{name}"
345+
346+
339347
class FunctionVisitor(PrototypeVisitor):
340348
"""Visitor to generate constructor functions for AST."""
341349

@@ -349,7 +357,7 @@ def emit(s, depth=0, reflow=True):
349357
else:
350358
argstr = "PyArena *arena"
351359
self.emit("%s" % ctype, 0)
352-
emit("%s(%s)" % (name, argstr))
360+
emit("%s(%s)" % (pyfunc_name(name), argstr))
353361
emit("{")
354362
emit("%s p;" % ctype, 1)
355363
for argtype, argname, opt in args:
@@ -488,7 +496,7 @@ def complexSum(self, sum, name):
488496
for f in t.fields:
489497
self.visitField(f, t.name, sum=sum, depth=2)
490498
args = [f.name for f in t.fields] + [a.name for a in sum.attributes]
491-
self.emit("*out = %s(%s);" % (t.name, self.buildArgs(args)), 2)
499+
self.emit("*out = %s(%s);" % (pyfunc_name(t.name), self.buildArgs(args)), 2)
492500
self.emit("if (*out == NULL) goto failed;", 2)
493501
self.emit("return 0;", 2)
494502
self.emit("}", 1)
@@ -521,7 +529,7 @@ def visitProduct(self, prod, name):
521529
self.visitField(a, name, prod=prod, depth=1)
522530
args = [f.name for f in prod.fields]
523531
args.extend([a.name for a in prod.attributes])
524-
self.emit("*out = %s(%s);" % (name, self.buildArgs(args)), 1)
532+
self.emit("*out = %s(%s);" % (pyfunc_name(name), self.buildArgs(args)), 1)
525533
self.emit("return 0;", 1)
526534
self.emit("failed:", 0)
527535
self.emit("Py_XDECREF(tmp);", 1)
@@ -1423,34 +1431,29 @@ def generate_module_def(mod, f, internal_h):
14231431

14241432
generate_ast_state(module_state, internal_h)
14251433

1426-
print(textwrap.dedent(f"""
1434+
print(textwrap.dedent("""
1435+
#include "Python.h"
1436+
#include "pycore_ast.h"
14271437
#include "pycore_ast_state.h" // struct ast_state
14281438
#include "pycore_interp.h" // _PyInterpreterState.ast
14291439
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1430-
""").rstrip(), file=f)
1431-
1432-
f.write("""
1433-
// Forward declaration
1434-
static int init_types(struct ast_state *state);
1440+
#include "structmember.h"
1441+
#include <stddef.h>
14351442
1436-
static struct ast_state*
1437-
get_ast_state(void)
1438-
{
1439-
PyInterpreterState *interp = _PyInterpreterState_GET();
1440-
struct ast_state *state = &interp->ast;
1441-
if (!init_types(state)) {
1442-
return NULL;
1443-
}
1444-
return state;
1445-
}
1446-
""")
1443+
// Forward declaration
1444+
static int init_types(struct ast_state *state);
14471445
1448-
print(textwrap.dedent("""
1449-
// Include pycore_ast.h after pycore_interp.h to avoid conflicts
1450-
// with the Yield macro redefined by <winbase.h>
1451-
#include "pycore_ast.h"
1452-
#include "structmember.h"
1453-
""").rstrip(), file=f)
1446+
static struct ast_state*
1447+
get_ast_state(void)
1448+
{
1449+
PyInterpreterState *interp = _PyInterpreterState_GET();
1450+
struct ast_state *state = &interp->ast;
1451+
if (!init_types(state)) {
1452+
return NULL;
1453+
}
1454+
return state;
1455+
}
1456+
""").strip(), file=f)
14541457

14551458
generate_ast_fini(module_state, f)
14561459

@@ -1477,8 +1480,6 @@ def write_header(mod, f):
14771480
14781481
#include "pycore_asdl.h"
14791482
1480-
#undef Yield /* undefine macro conflicting with <winbase.h> */
1481-
14821483
""").lstrip())
14831484
c = ChainOfVisitors(TypeDefVisitor(f),
14841485
SequenceDefVisitor(f),
@@ -1534,12 +1535,6 @@ def write_internal_h_footer(mod, f):
15341535

15351536

15361537
def write_source(mod, f, internal_h_file):
1537-
print(textwrap.dedent(f"""
1538-
#include <stddef.h>
1539-
1540-
#include "Python.h"
1541-
"""), file=f)
1542-
15431538
generate_module_def(mod, f, internal_h_file)
15441539

15451540
v = ChainOfVisitors(

0 commit comments

Comments
 (0)