Skip to content

bpo-35081: Enhance Python-ast.h and ast.h #10277

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions Include/Python-ast.h

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

3 changes: 3 additions & 0 deletions Include/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
extern "C" {
#endif

#include "node.h" /* node */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other option is to add forward declarations, as in compile.h.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, maybe, but it seems like node.h is included anyway in all C files including ast.h. I don't see the benefit of avoiding to include node.h here. I prefer to be explicit and link ast.h to node.h.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include "Python-ast.h" is already surrounded with extern "C" { in ast.h.

#include "Python-ast.h" /* mod_ty */

PyAPI_FUNC(int) PyAST_Validate(mod_ty);
PyAPI_FUNC(mod_ty) PyAST_FromNode(
const node *n,
Expand Down
1 change: 0 additions & 1 deletion Modules/parsermodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "grammar.h"
#include "parsetok.h"
/* ISTERMINAL() / ISNONTERMINAL() */
#undef Yield
#include "ast.h"

extern grammar _PyParser_Grammar; /* From graminit.c */
Expand Down
16 changes: 15 additions & 1 deletion Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,16 @@ def main(srcfile, dump_module=False):
if H_FILE:
with open(H_FILE, "w") as f:
f.write(auto_gen_msg)
f.write('#include "asdl.h"\n\n')
f.write('#ifndef PYTHON_AST_H\n')
f.write('#define PYTHON_AST_H\n')
f.write('#ifdef __cplusplus\n')
f.write('extern "C" {\n')
f.write('#endif\n')
f.write('\n')
f.write('#include "asdl.h"\n')
f.write('\n')
f.write('#undef Yield /* undefine macro conflicting with winbase.h */\n')
f.write('\n')
c = ChainOfVisitors(TypeDefVisitor(f),
StructVisitor(f),
PrototypeVisitor(f),
Expand All @@ -1248,6 +1257,11 @@ def main(srcfile, dump_module=False):
f.write("PyObject* PyAST_mod2obj(mod_ty t);\n")
f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n")
f.write("int PyAST_Check(PyObject* obj);\n")
f.write('\n')
f.write('#ifdef __cplusplus\n')
f.write('}\n')
f.write('#endif\n')
f.write('#endif /* !PYTHON_AST_H */\n')

if C_FILE:
with open(C_FILE, "w") as f:
Expand Down
1 change: 0 additions & 1 deletion Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "Python.h"

#include "Python-ast.h"
#undef Yield /* undefine macro conflicting with winbase.h */
#include "pycore_hash.h"
#include "pycore_lifecycle.h"
#include "pycore_mem.h"
Expand Down
1 change: 0 additions & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "Python.h"

#include "Python-ast.h"
#undef Yield /* undefine macro conflicting with winbase.h */
#include "pycore_context.h"
#include "pycore_hamt.h"
#include "pycore_lifecycle.h"
Expand Down
1 change: 0 additions & 1 deletion Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "Python.h"

#include "Python-ast.h"
#undef Yield /* undefine macro conflicting with winbase.h */
#include "pycore_state.h"
#include "grammar.h"
#include "node.h"
Expand Down
3 changes: 0 additions & 3 deletions Python/symtable.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "Python.h"
#include "pycore_state.h"
#ifdef Yield
#undef Yield /* undefine conflicting macro from winbase.h */
#endif
#include "Python-ast.h"
#include "code.h"
#include "symtable.h"
Expand Down