Skip to content

bpo-43244: Remove Yield macro from pycore_ast.h #25243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Include/internal/pycore_ast.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 31 additions & 36 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,12 @@ def emit_function(self, name, ctype, args, attrs, union=True):
margs = "a0"
for i in range(1, len(args)+1):
margs += ", a%d" % i
self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0,
reflow=False)
# 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)

def visitProduct(self, prod, name):
Expand All @@ -336,6 +340,10 @@ def visitProduct(self, prod, name):
union=False)


def pyfunc_name(name):
return f"_Py_{name}"


class FunctionVisitor(PrototypeVisitor):
"""Visitor to generate constructor functions for AST."""

Expand All @@ -349,7 +357,7 @@ def emit(s, depth=0, reflow=True):
else:
argstr = "PyArena *arena"
self.emit("%s" % ctype, 0)
emit("%s(%s)" % (name, argstr))
emit("%s(%s)" % (pyfunc_name(name), argstr))
emit("{")
emit("%s p;" % ctype, 1)
for argtype, argname, opt in args:
Expand Down Expand Up @@ -488,7 +496,7 @@ def complexSum(self, sum, name):
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);" % (t.name, self.buildArgs(args)), 2)
self.emit("*out = %s(%s);" % (pyfunc_name(t.name), self.buildArgs(args)), 2)
self.emit("if (*out == NULL) goto failed;", 2)
self.emit("return 0;", 2)
self.emit("}", 1)
Expand Down Expand Up @@ -521,7 +529,7 @@ def visitProduct(self, prod, name):
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);" % (name, self.buildArgs(args)), 1)
self.emit("*out = %s(%s);" % (pyfunc_name(name), self.buildArgs(args)), 1)
self.emit("return 0;", 1)
self.emit("failed:", 0)
self.emit("Py_XDECREF(tmp);", 1)
Expand Down Expand Up @@ -1423,34 +1431,29 @@ def generate_module_def(mod, f, internal_h):

generate_ast_state(module_state, internal_h)

print(textwrap.dedent(f"""
print(textwrap.dedent("""
#include "Python.h"
#include "pycore_ast.h"
#include "pycore_ast_state.h" // struct ast_state
#include "pycore_interp.h" // _PyInterpreterState.ast
#include "pycore_pystate.h" // _PyInterpreterState_GET()
""").rstrip(), file=f)

f.write("""
// Forward declaration
static int init_types(struct ast_state *state);
#include "structmember.h"
#include <stddef.h>

static struct ast_state*
get_ast_state(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
struct ast_state *state = &interp->ast;
if (!init_types(state)) {
return NULL;
}
return state;
}
""")
// Forward declaration
static int init_types(struct ast_state *state);

print(textwrap.dedent("""
// Include pycore_ast.h after pycore_interp.h to avoid conflicts
// with the Yield macro redefined by <winbase.h>
#include "pycore_ast.h"
#include "structmember.h"
""").rstrip(), file=f)
static struct ast_state*
get_ast_state(void)
{
PyInterpreterState *interp = _PyInterpreterState_GET();
struct ast_state *state = &interp->ast;
if (!init_types(state)) {
return NULL;
}
return state;
}
""").strip(), file=f)

generate_ast_fini(module_state, f)

Expand All @@ -1477,8 +1480,6 @@ def write_header(mod, f):

#include "pycore_asdl.h"

#undef Yield /* undefine macro conflicting with <winbase.h> */

""").lstrip())
c = ChainOfVisitors(TypeDefVisitor(f),
SequenceDefVisitor(f),
Expand Down Expand Up @@ -1534,12 +1535,6 @@ def write_internal_h_footer(mod, f):


def write_source(mod, f, internal_h_file):
print(textwrap.dedent(f"""
#include <stddef.h>

#include "Python.h"
"""), file=f)

generate_module_def(mod, f, internal_h_file)

v = ChainOfVisitors(
Expand Down
Loading