@@ -325,8 +325,12 @@ def emit_function(self, name, ctype, args, attrs, union=True):
325
325
margs = "a0"
326
326
for i in range (1 , len (args )+ 1 ):
327
327
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 )
330
334
self .emit ("%s _Py_%s(%s);" % (ctype , name , argstr ), False )
331
335
332
336
def visitProduct (self , prod , name ):
@@ -336,6 +340,10 @@ def visitProduct(self, prod, name):
336
340
union = False )
337
341
338
342
343
+ def pyfunc_name (name ):
344
+ return f"_Py_{ name } "
345
+
346
+
339
347
class FunctionVisitor (PrototypeVisitor ):
340
348
"""Visitor to generate constructor functions for AST."""
341
349
@@ -349,7 +357,7 @@ def emit(s, depth=0, reflow=True):
349
357
else :
350
358
argstr = "PyArena *arena"
351
359
self .emit ("%s" % ctype , 0 )
352
- emit ("%s(%s)" % (name , argstr ))
360
+ emit ("%s(%s)" % (pyfunc_name ( name ) , argstr ))
353
361
emit ("{" )
354
362
emit ("%s p;" % ctype , 1 )
355
363
for argtype , argname , opt in args :
@@ -488,7 +496,7 @@ def complexSum(self, sum, name):
488
496
for f in t .fields :
489
497
self .visitField (f , t .name , sum = sum , depth = 2 )
490
498
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 )
492
500
self .emit ("if (*out == NULL) goto failed;" , 2 )
493
501
self .emit ("return 0;" , 2 )
494
502
self .emit ("}" , 1 )
@@ -521,7 +529,7 @@ def visitProduct(self, prod, name):
521
529
self .visitField (a , name , prod = prod , depth = 1 )
522
530
args = [f .name for f in prod .fields ]
523
531
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 )
525
533
self .emit ("return 0;" , 1 )
526
534
self .emit ("failed:" , 0 )
527
535
self .emit ("Py_XDECREF(tmp);" , 1 )
@@ -1423,34 +1431,29 @@ def generate_module_def(mod, f, internal_h):
1423
1431
1424
1432
generate_ast_state (module_state , internal_h )
1425
1433
1426
- print (textwrap .dedent (f"""
1434
+ print (textwrap .dedent ("""
1435
+ #include "Python.h"
1436
+ #include "pycore_ast.h"
1427
1437
#include "pycore_ast_state.h" // struct ast_state
1428
1438
#include "pycore_interp.h" // _PyInterpreterState.ast
1429
1439
#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>
1435
1442
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);
1447
1445
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 )
1454
1457
1455
1458
generate_ast_fini (module_state , f )
1456
1459
@@ -1477,8 +1480,6 @@ def write_header(mod, f):
1477
1480
1478
1481
#include "pycore_asdl.h"
1479
1482
1480
- #undef Yield /* undefine macro conflicting with <winbase.h> */
1481
-
1482
1483
""" ).lstrip ())
1483
1484
c = ChainOfVisitors (TypeDefVisitor (f ),
1484
1485
SequenceDefVisitor (f ),
@@ -1534,12 +1535,6 @@ def write_internal_h_footer(mod, f):
1534
1535
1535
1536
1536
1537
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
-
1543
1538
generate_module_def (mod , f , internal_h_file )
1544
1539
1545
1540
v = ChainOfVisitors (
0 commit comments