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.
This commit is contained in:
Victor Stinner 2021-04-07 13:01:09 +02:00 committed by GitHub
parent 67969f5eb8
commit d36d6a9c18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 320 additions and 326 deletions

View File

@ -12,8 +12,6 @@ extern "C" {
#include "pycore_asdl.h"
#undef Yield /* undefine macro conflicting with <winbase.h> */
typedef struct _mod *mod_ty;
typedef struct _stmt *stmt_ty;
@ -729,7 +727,6 @@ expr_ty _Py_GeneratorExp(expr_ty elt, asdl_comprehension_seq * generators, int
#define Await(a0, a1, a2, a3, a4, a5) _Py_Await(a0, a1, a2, a3, a4, a5)
expr_ty _Py_Await(expr_ty value, int lineno, int col_offset, int end_lineno,
int end_col_offset, PyArena *arena);
#define Yield(a0, a1, a2, a3, a4, a5) _Py_Yield(a0, a1, a2, a3, a4, a5)
expr_ty _Py_Yield(expr_ty value, int lineno, int col_offset, int end_lineno,
int end_col_offset, PyArena *arena);
#define YieldFrom(a0, a1, a2, a3, a4, a5) _Py_YieldFrom(a0, a1, a2, a3, a4, a5)

View File

@ -325,6 +325,10 @@ class PrototypeVisitor(EmitVisitor):
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)
@ -336,6 +340,10 @@ class PrototypeVisitor(EmitVisitor):
union=False)
def pyfunc_name(name):
return f"_Py_{name}"
class FunctionVisitor(PrototypeVisitor):
"""Visitor to generate constructor functions for AST."""
@ -349,7 +357,7 @@ class FunctionVisitor(PrototypeVisitor):
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:
@ -488,7 +496,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);" % (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)
@ -521,7 +529,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);" % (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)
@ -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)
#include "structmember.h"
#include <stddef.h>
f.write("""
// Forward declaration
static int init_types(struct ast_state *state);
// Forward declaration
static int init_types(struct ast_state *state);
static struct ast_state*
get_ast_state(void)
{
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;
}
""")
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)
}
""").strip(), file=f)
generate_ast_fini(module_state, f)
@ -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),
@ -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(

441
Python/Python-ast.c generated

File diff suppressed because it is too large Load Diff

View File

@ -451,7 +451,7 @@ astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state)
return 0;
}
asdl_seq_SET(values, 0, st->v.Expr.value);
expr_ty expr = JoinedStr(values, st->lineno, st->col_offset,
expr_ty expr = _Py_JoinedStr(values, st->lineno, st->col_offset,
st->end_lineno, st->end_col_offset, ctx_);
if (!expr) {
return 0;

View File

@ -3,7 +3,6 @@
#include "Python.h"
#include <ctype.h>
#include "pycore_ast.h" // _PyAST_Validate()
#undef Yield /* undefine macro conflicting with <winbase.h> */
#include "pycore_compile.h" // _PyAST_Compile()
#include "pycore_object.h" // _Py_AddToAllObjects()
#include "pycore_pyerrors.h" // _PyErr_NoMemory()

View File

@ -2,7 +2,6 @@
#include "Python.h"
#undef Yield /* undefine macro conflicting with <winbase.h> */
#include "pycore_import.h" // _PyImport_BootstrapImp()
#include "pycore_initconfig.h"
#include "pycore_pyerrors.h"

View File

@ -11,8 +11,6 @@
#include "Python.h"
#include "pycore_ast.h" // PyAST_mod2obj
#undef Yield /* undefine macro conflicting with <winbase.h> */
#include "pycore_compile.h" // _PyAST_Compile()
#include "pycore_interp.h" // PyInterpreterState.importlib
#include "pycore_object.h" // _PyDebug_PrintTotalRefs()

View File

@ -1,6 +1,5 @@
#include "Python.h"
#include "pycore_ast.h" // identifier, stmt_ty
#undef Yield /* undefine macro conflicting with <winbase.h> */
#include "pycore_compile.h" // _Py_Mangle()
#include "pycore_parser.h" // _PyParser_ASTFromString()
#include "pycore_pystate.h" // _PyThreadState_GET()