diff options
author | Victor Stinner <vstinner@python.org> | 2021-04-07 11:01:09 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-07 11:01:09 (GMT) |
commit | d36d6a9c1808e87628ebaa855d4bec80130189f4 (patch) | |
tree | 2b4633ff2e80878f75abad58d8a768a14160b1fc /Parser/asdl_c.py | |
parent | 67969f5eb80844b68005181fd887bcf94c01fb40 (diff) | |
download | cpython-d36d6a9c1808e87628ebaa855d4bec80130189f4.zip cpython-d36d6a9c1808e87628ebaa855d4bec80130189f4.tar.gz cpython-d36d6a9c1808e87628ebaa855d4bec80130189f4.tar.bz2 |
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.
Diffstat (limited to 'Parser/asdl_c.py')
-rwxr-xr-x | Parser/asdl_c.py | 67 |
1 files changed, 31 insertions, 36 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 3bdeedb..02be1b3 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -325,8 +325,12 @@ class PrototypeVisitor(EmitVisitor): 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): @@ -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) - - 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) @@ -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( |